Support name patterns in custom uploader headers too

This commit is contained in:
Jaex 2018-02-03 22:21:24 +03:00
parent 3c202bd615
commit d0c2275207
8 changed files with 100 additions and 32 deletions

View file

@ -85,8 +85,10 @@ public override UploadResult Upload(Stream stream, string fileName)
throw new Exception("'Request type' must be 'POST' when using custom file uploader.");
}
CustomUploaderArgumentInput input = new CustomUploaderArgumentInput(fileName, "");
UploadResult result = SendRequestFile(customUploader.GetRequestURL(), stream, fileName, customUploader.GetFileFormName(),
customUploader.GetArguments(fileName), customUploader.GetHeaders(), responseType: customUploader.ResponseType);
customUploader.GetArguments(input), customUploader.GetHeaders(input), responseType: customUploader.ResponseType);
if (result.IsSuccess)
{

View file

@ -0,0 +1,58 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2018 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;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ShareX.UploadersLib
{
public class CustomUploaderArgumentInput
{
public string Filename { get; set; }
public string Input { get; set; }
public CustomUploaderArgumentInput(string filename, string input)
{
Filename = filename;
Input = input;
}
public string Parse(string arg)
{
arg = NameParser.Parse(NameParserType.Text, arg);
arg = arg.BatchReplace(new Dictionary<string, string>()
{
{ "$filename$", Filename },
{ "$input$", Input }
});
return arg;
}
}
}

View file

@ -151,7 +151,7 @@ public string GetFileFormName()
return FileFormName;
}
public Dictionary<string, string> GetArguments(string filename = "", string input = "")
public Dictionary<string, string> GetArguments(CustomUploaderArgumentInput input)
{
Dictionary<string, string> arguments = new Dictionary<string, string>();
@ -159,24 +159,14 @@ public Dictionary<string, string> GetArguments(string filename = "", string inpu
{
foreach (KeyValuePair<string, string> arg in Arguments)
{
string value = arg.Value;
value = NameParser.Parse(NameParserType.Text, value);
value = value.BatchReplace(new Dictionary<string, string>()
{
{ "$filename$", filename },
{ "$input$", input }
});
arguments.Add(arg.Key, value);
arguments.Add(arg.Key, input.Parse(arg.Value));
}
}
return arguments;
}
public NameValueCollection GetHeaders()
public NameValueCollection GetHeaders(CustomUploaderArgumentInput input)
{
if (Headers != null && Headers.Count > 0)
{
@ -184,7 +174,7 @@ public NameValueCollection GetHeaders()
foreach (KeyValuePair<string, string> header in Headers)
{
collection.Add(header.Key, header.Value);
collection.Add(header.Key, input.Parse(header.Value));
}
return collection;

View file

@ -82,8 +82,10 @@ public override UploadResult Upload(Stream stream, string fileName)
throw new Exception("'Request type' must be 'POST' when using custom image uploader.");
}
CustomUploaderArgumentInput input = new CustomUploaderArgumentInput(fileName, "");
UploadResult result = SendRequestFile(customUploader.GetRequestURL(), stream, fileName, customUploader.GetFileFormName(),
customUploader.GetArguments(fileName), customUploader.GetHeaders(), responseType: customUploader.ResponseType);
customUploader.GetArguments(input), customUploader.GetHeaders(input), responseType: customUploader.ResponseType);
if (result.IsSuccess)
{

View file

@ -233,6 +233,7 @@
</Compile>
<Compile Include="FileUploaders\Pushbullet.cs" />
<Compile Include="FileUploaders\GfycatUploader.cs" />
<Compile Include="Helpers\CustomUploaderArgumentInput.cs" />
<Compile Include="Helpers\OAuth\IOAuthBase.cs" />
<Compile Include="Helpers\SSLBypassHelper.cs" />
<Compile Include="BaseServices\URLSharingService.cs" />

View file

@ -77,20 +77,25 @@ public override UploadResult ShareURL(string url)
if (customUploader.RequestType == CustomUploaderRequestType.POST && !string.IsNullOrEmpty(customUploader.FileFormName))
throw new Exception("'File form name' cannot be used with custom URL sharing service.");
if (customUploader.Arguments == null || !customUploader.Arguments.Any(x => x.Value.Contains("$input$")))
throw new Exception("Atleast one '$input$' required for argument value.");
if ((customUploader.Arguments == null || !customUploader.Arguments.Any(x => x.Value.Contains("$input$"))) &&
(customUploader.Headers == null || !customUploader.Headers.Any(x => x.Value.Contains("$input$"))))
throw new Exception("Atleast one '$input$' required for argument or header value.");
UploadResult result = new UploadResult { URL = url, IsURLExpected = false };
Dictionary<string, string> args = customUploader.GetArguments("", url);
CustomUploaderArgumentInput input = new CustomUploaderArgumentInput("", url);
Dictionary<string, string> args = customUploader.GetArguments(input);
if (customUploader.RequestType == CustomUploaderRequestType.POST)
{
result.Response = SendRequestMultiPart(customUploader.GetRequestURL(), args, customUploader.GetHeaders(), responseType: customUploader.ResponseType);
result.Response = SendRequestMultiPart(customUploader.GetRequestURL(), args, customUploader.GetHeaders(input),
responseType: customUploader.ResponseType);
}
else
{
result.Response = SendRequest(customUploader.GetHttpMethod(), customUploader.GetRequestURL(), args, customUploader.GetHeaders(), responseType: customUploader.ResponseType);
result.Response = SendRequest(customUploader.GetHttpMethod(), customUploader.GetRequestURL(), args, customUploader.GetHeaders(input),
responseType: customUploader.ResponseType);
}
return result;

View file

@ -85,29 +85,34 @@ public override UploadResult UploadText(string text, string fileName)
string requestURL = customUploader.GetRequestURL();
if ((customUploader.RequestType != CustomUploaderRequestType.POST || string.IsNullOrEmpty(customUploader.FileFormName)) &&
(customUploader.Arguments == null || !customUploader.Arguments.Any(x => x.Value.Contains("$input$"))))
throw new Exception("Atleast one '$input$' required for argument value.");
((customUploader.Arguments == null || !customUploader.Arguments.Any(x => x.Value.Contains("$input$"))) &&
(customUploader.Headers == null || !customUploader.Headers.Any(x => x.Value.Contains("$input$")))))
throw new Exception("Atleast one '$input$' required for argument or header value.");
Dictionary<string, string> args = customUploader.GetArguments(fileName, text);
CustomUploaderArgumentInput input = new CustomUploaderArgumentInput(fileName, text);
Dictionary<string, string> args = customUploader.GetArguments(input);
if (customUploader.RequestType == CustomUploaderRequestType.POST)
{
if (string.IsNullOrEmpty(customUploader.FileFormName))
{
result.Response = SendRequestMultiPart(requestURL, args, customUploader.GetHeaders(), responseType: customUploader.ResponseType);
result.Response = SendRequestMultiPart(requestURL, args, customUploader.GetHeaders(input), responseType: customUploader.ResponseType);
}
else
{
byte[] byteArray = Encoding.UTF8.GetBytes(text);
using (MemoryStream stream = new MemoryStream(byteArray))
{
result = SendRequestFile(requestURL, stream, fileName, customUploader.GetFileFormName(), args, customUploader.GetHeaders(), responseType: customUploader.ResponseType);
result = SendRequestFile(requestURL, stream, fileName, customUploader.GetFileFormName(), args, customUploader.GetHeaders(input),
responseType: customUploader.ResponseType);
}
}
}
else
{
result.Response = SendRequest(customUploader.GetHttpMethod(), requestURL, args, customUploader.GetHeaders(), responseType: customUploader.ResponseType);
result.Response = SendRequest(customUploader.GetHttpMethod(), requestURL, args, customUploader.GetHeaders(input),
responseType: customUploader.ResponseType);
}
try

View file

@ -78,20 +78,25 @@ 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 (customUploader.Arguments == null || !customUploader.Arguments.Any(x => x.Value.Contains("$input$")))
throw new Exception("Atleast one '$input$' required for argument value.");
if ((customUploader.Arguments == null || !customUploader.Arguments.Any(x => x.Value.Contains("$input$"))) &&
(customUploader.Headers == null || !customUploader.Headers.Any(x => x.Value.Contains("$input$"))))
throw new Exception("Atleast one '$input$' required for argument or header value.");
UploadResult result = new UploadResult { URL = url };
Dictionary<string, string> args = customUploader.GetArguments("", url);
CustomUploaderArgumentInput input = new CustomUploaderArgumentInput("", url);
Dictionary<string, string> args = customUploader.GetArguments(input);
if (customUploader.RequestType == CustomUploaderRequestType.POST)
{
result.Response = SendRequestMultiPart(customUploader.GetRequestURL(), args, customUploader.GetHeaders(), responseType: customUploader.ResponseType);
result.Response = SendRequestMultiPart(customUploader.GetRequestURL(), args, customUploader.GetHeaders(input),
responseType: customUploader.ResponseType);
}
else
{
result.Response = SendRequest(customUploader.GetHttpMethod(), customUploader.GetRequestURL(), args, customUploader.GetHeaders(), responseType: customUploader.ResponseType);
result.Response = SendRequest(customUploader.GetHttpMethod(), customUploader.GetRequestURL(), args, customUploader.GetHeaders(input),
responseType: customUploader.ResponseType);
}
try