Custom uploader request URL auto add http://

This commit is contained in:
Jaex 2015-06-28 22:30:55 +03:00
parent 721966c2f0
commit 21a6d850b1
6 changed files with 46 additions and 25 deletions

View file

@ -304,6 +304,11 @@ public static List<string> 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)

View file

@ -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)
{

View file

@ -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<string, string> 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<string, string> GetArguments(string input = null)
{
Dictionary<string, string> arguments = new Dictionary<string, string>();

View file

@ -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)
{

View file

@ -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<string, string> args = customUploader.ParseArguments(text);
Dictionary<string, string> 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);

View file

@ -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<string, string> args = customUploader.ParseArguments(url);
Dictionary<string, string> 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);