diff --git a/ShareX.HelpersLib/Helpers/URLHelpers.cs b/ShareX.HelpersLib/Helpers/URLHelpers.cs index d0d5fbb41..3935f9b89 100644 --- a/ShareX.HelpersLib/Helpers/URLHelpers.cs +++ b/ShareX.HelpersLib/Helpers/URLHelpers.cs @@ -304,6 +304,11 @@ public static List GetPaths(string path) private static readonly string[] URLPrefixes = new string[] { "http://", "https://", "ftp://", "ftps://", "file://" }; + public static bool HasPrefix(string url) + { + return URLPrefixes.Any(x => url.StartsWith(x, StringComparison.InvariantCultureIgnoreCase)); + } + public static string FixPrefix(string url) { if (!HasPrefix(url)) @@ -324,11 +329,6 @@ public static string ForceHTTPS(string url) return url; } - public static bool HasPrefix(string url) - { - return URLPrefixes.Any(x => url.StartsWith(x, StringComparison.InvariantCultureIgnoreCase)); - } - public static string RemovePrefixes(string url) { foreach (string prefix in URLPrefixes) diff --git a/ShareX.UploadersLib/FileUploaders/CustomFileUploader.cs b/ShareX.UploadersLib/FileUploaders/CustomFileUploader.cs index eec226129..201b9c852 100644 --- a/ShareX.UploadersLib/FileUploaders/CustomFileUploader.cs +++ b/ShareX.UploadersLib/FileUploaders/CustomFileUploader.cs @@ -39,11 +39,12 @@ public CustomFileUploader(CustomUploaderItem customUploaderItem) public override UploadResult Upload(Stream stream, string fileName) { - if (customUploader.RequestType != CustomUploaderRequestType.POST) throw new Exception("'Request type' must be 'POST' when using custom file uploader."); - if (string.IsNullOrEmpty(customUploader.FileFormName)) throw new Exception("'File form name' must be not empty when using custom file uploader."); - if (string.IsNullOrEmpty(customUploader.RequestURL)) throw new Exception("'Request URL' must be not empty."); + if (customUploader.RequestType != CustomUploaderRequestType.POST) + { + throw new Exception("'Request type' must be 'POST' when using custom file uploader."); + } - UploadResult result = UploadData(stream, customUploader.RequestURL, fileName, customUploader.FileFormName, customUploader.ParseArguments(), responseType: customUploader.ResponseType); + UploadResult result = UploadData(stream, customUploader.GetRequestURL(), fileName, customUploader.GetFileFormName(), customUploader.GetArguments(), responseType: customUploader.ResponseType); if (result.IsSuccess) { diff --git a/ShareX.UploadersLib/HelperClasses/CustomUploaderItem.cs b/ShareX.UploadersLib/HelperClasses/CustomUploaderItem.cs index a13934486..61d664463 100644 --- a/ShareX.UploadersLib/HelperClasses/CustomUploaderItem.cs +++ b/ShareX.UploadersLib/HelperClasses/CustomUploaderItem.cs @@ -24,6 +24,7 @@ #endregion License Information (GPL v3) using ShareX.HelpersLib; +using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; @@ -80,7 +81,27 @@ public HttpMethod GetHttpMethod() } } - public Dictionary ParseArguments(string input = null) + public string GetRequestURL() + { + if (string.IsNullOrEmpty(RequestURL)) + { + throw new Exception("'Request URL' must be not empty."); + } + + return URLHelpers.FixPrefix(RequestURL); + } + + public string GetFileFormName() + { + if (string.IsNullOrEmpty(FileFormName)) + { + throw new Exception("'File form name' must be not empty."); + } + + return FileFormName; + } + + public Dictionary GetArguments(string input = null) { Dictionary arguments = new Dictionary(); diff --git a/ShareX.UploadersLib/ImageUploaders/CustomImageUploader.cs b/ShareX.UploadersLib/ImageUploaders/CustomImageUploader.cs index f7a3cbf54..a3fe62070 100644 --- a/ShareX.UploadersLib/ImageUploaders/CustomImageUploader.cs +++ b/ShareX.UploadersLib/ImageUploaders/CustomImageUploader.cs @@ -39,11 +39,12 @@ public CustomImageUploader(CustomUploaderItem customUploaderItem) public override UploadResult Upload(Stream stream, string fileName) { - if (customUploader.RequestType != CustomUploaderRequestType.POST) throw new Exception("'Request type' must be 'POST' when using custom image uploader."); - if (string.IsNullOrEmpty(customUploader.FileFormName)) throw new Exception("'File form name' must be not empty when using custom image uploader."); - if (string.IsNullOrEmpty(customUploader.RequestURL)) throw new Exception("'Request URL' must be not empty."); + if (customUploader.RequestType != CustomUploaderRequestType.POST) + { + throw new Exception("'Request type' must be 'POST' when using custom image uploader."); + } - UploadResult result = UploadData(stream, customUploader.RequestURL, fileName, customUploader.FileFormName, customUploader.ParseArguments(), responseType: customUploader.ResponseType); + UploadResult result = UploadData(stream, customUploader.GetRequestURL(), fileName, customUploader.GetFileFormName(), customUploader.GetArguments(), responseType: customUploader.ResponseType); if (result.IsSuccess) { diff --git a/ShareX.UploadersLib/TextUploaders/CustomTextUploader.cs b/ShareX.UploadersLib/TextUploaders/CustomTextUploader.cs index b09b7b280..f41134c36 100644 --- a/ShareX.UploadersLib/TextUploaders/CustomTextUploader.cs +++ b/ShareX.UploadersLib/TextUploaders/CustomTextUploader.cs @@ -42,34 +42,34 @@ public CustomTextUploader(CustomUploaderItem customUploaderItem) public override UploadResult UploadText(string text, string fileName) { - if (string.IsNullOrEmpty(customUploader.RequestURL)) throw new Exception("'Request URL' must be not empty."); + UploadResult result = new UploadResult(); + + string requestURL = customUploader.GetRequestURL(); if ((customUploader.RequestType != CustomUploaderRequestType.POST || string.IsNullOrEmpty(customUploader.FileFormName)) && (customUploader.Arguments == null || !customUploader.Arguments.Any(x => x.Value.Contains("$input$") || x.Value.Contains("%input")))) throw new Exception("Atleast one '$input$' required for argument value."); - UploadResult result = new UploadResult(); - - Dictionary args = customUploader.ParseArguments(text); + Dictionary args = customUploader.GetArguments(text); if (customUploader.RequestType == CustomUploaderRequestType.POST) { if (string.IsNullOrEmpty(customUploader.FileFormName)) { - result.Response = SendRequest(HttpMethod.POST, customUploader.RequestURL, args, responseType: customUploader.ResponseType); + result.Response = SendRequest(HttpMethod.POST, requestURL, args, responseType: customUploader.ResponseType); } else { byte[] byteArray = Encoding.UTF8.GetBytes(text); using (MemoryStream stream = new MemoryStream(byteArray)) { - result = UploadData(stream, customUploader.RequestURL, fileName, customUploader.FileFormName, args, responseType: customUploader.ResponseType); + result = UploadData(stream, requestURL, fileName, customUploader.GetFileFormName(), args, responseType: customUploader.ResponseType); } } } else { - result.Response = SendRequest(customUploader.GetHttpMethod(), customUploader.RequestURL, args, responseType: customUploader.ResponseType); + result.Response = SendRequest(customUploader.GetHttpMethod(), requestURL, args, responseType: customUploader.ResponseType); } customUploader.ParseResponse(result); diff --git a/ShareX.UploadersLib/URLShorteners/CustomURLShortener.cs b/ShareX.UploadersLib/URLShorteners/CustomURLShortener.cs index 51d9d8e08..aa5294e43 100644 --- a/ShareX.UploadersLib/URLShorteners/CustomURLShortener.cs +++ b/ShareX.UploadersLib/URLShorteners/CustomURLShortener.cs @@ -43,16 +43,14 @@ public override UploadResult ShortenURL(string url) if (customUploader.RequestType == CustomUploaderRequestType.POST && !string.IsNullOrEmpty(customUploader.FileFormName)) throw new Exception("'File form name' cannot be used with custom URL shortener."); - if (string.IsNullOrEmpty(customUploader.RequestURL)) throw new Exception("'Request URL' must be not empty."); - if (customUploader.Arguments == null || !customUploader.Arguments.Any(x => x.Value.Contains("$input$") || x.Value.Contains("%input"))) throw new Exception("Atleast one '$input$' required for argument value."); UploadResult result = new UploadResult { URL = url }; - Dictionary args = customUploader.ParseArguments(url); + Dictionary args = customUploader.GetArguments(url); - result.Response = SendRequest(customUploader.GetHttpMethod(), customUploader.RequestURL, args, responseType: customUploader.ResponseType); + result.Response = SendRequest(customUploader.GetHttpMethod(), customUploader.GetRequestURL(), args, responseType: customUploader.ResponseType); customUploader.ParseResponse(result, true);