public void ValidateTest(string content)
        {
            var parseResult = CustomUploader.TryParse(content, out var result);

            Assert.True(parseResult);
            Assert.NotNull(result);
            Assert.NotNull(result !.UploaderInfo);
            Assert.NotNull(result.UploaderInfo.Meta);
            Assert.NotNull(result.UploaderInfo.Uploader);
            Assert.NotNull(result.UploaderInfo.SchemaVersion);

            Assert.True(result.UploaderInfo.Meta.Version == new Semver.SemVersion(1, 0, 0));
        }
Beispiel #2
0
        /// <summary> Can also be safely called for reloads. </summary>
        public async Task Load()
        {
            try
            {
                HolzShotsPaths.EnsureDirectory(_customUploadersDirectory);
                // var res = new Dictionary<UploaderMeta, CustomUploader>();

                var res   = ImmutableDictionary.CreateBuilder <UploaderMeta, CustomUploader>();
                var files = ImmutableDictionary.CreateBuilder <string, CustomUploaderSpec>();

                foreach (var jsonFile in Directory.EnumerateFiles(_customUploadersDirectory, HolzShotsPaths.CustomUploadersFilePattern))
                {
                    using var reader = File.OpenText(jsonFile);
                    var jsonStr = await reader.ReadToEndAsync().ConfigureAwait(false);

                    // TODO: Catch parsing errors
                    var uploader = JsonConvert.DeserializeObject <CustomUploaderSpec>(jsonStr, JsonConfig.JsonSettings);

                    // TODO: Aggregate errors of invalid files (and display them to the user)
                    Debug.Assert(uploader != null);

                    if (CustomUploader.TryLoad(uploader, out var loadedUploader))
                    {
                        Debug.Assert(loadedUploader != null);
                        res.Add(uploader.Meta, loadedUploader);
                        files.Add(jsonFile, uploader);
                    }
                }

                _uploadersFiles  = files.ToImmutable();
                _customUploaders = res.ToImmutable();
            }
            catch (FileNotFoundException)
            {
                _customUploaders = ImmutableDictionary <UploaderMeta, CustomUploader> .Empty;
            }
            finally
            {
                Loaded = true;
            }
        }
Beispiel #3
0
        private void TestCustomUploader(CustomUploaderInfo cui)
        {
            UploadResult ur = null;

            AsyncHelper.AsyncJob(() =>
            {
                try
                {
                    using (Stream stream = Resources.ZScreen_256.GetStream())
                    {
                        CustomUploader cu = new CustomUploader(cui);
                        ur        = cu.Upload(stream, "Test.png");
                        ur.Errors = cu.Errors;
                    }
                }
                catch { }
            },
                                 () =>
            {
                if (ur != null)
                {
                    if (!string.IsNullOrEmpty(ur.URL))
                    {
                        txtCustomUploaderLog.AppendText("URL: " + ur.URL + Environment.NewLine);
                    }
                    else if (ur.IsError)
                    {
                        txtCustomUploaderLog.AppendText("Error: " + ur.ErrorsToString() + Environment.NewLine);
                    }
                    else
                    {
                        txtCustomUploaderLog.AppendText("Error: Result is empty." + Environment.NewLine);
                    }

                    txtCustomUploaderLog.ScrollToCaret();
                }

                btnCustomUploaderTest.Enabled = true;
            });
        }
        public UploadResult UploadFile(Stream stream, string fileName)
        {
            FileUploader fileUploader = null;

            switch (UploadManager.FileUploader)
            {
            case FileDestination.Dropbox:
                NameParser parser = new NameParser {
                    IsFolderPath = true
                };
                string uploadPath = parser.Convert(Dropbox.TidyUploadPath(Program.UploadersConfig.DropboxUploadPath));
                fileUploader = new Dropbox(Program.UploadersConfig.DropboxOAuthInfo, uploadPath, Program.UploadersConfig.DropboxAccountInfo)
                {
                    AutoCreateShareableLink = Program.UploadersConfig.DropboxAutoCreateShareableLink
                };
                break;

            case FileDestination.RapidShare:
                fileUploader = new RapidShare(Program.UploadersConfig.RapidShareUsername, Program.UploadersConfig.RapidSharePassword,
                                              Program.UploadersConfig.RapidShareFolderID);
                break;

            case FileDestination.SendSpace:
                fileUploader = new SendSpace(ZKeys.SendSpaceKey);
                switch (Program.UploadersConfig.SendSpaceAccountType)
                {
                case AccountType.Anonymous:
                    SendSpaceManager.PrepareUploadInfo(ZKeys.SendSpaceKey);
                    break;

                case AccountType.User:
                    SendSpaceManager.PrepareUploadInfo(ZKeys.SendSpaceKey, Program.UploadersConfig.SendSpaceUsername, Program.UploadersConfig.SendSpacePassword);
                    break;
                }
                break;

            case FileDestination.Minus:
                fileUploader = new Minus(Program.UploadersConfig.MinusConfig, new OAuthInfo(ZKeys.MinusConsumerKey, ZKeys.MinusConsumerSecret));
                break;

            case FileDestination.Box:
                fileUploader = new Box(ZKeys.BoxKey)
                {
                    AuthToken = Program.UploadersConfig.BoxAuthToken,
                    FolderID  = Program.UploadersConfig.BoxFolderID,
                    Share     = Program.UploadersConfig.BoxShare
                };
                break;

            case FileDestination.CustomUploader:
                if (Program.UploadersConfig.CustomUploadersList.HasValidIndex(Program.UploadersConfig.CustomUploaderSelected))
                {
                    fileUploader = new CustomUploader(Program.UploadersConfig.CustomUploadersList[Program.UploadersConfig.CustomUploaderSelected]);
                }
                break;

            case FileDestination.FTP:
                int index = Program.UploadersConfig.GetFtpIndex(Info.DataType);

                if (Program.UploadersConfig.FTPAccountList2.HasValidIndex(index))
                {
                    fileUploader = new FTPUploader(Program.UploadersConfig.FTPAccountList2[index]);
                }
                break;

            case FileDestination.Email:
                using (EmailForm emailForm = new EmailForm(Program.UploadersConfig.EmailRememberLastTo ? Program.UploadersConfig.EmailLastTo : string.Empty,
                                                           Program.UploadersConfig.EmailDefaultSubject, Program.UploadersConfig.EmailDefaultBody))
                {
                    if (emailForm.ShowDialog() == DialogResult.OK)
                    {
                        if (Program.UploadersConfig.EmailRememberLastTo)
                        {
                            Program.UploadersConfig.EmailLastTo = emailForm.ToEmail;
                        }

                        fileUploader = new Email
                        {
                            SmtpServer = Program.UploadersConfig.EmailSmtpServer,
                            SmtpPort   = Program.UploadersConfig.EmailSmtpPort,
                            FromEmail  = Program.UploadersConfig.EmailFrom,
                            Password   = Program.UploadersConfig.EmailPassword,
                            ToEmail    = emailForm.ToEmail,
                            Subject    = emailForm.Subject,
                            Body       = emailForm.Body
                        };
                    }
                    else
                    {
                        IsStopped = true;
                    }
                }
                break;
            }

            if (fileUploader != null)
            {
                PrepareUploader(fileUploader);
                return(fileUploader.Upload(stream, fileName));
            }

            return(null);
        }