mirror of
https://github.com/ShareX/ShareX.git
synced 2024-10-04 04:06:05 +13:00
Added json parsing support to custom uploader, example syntax: $json:files[0].url$
This commit is contained in:
parent
8447e68548
commit
a11b7a8d9b
2 changed files with 96 additions and 55 deletions
|
@ -297,6 +297,12 @@ public enum CustomUploaderRequestType
|
||||||
DELETE
|
DELETE
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public enum CustomUploaderResponseParseType
|
||||||
|
{
|
||||||
|
Regex,
|
||||||
|
Json
|
||||||
|
}
|
||||||
|
|
||||||
public enum FTPSEncryption
|
public enum FTPSEncryption
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
@ -23,6 +23,7 @@ You should have received a copy of the GNU General Public License
|
||||||
|
|
||||||
#endregion License Information (GPL v3)
|
#endregion License Information (GPL v3)
|
||||||
|
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
using ShareX.HelpersLib;
|
using ShareX.HelpersLib;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
@ -46,6 +47,7 @@ public class CustomUploaderItem
|
||||||
public string ThumbnailURL { get; set; }
|
public string ThumbnailURL { get; set; }
|
||||||
public string DeletionURL { get; set; }
|
public string DeletionURL { get; set; }
|
||||||
|
|
||||||
|
private string response;
|
||||||
private List<Match> regexResult;
|
private List<Match> regexResult;
|
||||||
|
|
||||||
public CustomUploaderItem()
|
public CustomUploaderItem()
|
||||||
|
@ -143,7 +145,8 @@ public void ParseResponse(UploadResult result, bool isShortenedURL = false)
|
||||||
{
|
{
|
||||||
if (result != null && !string.IsNullOrEmpty(result.Response))
|
if (result != null && !string.IsNullOrEmpty(result.Response))
|
||||||
{
|
{
|
||||||
regexResult = ParseRegexList(result.Response);
|
response = result.Response;
|
||||||
|
ParseRegexList();
|
||||||
|
|
||||||
string url;
|
string url;
|
||||||
|
|
||||||
|
@ -153,7 +156,7 @@ public void ParseResponse(UploadResult result, bool isShortenedURL = false)
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
url = result.Response;
|
url = response;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isShortenedURL)
|
if (isShortenedURL)
|
||||||
|
@ -170,19 +173,17 @@ public void ParseResponse(UploadResult result, bool isShortenedURL = false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Match> ParseRegexList(string response)
|
private void ParseRegexList()
|
||||||
{
|
{
|
||||||
List<Match> result = new List<Match>();
|
regexResult = new List<Match>();
|
||||||
|
|
||||||
if (RegexList != null)
|
if (RegexList != null)
|
||||||
{
|
{
|
||||||
foreach (string regex in RegexList)
|
foreach (string regex in RegexList)
|
||||||
{
|
{
|
||||||
result.Add(Regex.Match(response, regex));
|
regexResult.Add(Regex.Match(response, regex));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private string ParseURL(string url)
|
private string ParseURL(string url)
|
||||||
|
@ -194,33 +195,65 @@ private string ParseURL(string url)
|
||||||
|
|
||||||
StringBuilder result = new StringBuilder();
|
StringBuilder result = new StringBuilder();
|
||||||
|
|
||||||
bool regexStart = false;
|
bool syntaxStart = false;
|
||||||
int regexStartIndex = 0;
|
CustomUploaderResponseParseType parseType = CustomUploaderResponseParseType.Regex;
|
||||||
|
int syntaxStartIndex = 0;
|
||||||
|
|
||||||
for (int i = 0; i < url.Length; i++)
|
for (int i = 0; i < url.Length; i++)
|
||||||
{
|
{
|
||||||
if (url[i] == '$')
|
if (url[i] == '$')
|
||||||
{
|
{
|
||||||
if (!regexStart)
|
if (!syntaxStart)
|
||||||
{
|
{
|
||||||
regexStart = true;
|
syntaxStart = true;
|
||||||
regexStartIndex = i;
|
|
||||||
|
string syntaxCheck = url.Substring(i + 1);
|
||||||
|
|
||||||
|
if (syntaxCheck.StartsWith("json:", StringComparison.InvariantCultureIgnoreCase))
|
||||||
|
{
|
||||||
|
parseType = CustomUploaderResponseParseType.Json;
|
||||||
|
syntaxStartIndex = i + 6;
|
||||||
|
}
|
||||||
|
else if (syntaxCheck.StartsWith("regex:", StringComparison.InvariantCultureIgnoreCase))
|
||||||
|
{
|
||||||
|
parseType = CustomUploaderResponseParseType.Regex;
|
||||||
|
syntaxStartIndex = i + 7;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
string regexResult = ParseRegexSyntax(url.Substring(regexStartIndex + 1, i - regexStartIndex - 1));
|
parseType = CustomUploaderResponseParseType.Regex;
|
||||||
|
syntaxStartIndex = i + 1;
|
||||||
if (!string.IsNullOrEmpty(regexResult))
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
result.Append(regexResult);
|
string parseText = url.Substring(syntaxStartIndex, i - syntaxStartIndex).Trim();
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(parseText))
|
||||||
|
{
|
||||||
|
string resultText;
|
||||||
|
|
||||||
|
switch (parseType)
|
||||||
|
{
|
||||||
|
default:
|
||||||
|
case CustomUploaderResponseParseType.Regex:
|
||||||
|
resultText = ParseRegexSyntax(parseText);
|
||||||
|
break;
|
||||||
|
case CustomUploaderResponseParseType.Json:
|
||||||
|
resultText = ParseJsonSyntax(parseText);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
regexStart = false;
|
if (!string.IsNullOrEmpty(resultText))
|
||||||
continue;
|
{
|
||||||
|
result.Append(resultText);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!regexStart)
|
syntaxStart = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (!syntaxStart)
|
||||||
{
|
{
|
||||||
result.Append(url[i]);
|
result.Append(url[i]);
|
||||||
}
|
}
|
||||||
|
@ -229,24 +262,22 @@ private string ParseURL(string url)
|
||||||
return result.ToString();
|
return result.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private string ParseRegexSyntax(string text)
|
private string ParseRegexSyntax(string syntax)
|
||||||
{
|
|
||||||
if (text.Length > 0)
|
|
||||||
{
|
{
|
||||||
int i = 0;
|
int i = 0;
|
||||||
string regexIndexString = "";
|
string regexIndexString = "";
|
||||||
int regexIndex;
|
int regexIndex;
|
||||||
bool isGroupRegex = false;
|
bool isGroupRegex = false;
|
||||||
|
|
||||||
for (; i < text.Length; i++)
|
for (; i < syntax.Length; i++)
|
||||||
{
|
{
|
||||||
if (char.IsDigit(text[i]))
|
if (char.IsDigit(syntax[i]))
|
||||||
{
|
{
|
||||||
regexIndexString += text[i];
|
regexIndexString += syntax[i];
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (text[i] == ',')
|
if (syntax[i] == ',')
|
||||||
{
|
{
|
||||||
isGroupRegex = true;
|
isGroupRegex = true;
|
||||||
}
|
}
|
||||||
|
@ -259,9 +290,9 @@ private string ParseRegexSyntax(string text)
|
||||||
{
|
{
|
||||||
Match match = regexResult[regexIndex - 1];
|
Match match = regexResult[regexIndex - 1];
|
||||||
|
|
||||||
if (isGroupRegex && i + 1 < text.Length)
|
if (isGroupRegex && i + 1 < syntax.Length)
|
||||||
{
|
{
|
||||||
string group = text.Substring(i + 1);
|
string group = syntax.Substring(i + 1);
|
||||||
int groupNumber;
|
int groupNumber;
|
||||||
|
|
||||||
if (int.TryParse(group, out groupNumber))
|
if (int.TryParse(group, out groupNumber))
|
||||||
|
@ -274,9 +305,13 @@ private string ParseRegexSyntax(string text)
|
||||||
|
|
||||||
return match.Value;
|
return match.Value;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private string ParseJsonSyntax(string syntax)
|
||||||
|
{
|
||||||
|
return (string)JObject.Parse(response).SelectToken(syntax);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue