Added TextUploaderService, using it on Pastebin for now

This commit is contained in:
Jaex 2016-03-22 00:51:37 +02:00
parent f0e0b7e463
commit 8c58369192
8 changed files with 175 additions and 70 deletions

View file

@ -25,12 +25,10 @@ You should have received a copy of the GNU General Public License
namespace ShareX.UploadersLib
{
public abstract class ImageUploaderService
public abstract class ImageUploaderService : UploaderService
{
public abstract ImageDestination EnumValue { get; }
public abstract ImageUploader CreateUploader(UploadersConfig uploadersConfig);
public abstract bool CheckConfig(UploadersConfig uploadersConfig);
}
}

View file

@ -207,7 +207,8 @@
<Compile Include="FileUploaders\GfycatUploader.cs" />
<Compile Include="HelperClasses\OAuth\IOAuthBase.cs" />
<Compile Include="HelperClasses\SSLBypassHelper.cs" />
<Compile Include="ImageUploaderFactory.cs" />
<Compile Include="TextUploaderService.cs" />
<Compile Include="UploaderFactory.cs" />
<Compile Include="ImageUploaders\Chevereto.cs" />
<Compile Include="ImageUploaders\CheveretoUploader.cs" />
<Compile Include="ImageUploaders\ImglandUploader.cs" />
@ -266,6 +267,7 @@
<Compile Include="TextUploaders\CustomTextUploader.cs" />
<Compile Include="TextUploaders\Gist.cs" />
<Compile Include="TextUploaders\Paste_ee.cs" />
<Compile Include="UploaderService.cs" />
<Compile Include="UploadResult.cs" />
<Compile Include="Forms\TwitterTweetForm.cs">
<SubType>Form</SubType>

View file

@ -23,18 +23,12 @@ You should have received a copy of the GNU General Public License
#endregion License Information (GPL v3)
using ShareX.HelpersLib;
using System.Linq;
namespace ShareX.UploadersLib
{
public class ImageUploaderFactory
public abstract class TextUploaderService : UploaderService
{
private static readonly ImageUploaderService[] services = Helpers.GetInstances<ImageUploaderService>();
public abstract TextDestination EnumValue { get; }
public static ImageUploaderService GetServiceByEnum(ImageDestination enumValue)
{
return services.FirstOrDefault(x => x.EnumValue == enumValue);
}
public abstract TextUploader CreateUploader(UploadersConfig uploadersConfig);
}
}

View file

@ -30,6 +30,25 @@ You should have received a copy of the GNU General Public License
namespace ShareX.UploadersLib.TextUploaders
{
public class PastebinTextUploaderService : TextUploaderService
{
public override TextDestination EnumValue { get; } = TextDestination.Pastebin;
public override TextUploader CreateUploader(UploadersConfig uploadersConfig)
{
PastebinSettings settings = uploadersConfig.PastebinSettings;
if (string.IsNullOrEmpty(settings.TextFormat))
{
settings.TextFormat = uploadersConfig.TextFormat;
}
return new Pastebin(APIKeys.PastebinKey, settings);
}
public override bool CheckConfig(UploadersConfig uploadersConfig) => true;
}
public sealed class Pastebin : TextUploader
{
private string APIKey;

View file

@ -0,0 +1,46 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2016 ShareX Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using ShareX.HelpersLib;
using System.Linq;
namespace ShareX.UploadersLib
{
public class UploaderFactory
{
private static readonly ImageUploaderService[] imageUploaderServices = Helpers.GetInstances<ImageUploaderService>();
private static readonly TextUploaderService[] textUploaderServices = Helpers.GetInstances<TextUploaderService>();
public static ImageUploaderService GetImageUploaderServiceByEnum(ImageDestination enumValue)
{
return imageUploaderServices.FirstOrDefault(x => x.EnumValue == enumValue);
}
public static TextUploaderService GetTextUploaderServiceByEnum(TextDestination enumValue)
{
return textUploaderServices.FirstOrDefault(x => x.EnumValue == enumValue);
}
}
}

View file

@ -0,0 +1,34 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2016 ShareX Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
namespace ShareX.UploadersLib
{
public abstract class UploaderService
{
// This function is used in destinations menu to show invalid uploaders as disabled.
// For example if uploader is only allowed to upload with account and if account configuration is not valid then this function should return false.
public abstract bool CheckConfig(UploadersConfig uploadersConfig);
}
}

View file

@ -94,6 +94,8 @@ public class UploadersConfig : SettingsBase<UploadersConfig>
#region Text uploaders
public string TextFormat = "";
// Pastebin
public PastebinSettings PastebinSettings = new PastebinSettings();
@ -387,7 +389,7 @@ public bool IsValid<T>(int index)
public bool IsValid(ImageDestination destination)
{
ImageUploaderService service = ImageUploaderFactory.GetServiceByEnum(destination);
ImageUploaderService service = UploaderFactory.GetImageUploaderServiceByEnum(destination);
if (service != null)
{
@ -419,6 +421,13 @@ public bool IsValid(ImageDestination destination)
public bool IsValid(TextDestination destination)
{
TextUploaderService service = UploaderFactory.GetTextUploaderServiceByEnum(destination);
if (service != null)
{
return service.CheckConfig(this);
}
switch (destination)
{
case TextDestination.CustomTextUploader:

View file

@ -808,7 +808,7 @@ public UploadResult UploadImage(Stream stream, string fileName)
{
ImageUploader imageUploader = null;
ImageUploaderService service = ImageUploaderFactory.GetServiceByEnum(Info.TaskSettings.ImageDestination);
ImageUploaderService service = UploaderFactory.GetImageUploaderServiceByEnum(Info.TaskSettings.ImageDestination);
if (service != null)
{
@ -895,62 +895,65 @@ public UploadResult UploadText(Stream stream, string fileName)
{
TextUploader textUploader = null;
switch (Info.TaskSettings.TextDestination)
Program.UploadersConfig.TextFormat = Info.TaskSettings.AdvancedSettings.TextFormat;
TextUploaderService service = UploaderFactory.GetTextUploaderServiceByEnum(Info.TaskSettings.TextDestination);
if (service != null)
{
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 = new Gist(Program.UploadersConfig.GistAnonymousLogin ? null : Program.UploadersConfig.GistOAuth2Info)
{
PublicUpload = Program.UploadersConfig.GistPublishPublic,
RawURL = Program.UploadersConfig.GistRawURL
};
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;
textUploader = service.CreateUploader(Program.UploadersConfig);
}
else
{
switch (Info.TaskSettings.TextDestination)
{
case TextDestination.Paste2:
textUploader = new Paste2(new Paste2Settings { TextFormat = Program.UploadersConfig.TextFormat });
break;
case TextDestination.Slexy:
textUploader = new Slexy(new SlexySettings { TextFormat = Program.UploadersConfig.TextFormat });
break;
case TextDestination.Pastee:
textUploader = new Pastee { Lexer = Program.UploadersConfig.TextFormat };
break;
case TextDestination.Paste_ee:
textUploader = new Paste_ee(Program.UploadersConfig.Paste_eeUserAPIKey);
break;
case TextDestination.Gist:
textUploader = new Gist(Program.UploadersConfig.GistAnonymousLogin ? null : Program.UploadersConfig.GistOAuth2Info)
{
PublicUpload = Program.UploadersConfig.GistPublishPublic,
RawURL = Program.UploadersConfig.GistRawURL
};
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)