ShareX/ShareX.UploadersLib/Helpers/CustomUploaderItem.cs

421 lines
13 KiB
C#
Raw Normal View History

#region License Information (GPL v3)
2013-11-03 23:53:49 +13:00
/*
ShareX - A program that allows you to take screenshots and share any file type
2018-01-02 03:59:14 +13:00
Copyright (c) 2007-2018 ShareX Team
2013-11-03 23:53:49 +13:00
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 Newtonsoft.Json.Linq;
2014-12-11 09:25:20 +13:00
using ShareX.HelpersLib;
2017-10-29 09:40:50 +13:00
using ShareX.UploadersLib.Properties;
using System;
2013-11-03 23:53:49 +13:00
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.IO;
2013-11-03 23:53:49 +13:00
using System.Text;
using System.Text.RegularExpressions;
using System.Xml.XPath;
2013-11-03 23:53:49 +13:00
2014-12-11 09:25:20 +13:00
namespace ShareX.UploadersLib
2013-11-03 23:53:49 +13:00
{
public class CustomUploaderItem
{
[DefaultValue("")]
2017-09-03 12:13:07 +12:00
public string Name { get; set; }
public bool ShouldSerializeName() => !string.IsNullOrEmpty(Name) && Name != URLHelpers.GetHostName(RequestURL);
[DefaultValue(CustomUploaderDestinationType.None)]
public CustomUploaderDestinationType DestinationType { get; set; }
[DefaultValue(CustomUploaderRequestType.POST)]
2013-11-03 23:53:49 +13:00
public CustomUploaderRequestType RequestType { get; set; }
[DefaultValue("")]
2013-11-03 23:53:49 +13:00
public string RequestURL { get; set; }
[DefaultValue("")]
2013-11-03 23:53:49 +13:00
public string FileFormName { get; set; }
[DefaultValue(null)]
2017-09-03 12:13:07 +12:00
public Dictionary<string, string> Arguments { get; set; }
public bool ShouldSerializeArguments() => Arguments != null && Arguments.Count > 0;
[DefaultValue(null)]
2017-09-03 12:13:07 +12:00
public Dictionary<string, string> Headers { get; set; }
public bool ShouldSerializeHeaders() => Headers != null && Headers.Count > 0;
[DefaultValue(ResponseType.Text)]
2013-11-03 23:53:49 +13:00
public ResponseType ResponseType { get; set; }
[DefaultValue(null)]
2017-09-03 12:13:07 +12:00
public List<string> RegexList { get; set; }
public bool ShouldSerializeRegexList() => RegexList != null && RegexList.Count > 0;
[DefaultValue("")]
2013-11-03 23:53:49 +13:00
public string URL { get; set; }
[DefaultValue("")]
2013-11-03 23:53:49 +13:00
public string ThumbnailURL { get; set; }
[DefaultValue("")]
2013-11-03 23:53:49 +13:00
public string DeletionURL { get; set; }
private string response;
2013-11-03 23:53:49 +13:00
private List<Match> regexResult;
public CustomUploaderItem()
{
}
public override string ToString()
{
if (!string.IsNullOrEmpty(Name))
{
return Name;
}
string name = URLHelpers.GetHostName(RequestURL);
2013-11-03 23:53:49 +13:00
if (!string.IsNullOrEmpty(name))
{
return name;
}
return "Name";
}
public string GetFileName()
{
return ToString() + ".sxcu";
}
public HttpMethod GetHttpMethod()
{
switch (RequestType)
{
default:
case CustomUploaderRequestType.POST:
return HttpMethod.POST;
case CustomUploaderRequestType.GET:
return HttpMethod.GET;
case CustomUploaderRequestType.PUT:
return HttpMethod.PUT;
case CustomUploaderRequestType.PATCH:
return HttpMethod.PATCH;
case CustomUploaderRequestType.DELETE:
return HttpMethod.DELETE;
}
}
public string GetRequestURL()
{
if (string.IsNullOrEmpty(RequestURL))
{
2017-10-29 09:40:50 +13:00
throw new Exception(Resources.CustomUploaderItem_GetRequestURL_RequestURLMustBeConfigured);
}
string url = ParseURL(RequestURL, false);
return URLHelpers.FixPrefix(url);
}
public string GetFileFormName()
{
if (string.IsNullOrEmpty(FileFormName))
{
2017-10-29 09:40:50 +13:00
throw new Exception(Resources.CustomUploaderItem_GetFileFormName_FileFormNameMustBeConfigured);
}
return FileFormName;
}
public Dictionary<string, string> GetArguments(CustomUploaderArgumentInput input)
2013-11-03 23:53:49 +13:00
{
Dictionary<string, string> arguments = new Dictionary<string, string>();
2015-12-23 11:21:19 +13:00
if (Arguments != null)
2013-11-03 23:53:49 +13:00
{
2015-12-23 11:21:19 +13:00
foreach (KeyValuePair<string, string> arg in Arguments)
{
arguments.Add(arg.Key, input.Parse(arg.Value));
2015-12-23 11:21:19 +13:00
}
2013-11-03 23:53:49 +13:00
}
return arguments;
}
public NameValueCollection GetHeaders(CustomUploaderArgumentInput input)
{
if (Headers != null && Headers.Count > 0)
{
NameValueCollection collection = new NameValueCollection();
foreach (KeyValuePair<string, string> header in Headers)
{
collection.Add(header.Key, input.Parse(header.Value));
}
return collection;
}
return null;
}
2013-12-08 05:08:38 +13:00
public void ParseResponse(UploadResult result, bool isShortenedURL = false)
2013-11-03 23:53:49 +13:00
{
2013-12-08 05:08:38 +13:00
if (result != null && !string.IsNullOrEmpty(result.Response))
2013-11-03 23:53:49 +13:00
{
response = result.Response;
ParseRegexList();
2013-11-03 23:53:49 +13:00
2013-12-08 05:08:38 +13:00
string url;
2013-11-03 23:53:49 +13:00
2013-12-08 05:08:38 +13:00
if (!string.IsNullOrEmpty(URL))
{
url = ParseURL(URL, true);
2013-12-08 05:08:38 +13:00
}
else
{
url = response;
2013-12-08 05:08:38 +13:00
}
if (isShortenedURL)
{
result.ShortenedURL = url;
}
else
{
result.URL = url;
}
result.ThumbnailURL = ParseURL(ThumbnailURL, true);
result.DeletionURL = ParseURL(DeletionURL, true);
2013-11-03 23:53:49 +13:00
}
2013-12-08 05:08:38 +13:00
}
2013-11-03 23:53:49 +13:00
private void ParseRegexList()
2013-12-08 05:08:38 +13:00
{
regexResult = new List<Match>();
2013-12-08 05:08:38 +13:00
if (RegexList != null)
2013-11-03 23:53:49 +13:00
{
2013-12-08 05:08:38 +13:00
foreach (string regex in RegexList)
{
regexResult.Add(Regex.Match(response, regex));
2013-12-08 05:08:38 +13:00
}
2013-11-03 23:53:49 +13:00
}
}
public string ParseURL(string url, bool usingResponse)
2013-11-03 23:53:49 +13:00
{
2013-12-08 05:08:38 +13:00
if (string.IsNullOrEmpty(url))
{
2016-05-25 06:15:45 +12:00
return "";
2013-12-08 05:08:38 +13:00
}
2013-11-03 23:53:49 +13:00
StringBuilder result = new StringBuilder();
bool syntaxStart = false;
int syntaxStartIndex = 0;
2018-01-02 08:49:35 +13:00
bool escape = false;
2013-11-03 23:53:49 +13:00
for (int i = 0; i < url.Length; i++)
{
2018-01-02 08:49:35 +13:00
if (url[i] == '$' && !escape)
2013-11-03 23:53:49 +13:00
{
if (!syntaxStart)
2013-11-03 23:53:49 +13:00
{
syntaxStart = true;
syntaxStartIndex = i + 1;
2013-11-03 23:53:49 +13:00
}
else
{
syntaxStart = false;
int syntaxLength = i - syntaxStartIndex;
2013-11-03 23:53:49 +13:00
if (syntaxLength > 0)
2013-11-03 23:53:49 +13:00
{
string syntax = url.Substring(syntaxStartIndex, syntaxLength);
string syntaxResult = ParseSyntax(syntax, usingResponse);
if (!string.IsNullOrEmpty(syntaxResult))
{
result.Append(syntaxResult);
}
2013-11-03 23:53:49 +13:00
}
}
2018-01-02 09:01:13 +13:00
escape = false;
2013-11-03 23:53:49 +13:00
}
else if (url[i] == '\\' && !escape)
2018-01-02 08:49:35 +13:00
{
escape = true;
}
else if (!syntaxStart)
2013-11-03 23:53:49 +13:00
{
result.Append(url[i]);
2018-01-02 08:56:08 +13:00
escape = false;
2013-11-03 23:53:49 +13:00
}
}
return result.ToString();
}
private string ParseSyntax(string syntax, bool usingResponse)
{
if (usingResponse)
{
if (syntax.Equals("response", StringComparison.InvariantCultureIgnoreCase)) // Example: $response$
{
return response;
}
else if (syntax.StartsWith("regex:", StringComparison.InvariantCultureIgnoreCase)) // Example: $regex:1|1$
{
return ParseSyntax(CustomUploaderResponseParseType.Regex, syntax.Substring(6));
}
else if (syntax.StartsWith("json:", StringComparison.InvariantCultureIgnoreCase)) // Example: $json:Files[0].URL$
{
return ParseSyntax(CustomUploaderResponseParseType.Json, syntax.Substring(5));
}
else if (syntax.StartsWith("xml:", StringComparison.InvariantCultureIgnoreCase)) // Example: $xml:/Files/File[1]/URL$
{
return ParseSyntax(CustomUploaderResponseParseType.Xml, syntax.Substring(4));
}
}
if (syntax.StartsWith("random:", StringComparison.InvariantCultureIgnoreCase)) // Example: $random:domain1.com|domain2.com$
{
return ParseSyntax(CustomUploaderResponseParseType.Random, syntax.Substring(7));
}
return null;
}
private string ParseSyntax(CustomUploaderResponseParseType parseType, string syntax)
{
if (!string.IsNullOrEmpty(syntax))
{
switch (parseType)
{
case CustomUploaderResponseParseType.Regex:
return ParseRegexSyntax(syntax);
case CustomUploaderResponseParseType.Json:
return ParseJsonSyntax(syntax);
case CustomUploaderResponseParseType.Xml:
return ParseXmlSyntax(syntax);
case CustomUploaderResponseParseType.Random:
return ParseRandomSyntax(syntax);
}
}
return null;
}
private string ParseRegexSyntax(string syntax)
2013-11-03 23:53:49 +13:00
{
string regexIndexString = "";
int regexIndex;
bool isGroupRegex = false;
2015-12-23 03:51:47 +13:00
int i;
2013-11-03 23:53:49 +13:00
2015-12-23 03:51:47 +13:00
for (i = 0; i < syntax.Length; i++)
{
if (char.IsDigit(syntax[i]))
2013-11-03 23:53:49 +13:00
{
regexIndexString += syntax[i];
}
else
{
if (syntax[i] == '|' || syntax[i] == ',')
2013-11-03 23:53:49 +13:00
{
isGroupRegex = true;
2013-11-03 23:53:49 +13:00
}
break;
2013-11-03 23:53:49 +13:00
}
}
if (regexIndexString.Length > 0 && int.TryParse(regexIndexString, out regexIndex))
{
Match match = regexResult[regexIndex - 1];
2013-11-03 23:53:49 +13:00
if (isGroupRegex && i + 1 < syntax.Length)
2013-11-03 23:53:49 +13:00
{
string group = syntax.Substring(i + 1);
int groupNumber;
2013-11-03 23:53:49 +13:00
if (int.TryParse(group, out groupNumber))
2013-11-03 23:53:49 +13:00
{
return match.Groups[groupNumber].Value;
2013-11-03 23:53:49 +13:00
}
return match.Groups[group].Value;
2013-11-03 23:53:49 +13:00
}
return match.Value;
2013-11-03 23:53:49 +13:00
}
return null;
}
// http://goessner.net/articles/JsonPath/
private string ParseJsonSyntax(string syntaxJsonPath)
{
return (string)JToken.Parse(response).SelectToken("$." + syntaxJsonPath);
}
// http://www.w3schools.com/xsl/xpath_syntax.asp
// https://msdn.microsoft.com/en-us/library/ms256086(v=vs.110).aspx
private string ParseXmlSyntax(string syntaxXPath)
{
using (StringReader sr = new StringReader(response))
{
XPathDocument doc = new XPathDocument(sr);
XPathNavigator nav = doc.CreateNavigator();
XPathNavigator node = nav.SelectSingleNode(syntaxXPath);
if (node != null)
{
return node.Value;
}
}
return null;
}
private string ParseRandomSyntax(string syntax)
{
string[] values = syntax.Split('|');
if (values.Length > 0)
{
return values[MathHelpers.Random(values.Length - 1)];
}
return "";
}
2013-11-03 23:53:49 +13:00
}
}