ShareX/UploadersLib/HelperClasses/CustomUploaderItem.cs

228 lines
6.6 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
2014-05-13 21:06:40 +12:00
Copyright (C) 2007-2014 ShareX Developers
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 HelpersLib;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
2013-12-08 05:08:38 +13:00
namespace 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; }
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>();
RegexList = new List<string>();
}
public CustomUploaderItem(string name)
: this()
{
Name = name;
}
public override string ToString()
{
return Name;
}
2013-12-08 05:08:38 +13:00
public Dictionary<string, string> ParseArguments(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;
2013-12-08 05:08:38 +13:00
if (!string.IsNullOrEmpty(input))
2013-11-03 23:53:49 +13:00
{
2013-12-08 05:08:38 +13:00
value = value.Replace("%input", input);
value = value.Replace("$input$", input);
2013-11-03 23:53:49 +13:00
}
NameParser parser = new NameParser(NameParserType.Text);
value = parser.Parse(value);
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;
}
}
}