示例#1
0
文件: UploadTask.cs 项目: Z1ni/ShareX
        public UploadResult UploadText(Stream stream, string fileName)
        {
            TextUploader textUploader = null;

            switch (Info.TaskSettings.TextDestination)
            {
            case TextDestination.Pastebin:
                PastebinSettings settings = Program.UploadersConfig.PastebinSettings;
                if (string.IsNullOrEmpty(settings.TextFormat))
                {
                    settings.TextFormat = Info.TaskSettings.AdvancedSettings.TextFormat;
                }
                textUploader = new Pastebin(APIKeys.PastebinKey, settings);
                break;

            case TextDestination.PastebinCA:
                textUploader = new Pastebin_ca(APIKeys.PastebinCaKey, new PastebinCaSettings {
                    TextFormat = Info.TaskSettings.AdvancedSettings.TextFormat
                });
                break;

            case TextDestination.Paste2:
                textUploader = new Paste2(new Paste2Settings {
                    TextFormat = Info.TaskSettings.AdvancedSettings.TextFormat
                });
                break;

            case TextDestination.Slexy:
                textUploader = new Slexy(new SlexySettings {
                    TextFormat = Info.TaskSettings.AdvancedSettings.TextFormat
                });
                break;

            case TextDestination.Pastee:
                textUploader = new Pastee {
                    Lexer = Info.TaskSettings.AdvancedSettings.TextFormat
                };
                break;

            case TextDestination.Paste_ee:
                textUploader = new Paste_ee(Program.UploadersConfig.Paste_eeUserAPIKey);
                break;

            case TextDestination.Gist:
                textUploader = Program.UploadersConfig.GistAnonymousLogin
                        ? new Gist(Program.UploadersConfig.GistPublishPublic)
                        : new Gist(Program.UploadersConfig.GistPublishPublic, Program.UploadersConfig.GistOAuth2Info);
                break;

            case TextDestination.Upaste:
                textUploader = new Upaste(Program.UploadersConfig.UpasteUserKey)
                {
                    IsPublic = Program.UploadersConfig.UpasteIsPublic
                };
                break;

            case TextDestination.CustomTextUploader:
                if (Program.UploadersConfig.CustomUploadersList.IsValidIndex(Program.UploadersConfig.CustomTextUploaderSelected))
                {
                    textUploader = new CustomTextUploader(Program.UploadersConfig.CustomUploadersList[Program.UploadersConfig.CustomTextUploaderSelected]);
                }
                break;
            }

            if (textUploader != null)
            {
                PrepareUploader(textUploader);
                return(textUploader.UploadText(stream, fileName));
            }

            return(null);
        }
        private async Task TestCustomUploader(CustomUploaderDestinationType type, int index)
        {
            if (!Config.CustomUploadersList.IsValidIndex(index))
            {
                return;
            }

            btnImageUploaderTest.Enabled    = btnTextUploaderTest.Enabled = btnFileUploaderTest.Enabled =
                btnURLShortenerTest.Enabled = btnURLSharingServiceTest.Enabled = false;
            rtbResult.ResetText();
            txtResponseText.ResetText();
            lbCustomUploaderList.SelectedIndex = index;

            CustomUploaderItem item   = Config.CustomUploadersList[index];
            UploadResult       result = null;

            await Task.Run(() =>
            {
                try
                {
                    switch (type)
                    {
                    case CustomUploaderDestinationType.ImageUploader:
                        using (Stream stream = ShareXResources.Logo.GetStream())
                        {
                            CustomImageUploader imageUploader = new CustomImageUploader(item);
                            result        = imageUploader.Upload(stream, "Test.png");
                            result.Errors = imageUploader.Errors;
                        }
                        break;

                    case CustomUploaderDestinationType.TextUploader:
                        CustomTextUploader textUploader = new CustomTextUploader(item);
                        result        = textUploader.UploadText("ShareX text upload test", "Test.txt");
                        result.Errors = textUploader.Errors;
                        break;

                    case CustomUploaderDestinationType.FileUploader:
                        using (Stream stream = ShareXResources.Logo.GetStream())
                        {
                            CustomFileUploader fileUploader = new CustomFileUploader(item);
                            result        = fileUploader.Upload(stream, "Test.png");
                            result.Errors = fileUploader.Errors;
                        }
                        break;

                    case CustomUploaderDestinationType.URLShortener:
                        CustomURLShortener urlShortener = new CustomURLShortener(item);
                        result        = urlShortener.ShortenURL(Links.URL_WEBSITE);
                        result.Errors = urlShortener.Errors;
                        break;

                    case CustomUploaderDestinationType.URLSharingService:
                        CustomURLSharer urlSharer = new CustomURLSharer(item);
                        result        = urlSharer.ShareURL(Links.URL_WEBSITE);
                        result.Errors = urlSharer.Errors;
                        break;
                    }
                }
                catch (Exception e)
                {
                    result = new UploadResult();
                    result.Errors.Add(e.Message);
                }
            });

            if (!IsDisposed)
            {
                if (result != null)
                {
                    StringBuilder sbResult = new StringBuilder();

                    if (((type == CustomUploaderDestinationType.ImageUploader || type == CustomUploaderDestinationType.TextUploader ||
                          type == CustomUploaderDestinationType.FileUploader) && !string.IsNullOrEmpty(result.URL)) ||
                        (type == CustomUploaderDestinationType.URLShortener && !string.IsNullOrEmpty(result.ShortenedURL)) ||
                        (type == CustomUploaderDestinationType.URLSharingService && !result.IsError && !string.IsNullOrEmpty(result.URL)))
                    {
                        if (!string.IsNullOrEmpty(result.ShortenedURL))
                        {
                            sbResult.AppendLine("Shortened URL: " + result.ShortenedURL);
                        }

                        if (!string.IsNullOrEmpty(result.URL))
                        {
                            sbResult.AppendLine("URL: " + result.URL);
                        }

                        if (!string.IsNullOrEmpty(result.ThumbnailURL))
                        {
                            sbResult.AppendLine("Thumbnail URL: " + result.ThumbnailURL);
                        }

                        if (!string.IsNullOrEmpty(result.DeletionURL))
                        {
                            sbResult.AppendLine("Deletion URL: " + result.DeletionURL);
                        }
                    }
                    else if (result.IsError)
                    {
                        sbResult.AppendLine(result.ErrorsToString());
                    }
                    else
                    {
                        sbResult.AppendLine(Resources.UploadersConfigForm_TestCustomUploader_Error__Result_is_empty_);
                    }

                    rtbResult.Text       = sbResult.ToString();
                    txtResponseText.Text = result.ResponseInfo?.ResponseText;

                    tcCustomUploader.SelectedTab = tpTest;
                }

                btnImageUploaderTest.Enabled    = btnTextUploaderTest.Enabled = btnFileUploaderTest.Enabled =
                    btnURLShortenerTest.Enabled = btnURLSharingServiceTest.Enabled = true;
            }
        }
示例#3
0
        private async Task TestCustomUploader(CustomUploaderDestinationType type, int index)
        {
            if (!Config.CustomUploadersList.IsValidIndex(index))
            {
                return;
            }

            btnImageUploaderTest.Enabled       = btnTextUploaderTest.Enabled = btnFileUploaderTest.Enabled =
                btnURLShortenerTest.Enabled    = btnURLSharingServiceTest.Enabled = false;
            lbCustomUploaderList.SelectedIndex = index;

            CustomUploaderItem item   = Config.CustomUploadersList[index];
            UploadResult       result = null;

            await Task.Run(() =>
            {
                try
                {
                    switch (type)
                    {
                    case CustomUploaderDestinationType.ImageUploader:
                        using (Stream stream = ShareXResources.Logo.GetStream())
                        {
                            CustomImageUploader imageUploader = new CustomImageUploader(item);
                            result = imageUploader.Upload(stream, "Test.png");
                            result.Errors.AddRange(imageUploader.Errors);
                        }
                        break;

                    case CustomUploaderDestinationType.TextUploader:
                        CustomTextUploader textUploader = new CustomTextUploader(item);
                        result = textUploader.UploadText("ShareX text upload test", "Test.txt");
                        result.Errors.AddRange(textUploader.Errors);
                        break;

                    case CustomUploaderDestinationType.FileUploader:
                        using (Stream stream = ShareXResources.Logo.GetStream())
                        {
                            CustomFileUploader fileUploader = new CustomFileUploader(item);
                            result = fileUploader.Upload(stream, "Test.png");
                            result.Errors.AddRange(fileUploader.Errors);
                        }
                        break;

                    case CustomUploaderDestinationType.URLShortener:
                        CustomURLShortener urlShortener = new CustomURLShortener(item);
                        result = urlShortener.ShortenURL(Links.URL_WEBSITE);
                        result.Errors.AddRange(urlShortener.Errors);
                        break;

                    case CustomUploaderDestinationType.URLSharingService:
                        CustomURLSharer urlSharer = new CustomURLSharer(item);
                        result = urlSharer.ShareURL(Links.URL_WEBSITE);
                        result.Errors.AddRange(urlSharer.Errors);
                        break;
                    }
                }
                catch (Exception e)
                {
                    result = new UploadResult();
                    result.Errors.Add(e.Message);
                }
            });

            if (!IsDisposed)
            {
                if (result != null)
                {
                    ResponseForm.ShowInstance(result);
                }

                btnImageUploaderTest.Enabled    = btnTextUploaderTest.Enabled = btnFileUploaderTest.Enabled =
                    btnURLShortenerTest.Enabled = btnURLSharingServiceTest.Enabled = true;
            }
        }
示例#4
0
        public UploadResult UploadText(Stream stream, string fileName)
        {
            TextUploader textUploader = null;

            switch (Info.TaskSettings.TextDestination)
            {
                case TextDestination.Pastebin:
                    PastebinSettings settings = Program.UploadersConfig.PastebinSettings;
                    if (string.IsNullOrEmpty(settings.TextFormat))
                    {
                        settings.TextFormat = Info.TaskSettings.AdvancedSettings.TextFormat;
                    }
                    textUploader = new Pastebin(APIKeys.PastebinKey, settings);
                    break;
                case TextDestination.Paste2:
                    textUploader = new Paste2(new Paste2Settings { TextFormat = Info.TaskSettings.AdvancedSettings.TextFormat });
                    break;
                case TextDestination.Slexy:
                    textUploader = new Slexy(new SlexySettings { TextFormat = Info.TaskSettings.AdvancedSettings.TextFormat });
                    break;
                case TextDestination.Pastee:
                    textUploader = new Pastee { Lexer = Info.TaskSettings.AdvancedSettings.TextFormat };
                    break;
                case TextDestination.Paste_ee:
                    textUploader = new Paste_ee(Program.UploadersConfig.Paste_eeUserAPIKey);
                    break;
                case TextDestination.Gist:
                    textUploader = Program.UploadersConfig.GistAnonymousLogin ? new Gist(Program.UploadersConfig.GistPublishPublic) :
                        new Gist(Program.UploadersConfig.GistPublishPublic, Program.UploadersConfig.GistOAuth2Info);
                    break;
                case TextDestination.Upaste:
                    textUploader = new Upaste(Program.UploadersConfig.UpasteUserKey)
                    {
                        IsPublic = Program.UploadersConfig.UpasteIsPublic
                    };
                    break;
                case TextDestination.Hastebin:
                    textUploader = new Hastebin()
                    {
                        CustomDomain = Program.UploadersConfig.HastebinCustomDomain,
                        SyntaxHighlighting = Program.UploadersConfig.HastebinSyntaxHighlighting
                    };
                    break;
                case TextDestination.OneTimeSecret:
                    textUploader = new OneTimeSecret()
                    {
                        API_KEY = Program.UploadersConfig.OneTimeSecretAPIKey,
                        API_USERNAME = Program.UploadersConfig.OneTimeSecretAPIUsername
                    };
                    break;
                case TextDestination.CustomTextUploader:
                    CustomUploaderItem customUploader = GetCustomUploader(Program.UploadersConfig.CustomTextUploaderSelected);
                    if (customUploader != null)
                    {
                        textUploader = new CustomTextUploader(customUploader);
                    }
                    break;
            }

            if (textUploader != null)
            {
                PrepareUploader(textUploader);
                return textUploader.UploadText(stream, fileName);
            }

            return null;
        }
        private void TestCustomUploader(CustomUploaderType type, CustomUploaderItem item)
        {
            btnCustomUploaderImageUploaderTest.Enabled = btnCustomUploaderTextUploaderTest.Enabled =
                btnCustomUploaderFileUploaderTest.Enabled = btnCustomUploaderURLShortenerTest.Enabled = false;

            UploadResult result = null;

            txtCustomUploaderLog.ResetText();

            TaskEx.Run(() =>
            {
                try
                {
                    switch (type)
                    {
                        case CustomUploaderType.Image:
                            using (Stream stream = ShareXYZResources.Logo.GetStream())
                            {
                                CustomImageUploader imageUploader = new CustomImageUploader(item);
                                result = imageUploader.Upload(stream, "Test.png");
                                result.Errors = imageUploader.Errors;
                            }
                            break;
                        case CustomUploaderType.Text:
                            CustomTextUploader textUploader = new CustomTextUploader(item);
                            result = textUploader.UploadText("ShareXYZ text upload test", "Test.txt");
                            result.Errors = textUploader.Errors;
                            break;
                        case CustomUploaderType.File:
                            using (Stream stream = ShareXYZResources.Logo.GetStream())
                            {
                                CustomFileUploader fileUploader = new CustomFileUploader(item);
                                result = fileUploader.Upload(stream, "Test.png");
                                result.Errors = fileUploader.Errors;
                            }
                            break;
                        case CustomUploaderType.URL:
                            CustomURLShortener urlShortener = new CustomURLShortener(item);
                            result = urlShortener.ShortenURL(Links.URL_WEBSITE);
                            result.Errors = urlShortener.Errors;
                            break;
                    }
                }
                catch (Exception e)
                {
                    result = new UploadResult();
                    result.Errors.Add(e.Message);
                }
            },
            () =>
            {
                if (!IsDisposed)
                {
                    if (result != null)
                    {
                        if ((type != CustomUploaderType.URL && !string.IsNullOrEmpty(result.URL)) || (type == CustomUploaderType.URL && !string.IsNullOrEmpty(result.ShortenedURL)))
                        {
                            txtCustomUploaderLog.AppendText("URL: " + result + Environment.NewLine);

                            if (!string.IsNullOrEmpty(result.ThumbnailURL))
                            {
                                txtCustomUploaderLog.AppendText("Thumbnail URL: " + result.ThumbnailURL + Environment.NewLine);
                            }

                            if (!string.IsNullOrEmpty(result.DeletionURL))
                            {
                                txtCustomUploaderLog.AppendText("Deletion URL: " + result.DeletionURL + Environment.NewLine);
                            }
                        }
                        else if (result.IsError)
                        {
                            txtCustomUploaderLog.AppendText(Resources.UploadersConfigForm_Error + ": " + result.ErrorsToString() + Environment.NewLine);
                        }
                        else
                        {
                            txtCustomUploaderLog.AppendText(Resources.UploadersConfigForm_TestCustomUploader_Error__Result_is_empty_ + Environment.NewLine);
                        }

                        txtCustomUploaderLog.ScrollToCaret();

                        btnCustomUploaderShowLastResponse.Tag = result.Response;
                        btnCustomUploaderShowLastResponse.Enabled = !string.IsNullOrEmpty(result.Response);
                    }

                    btnCustomUploaderImageUploaderTest.Enabled = btnCustomUploaderTextUploaderTest.Enabled =
                        btnCustomUploaderFileUploaderTest.Enabled = btnCustomUploaderURLShortenerTest.Enabled = true;
                }
            });
        }
示例#6
0
        private async Task TestCustomUploader(CustomUploaderDestinationType type, CustomUploaderItem item)
        {
            btnCustomUploaderImageUploaderTest.Enabled    = btnCustomUploaderTextUploaderTest.Enabled = btnCustomUploaderFileUploaderTest.Enabled =
                btnCustomUploaderURLShortenerTest.Enabled = btnCustomUploaderURLSharingServiceTest.Enabled = false;

            UploadResult result = null;

            txtCustomUploaderLog.ResetText();

            await Task.Run(() =>
            {
                try
                {
                    switch (type)
                    {
                    case CustomUploaderDestinationType.ImageUploader:
                        using (Stream stream = ShareXResources.Logo.GetStream())
                        {
                            CustomImageUploader imageUploader = new CustomImageUploader(item);
                            result        = imageUploader.Upload(stream, "Test.png");
                            result.Errors = imageUploader.Errors;
                        }
                        break;

                    case CustomUploaderDestinationType.TextUploader:
                        CustomTextUploader textUploader = new CustomTextUploader(item);
                        result        = textUploader.UploadText("ShareX text upload test", "Test.txt");
                        result.Errors = textUploader.Errors;
                        break;

                    case CustomUploaderDestinationType.FileUploader:
                        using (Stream stream = ShareXResources.Logo.GetStream())
                        {
                            CustomFileUploader fileUploader = new CustomFileUploader(item);
                            result        = fileUploader.Upload(stream, "Test.png");
                            result.Errors = fileUploader.Errors;
                        }
                        break;

                    case CustomUploaderDestinationType.URLShortener:
                        CustomURLShortener urlShortener = new CustomURLShortener(item);
                        result        = urlShortener.ShortenURL(Links.URL_WEBSITE);
                        result.Errors = urlShortener.Errors;
                        break;

                    case CustomUploaderDestinationType.URLSharingService:
                        CustomURLSharer urlSharer = new CustomURLSharer(item);
                        result        = urlSharer.ShareURL(Links.URL_WEBSITE);
                        result.Errors = urlSharer.Errors;
                        break;
                    }
                }
                catch (Exception e)
                {
                    result = new UploadResult();
                    result.Errors.Add(e.Message);
                }
            });

            if (!IsDisposed)
            {
                if (result != null)
                {
                    if (((type == CustomUploaderDestinationType.ImageUploader || type == CustomUploaderDestinationType.TextUploader ||
                          type == CustomUploaderDestinationType.FileUploader) && !string.IsNullOrEmpty(result.URL)) ||
                        (type == CustomUploaderDestinationType.URLShortener && !string.IsNullOrEmpty(result.ShortenedURL)) ||
                        (type == CustomUploaderDestinationType.URLSharingService && !result.IsError && !string.IsNullOrEmpty(result.URL)))
                    {
                        txtCustomUploaderLog.AppendText("URL: " + result + Environment.NewLine);

                        if (!string.IsNullOrEmpty(result.ThumbnailURL))
                        {
                            txtCustomUploaderLog.AppendText("Thumbnail URL: " + result.ThumbnailURL + Environment.NewLine);
                        }

                        if (!string.IsNullOrEmpty(result.DeletionURL))
                        {
                            txtCustomUploaderLog.AppendText("Deletion URL: " + result.DeletionURL + Environment.NewLine);
                        }
                    }
                    else if (result.IsError)
                    {
                        txtCustomUploaderLog.AppendText(Resources.UploadersConfigForm_Error + ": " + result.ErrorsToString() + Environment.NewLine);
                    }
                    else
                    {
                        txtCustomUploaderLog.AppendText(Resources.UploadersConfigForm_TestCustomUploader_Error__Result_is_empty_ + Environment.NewLine);
                    }

                    txtCustomUploaderLog.ScrollToCaret();

                    btnCustomUploaderShowLastResponse.Tag     = result.Response;
                    btnCustomUploaderShowLastResponse.Enabled = !string.IsNullOrEmpty(result.Response);
                }

                btnCustomUploaderImageUploaderTest.Enabled    = btnCustomUploaderTextUploaderTest.Enabled = btnCustomUploaderFileUploaderTest.Enabled =
                    btnCustomUploaderURLShortenerTest.Enabled = btnCustomUploaderURLSharingServiceTest.Enabled = true;
            }
        }