Better uploader service caching

This commit is contained in:
Jaex 2016-04-15 03:25:08 +03:00
parent cce83515be
commit e8aaa1576b
3 changed files with 20 additions and 39 deletions

View file

@ -24,41 +24,22 @@ You should have received a copy of the GNU General Public License
#endregion License Information (GPL v3)
using ShareX.HelpersLib;
using System.Collections.Generic;
using System.Linq;
namespace ShareX.UploadersLib
{
public static class UploaderFactory
{
private static readonly ImageUploaderService[] imageUploaderServices = Helpers.GetInstances<ImageUploaderService>();
private static readonly TextUploaderService[] textUploaderServices = Helpers.GetInstances<TextUploaderService>();
private static readonly FileUploaderService[] fileUploaderServices = Helpers.GetInstances<FileUploaderService>();
private static readonly URLShortenerService[] urlShortenerServices = Helpers.GetInstances<URLShortenerService>();
private static readonly URLSharingService[] urlSharingServices = Helpers.GetInstances<URLSharingService>();
public static Dictionary<ImageDestination, ImageUploaderService> ImageUploaderServices { get; } = CacheServices<ImageDestination, ImageUploaderService>();
public static Dictionary<TextDestination, TextUploaderService> TextUploaderServices { get; } = CacheServices<TextDestination, TextUploaderService>();
public static Dictionary<FileDestination, FileUploaderService> FileUploaderServices { get; } = CacheServices<FileDestination, FileUploaderService>();
public static Dictionary<UrlShortenerType, URLShortenerService> URLShortenerServices { get; } = CacheServices<UrlShortenerType, URLShortenerService>();
public static Dictionary<URLSharingServices, URLSharingService> URLSharingServices { get; } = CacheServices<URLSharingServices, URLSharingService>();
public static ImageUploaderService GetImageUploaderService(ImageDestination enumValue)
private static Dictionary<T, T2> CacheServices<T, T2>() where T2 : UploaderService<T>
{
return imageUploaderServices.First(x => x.EnumValue == enumValue);
}
public static TextUploaderService GetTextUploaderService(TextDestination enumValue)
{
return textUploaderServices.First(x => x.EnumValue == enumValue);
}
public static FileUploaderService GetFileUploaderService(FileDestination enumValue)
{
return fileUploaderServices.First(x => x.EnumValue == enumValue);
}
public static URLShortenerService GetURLShortenerService(UrlShortenerType enumValue)
{
return urlShortenerServices.First(x => x.EnumValue == enumValue);
}
public static URLSharingService GetURLSharingService(URLSharingServices enumValue)
{
return urlSharingServices.First(x => x.EnumValue == enumValue);
return Helpers.GetInstances<T2>().ToDictionary(x => x.EnumValue, x => x);
}
}
}

View file

@ -64,28 +64,28 @@ public static bool Validate<T>(int index, UploadersConfig config)
public static bool Validate(ImageDestination destination, UploadersConfig config)
{
if (destination == ImageDestination.FileUploader) return true;
return UploaderFactory.GetImageUploaderService(destination).CheckConfig(config);
return UploaderFactory.ImageUploaderServices[destination].CheckConfig(config);
}
public static bool Validate(TextDestination destination, UploadersConfig config)
{
if (destination == TextDestination.FileUploader) return true;
return UploaderFactory.GetTextUploaderService(destination).CheckConfig(config);
return UploaderFactory.TextUploaderServices[destination].CheckConfig(config);
}
public static bool Validate(FileDestination destination, UploadersConfig config)
{
return UploaderFactory.GetFileUploaderService(destination).CheckConfig(config);
return UploaderFactory.FileUploaderServices[destination].CheckConfig(config);
}
public static bool Validate(UrlShortenerType destination, UploadersConfig config)
{
return UploaderFactory.GetURLShortenerService(destination).CheckConfig(config);
return UploaderFactory.URLShortenerServices[destination].CheckConfig(config);
}
public static bool Validate(URLSharingServices destination, UploadersConfig config)
{
return UploaderFactory.GetURLSharingService(destination).CheckConfig(config);
return UploaderFactory.URLSharingServices[destination].CheckConfig(config);
}
}
}

View file

@ -75,7 +75,7 @@ public bool IsWorking
private Image tempImage;
private string tempText;
private ThreadWorker threadWorker;
private Uploader uploader;
private GenericUploader uploader;
private TaskReferenceHelper taskReferenceHelper;
private static string lastSaveAsFolder;
@ -851,7 +851,7 @@ public UploadResult UploadData(IGenericUploaderService service, Stream stream, s
return GetInvalidConfigResult(service);
}
GenericUploader uploader = service.CreateUploader(Program.UploadersConfig, taskReferenceHelper);
uploader = service.CreateUploader(Program.UploadersConfig, taskReferenceHelper);
if (uploader != null)
{
@ -871,28 +871,28 @@ public UploadResult UploadData(IGenericUploaderService service, Stream stream, s
public UploadResult UploadImage(Stream stream, string fileName)
{
ImageUploaderService service = UploaderFactory.GetImageUploaderService(Info.TaskSettings.ImageDestination);
ImageUploaderService service = UploaderFactory.ImageUploaderServices[Info.TaskSettings.ImageDestination];
return UploadData(service, stream, fileName);
}
public UploadResult UploadText(Stream stream, string fileName)
{
TextUploaderService service = UploaderFactory.GetTextUploaderService(Info.TaskSettings.TextDestination);
TextUploaderService service = UploaderFactory.TextUploaderServices[Info.TaskSettings.TextDestination];
return UploadData(service, stream, fileName);
}
public UploadResult UploadFile(Stream stream, string fileName)
{
FileUploaderService service = UploaderFactory.GetFileUploaderService(Info.TaskSettings.GetFileDestinationByDataType(Info.DataType));
FileUploaderService service = UploaderFactory.FileUploaderServices[Info.TaskSettings.GetFileDestinationByDataType(Info.DataType)];
return UploadData(service, stream, fileName);
}
public UploadResult ShortenURL(string url)
{
URLShortenerService service = UploaderFactory.GetURLShortenerService(Info.TaskSettings.URLShortenerDestination);
URLShortenerService service = UploaderFactory.URLShortenerServices[Info.TaskSettings.URLShortenerDestination];
if (!service.CheckConfig(Program.UploadersConfig))
{
@ -913,7 +913,7 @@ public UploadResult ShareURL(string url)
{
if (!string.IsNullOrEmpty(url))
{
URLSharingService service = UploaderFactory.GetURLSharingService(Info.TaskSettings.URLSharingServiceDestination);
URLSharingService service = UploaderFactory.URLSharingServices[Info.TaskSettings.URLSharingServiceDestination];
if (!service.CheckConfig(Program.UploadersConfig))
{