ShareX/ShareX.UploadersLib/Helpers/CustomUploaderItem.cs

379 lines
12 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
2017-01-11 21:39:40 +13:00
Copyright (c) 2007-2017 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)
2014-12-11 09:25:20 +13:00
using ShareX.HelpersLib;
using System;
2013-11-03 23:53:49 +13:00
using System.Collections.Generic;
using System.Collections.Specialized;
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
{
public string Name { get; set; } = "example.com";
public CustomUploaderDestinationType DestinationType { get; set; }
2013-11-03 23:53:49 +13:00
public CustomUploaderRequestType RequestType { get; set; }
public string RequestURL { get; set; }
public string FileFormName { get; set; }
public Dictionary<string, string> Arguments { get; set; } = new Dictionary<string, string>();
public Dictionary<string, string> Headers { get; set; } = new Dictionary<string, string>();
2013-11-03 23:53:49 +13:00
public ResponseType ResponseType { get; set; }
public List<string> RegexList { get; set; } = new List<string>();
2013-11-03 23:53:49 +13:00
public string URL { get; set; }
public string ThumbnailURL { get; set; }
public string DeletionURL { get; set; }
private string response;
2013-11-03 23:53:49 +13:00
private List<Match> regexResult;
public CustomUploaderItem()
{
}
public CustomUploaderItem(string name)
{
Name = name;
}
public override string ToString()
{
return Name;
}
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))
{
throw new Exception("'Request URL' must be not empty.");
}
string url = ParseURL(RequestURL, false);
return URLHelpers.FixPrefix(url);
}
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)
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)
{
string value = arg.Value;
2013-11-03 23:53:49 +13:00
2015-12-23 11:21:19 +13:00
value = NameParser.Parse(NameParserType.Text, value);
value = value.Replace("$input$", input);
2013-11-03 23:53:49 +13:00
2015-12-23 11:21:19 +13:00
arguments.Add(arg.Key, value);
}
2013-11-03 23:53:49 +13:00
}
return arguments;
}
public NameValueCollection GetHeaders()
{
if (Headers != null && Headers.Count > 0)
{
NameValueCollection collection = new NameValueCollection();
foreach (KeyValuePair<string, string> header in Headers)
{
collection.Add(header.Key, 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
}
}
private string ParseURL(string url, bool output)
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;
2013-11-03 23:53:49 +13:00
for (int i = 0; i < url.Length; i++)
{
if (url[i] == '$')
{
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, output);
if (!string.IsNullOrEmpty(syntaxResult))
{
result.Append(syntaxResult);
}
2013-11-03 23:53:49 +13:00
}
}
}
else if (!syntaxStart)
2013-11-03 23:53:49 +13:00
{
result.Append(url[i]);
}
}
return result.ToString();
}
private string ParseSyntax(string syntax, bool output)
{
CustomUploaderResponseParseType parseType;
if (syntax.Equals("response", StringComparison.InvariantCultureIgnoreCase)) // Example: $response$
{
return response;
}
else if (syntax.StartsWith("regex:", StringComparison.InvariantCultureIgnoreCase)) // Example: $regex:1,1$
{
parseType = CustomUploaderResponseParseType.Regex;
syntax = syntax.Substring(6);
}
else if (syntax.StartsWith("json:", StringComparison.InvariantCultureIgnoreCase)) // Example: $json:Files[0].URL$
{
parseType = CustomUploaderResponseParseType.Json;
syntax = syntax.Substring(5);
}
else if (syntax.StartsWith("xml:", StringComparison.InvariantCultureIgnoreCase)) // Example: $xml:/Files/File[1]/URL$
{
parseType = CustomUploaderResponseParseType.Xml;
syntax = syntax.Substring(4);
}
else if (syntax.StartsWith("random:", StringComparison.InvariantCultureIgnoreCase)) // Example: $random:domain1.com|domain2.com$
{
parseType = CustomUploaderResponseParseType.Random;
syntax = syntax.Substring(7);
}
else // Example: $1,1$
{
parseType = CustomUploaderResponseParseType.Regex;
}
if (!string.IsNullOrEmpty(syntax))
{
if (output)
{
switch (parseType)
{
case CustomUploaderResponseParseType.Regex:
return ParseRegexSyntax(syntax);
case CustomUploaderResponseParseType.Json:
return ParseJsonSyntax(syntax);
case CustomUploaderResponseParseType.Xml:
return ParseXmlSyntax(syntax);
}
}
if (parseType == 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] == ',')
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 Helpers.ParseJSON(response, 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
}
}