ShareX/ShareX.UploadersLib/HelperClasses/CustomUploaderItem.cs

264 lines
7.8 KiB
C#
Raw Normal View History

2013-11-03 23:53:49 +13:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2015-08-13 13:07:38 +12:00
Copyright (c) 2007-2015 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.Text;
using System.Text.RegularExpressions;
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; }
public CustomUploaderRequestType RequestType { get; set; }
public string RequestURL { get; set; }
public string FileFormName { get; set; }
public Dictionary<string, string> Arguments { get; set; }
2015-10-20 01:26:36 +13:00
public Dictionary<string, string> Headers { get; set; }
2013-11-03 23:53:49 +13:00
public ResponseType ResponseType { get; set; }
public List<string> RegexList { get; set; }
public string URL { get; set; }
public string ThumbnailURL { get; set; }
public string DeletionURL { get; set; }
private List<Match> regexResult;
public CustomUploaderItem()
{
Arguments = new Dictionary<string, string>();
2015-10-20 01:26:36 +13:00
Headers = new Dictionary<string, string>();
2013-11-03 23:53:49 +13:00
RegexList = new List<string>();
}
public CustomUploaderItem(string name)
: this()
{
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.");
}
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)
2013-11-03 23:53:49 +13:00
{
Dictionary<string, string> arguments = new Dictionary<string, string>();
foreach (KeyValuePair<string, string> arg in Arguments)
{
string value = arg.Value;
value = value.Replace("%input", "$input$"); // For backward compatibility
value = NameParser.Parse(NameParserType.Text, value);
value = value.Replace("$input$", input);
2013-11-03 23:53:49 +13:00
arguments.Add(arg.Key, value);
}
return arguments;
}
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
{
2013-12-08 05:08:38 +13:00
regexResult = ParseRegexList(result.Response);
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);
}
else
{
url = result.Response;
}
if (isShortenedURL)
{
result.ShortenedURL = url;
}
else
{
result.URL = url;
}
result.ThumbnailURL = ParseURL(ThumbnailURL);
result.DeletionURL = ParseURL(DeletionURL);
2013-11-03 23:53:49 +13:00
}
2013-12-08 05:08:38 +13:00
}
2013-11-03 23:53:49 +13:00
2013-12-08 05:08:38 +13:00
private List<Match> ParseRegexList(string response)
{
List<Match> result = new List<Match>();
if (RegexList != null)
2013-11-03 23:53:49 +13:00
{
2013-12-08 05:08:38 +13:00
foreach (string regex in RegexList)
{
result.Add(Regex.Match(response, regex));
}
2013-11-03 23:53:49 +13:00
}
2013-12-08 05:08:38 +13:00
return result;
2013-11-03 23:53:49 +13:00
}
private string ParseURL(string url)
{
2013-12-08 05:08:38 +13:00
if (string.IsNullOrEmpty(url))
{
return string.Empty;
}
2013-11-03 23:53:49 +13:00
StringBuilder result = new StringBuilder();
bool regexStart = false;
int regexStartIndex = 0;
for (int i = 0; i < url.Length; i++)
{
if (url[i] == '$')
{
if (!regexStart)
{
regexStart = true;
regexStartIndex = i;
}
else
{
string regexResult = ParseRegexSyntax(url.Substring(regexStartIndex + 1, i - regexStartIndex - 1));
if (!string.IsNullOrEmpty(regexResult))
{
result.Append(regexResult);
}
regexStart = false;
continue;
}
}
if (!regexStart)
{
result.Append(url[i]);
}
}
return result.ToString();
}
private string ParseRegexSyntax(string text)
{
if (text.Length > 0)
{
int i = 0;
string regexIndexString = "";
int regexIndex;
bool isGroupRegex = false;
for (; i < text.Length; i++)
{
if (char.IsDigit(text[i]))
{
regexIndexString += text[i];
}
else
{
if (text[i] == ',')
{
isGroupRegex = true;
}
break;
}
}
if (regexIndexString.Length > 0 && int.TryParse(regexIndexString, out regexIndex))
{
Match match = regexResult[regexIndex - 1];
if (isGroupRegex && i + 1 < text.Length)
{
string group = text.Substring(i + 1);
int groupNumber;
if (int.TryParse(group, out groupNumber))
{
return match.Groups[groupNumber].Value;
}
2013-12-09 10:28:19 +13:00
return match.Groups[group].Value;
2013-11-03 23:53:49 +13:00
}
return match.Value;
}
}
return null;
}
}
}