Added $responseurl$ syntax support to custom uploader, which can be used in place of previously removed redirection url response type

This commit is contained in:
Jaex 2019-01-21 17:06:11 +03:00
parent 58cd13db01
commit bab795bb57
6 changed files with 24 additions and 17 deletions

View file

@ -241,11 +241,11 @@ public NameValueCollection GetHeaders(CustomUploaderInput input)
return null;
}
public void ParseResponse(UploadResult result, CustomUploaderInput input, bool isShortenedURL = false)
public void ParseResponse(UploadResult result, ResponseInfo responseInfo, CustomUploaderInput input, bool isShortenedURL = false)
{
if (result != null && !string.IsNullOrEmpty(result.Response))
if (result != null && responseInfo != null && !string.IsNullOrEmpty(responseInfo.ResponseText))
{
CustomUploaderParser parser = new CustomUploaderParser(result.Response, RegexList);
CustomUploaderParser parser = new CustomUploaderParser(responseInfo, RegexList);
parser.Filename = input.Filename;
parser.URLEncode = true;
@ -257,7 +257,7 @@ public void ParseResponse(UploadResult result, CustomUploaderInput input, bool i
}
else
{
url = parser.Response;
url = parser.ResponseInfo.ResponseText;
}
if (isShortenedURL)

View file

@ -23,14 +23,12 @@
#endregion License Information (GPL v3)
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using ShareX.HelpersLib;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
@ -47,7 +45,7 @@ public class CustomUploaderParser
public bool IsOutput { get; set; }
public string Filename { get; set; }
public string Input { get; set; }
public string Response { get; set; }
public ResponseInfo ResponseInfo { get; set; }
public List<Match> RegexMatches { get; set; }
public bool URLEncode { get; set; } // Only URL encodes filename and input
public bool JSONEncode { get; set; }
@ -70,9 +68,9 @@ public CustomUploaderParser(string filename, string input)
IsOutput = false;
}
public CustomUploaderParser(string response, List<string> regexList)
public CustomUploaderParser(ResponseInfo responseInfo, List<string> regexList)
{
Response = response;
ResponseInfo = responseInfo;
RegexMatches = new List<Match>();
@ -80,7 +78,7 @@ public CustomUploaderParser(string response, List<string> regexList)
{
foreach (string regex in regexList)
{
Match match = Regex.Match(response, regex);
Match match = Regex.Match(ResponseInfo.ResponseText, regex);
RegexMatches.Add(match);
}
}
@ -201,7 +199,11 @@ private string ParseSyntax(string syntax, bool isOutput)
{
if (CheckKeyword(syntax, "response")) // Example: $response$
{
return Response;
return ResponseInfo.ResponseText;
}
else if (CheckKeyword(syntax, "responseurl")) // Example: $responseurl$
{
return ResponseInfo.ResponseURL;
}
else if (CheckKeyword(syntax, "regex", out value)) // Examples: $regex:1$ $regex:1|1$ $regex:1|thumbnail$
{
@ -334,7 +336,12 @@ private string ParseSyntaxJson(string syntaxJsonPath)
{
if (!string.IsNullOrEmpty(syntaxJsonPath))
{
return (string)JToken.Parse(Response).SelectToken("$." + syntaxJsonPath);
if (!syntaxJsonPath.StartsWith("$."))
{
syntaxJsonPath = "$." + syntaxJsonPath;
}
return (string)JToken.Parse(ResponseInfo.ResponseText).SelectToken(syntaxJsonPath);
}
return null;
@ -346,7 +353,7 @@ private string ParseSyntaxXml(string syntaxXPath)
{
if (!string.IsNullOrEmpty(syntaxXPath))
{
using (StringReader sr = new StringReader(Response))
using (StringReader sr = new StringReader(ResponseInfo.ResponseText))
{
XPathDocument doc = new XPathDocument(sr);
XPathNavigator nav = doc.CreateNavigator();

View file

@ -100,7 +100,7 @@ public override UploadResult Upload(Stream stream, string fileName)
try
{
uploader.ParseResponse(result, input);
uploader.ParseResponse(result, LastResponseInfo, input);
}
catch (Exception e)
{

View file

@ -97,7 +97,7 @@ public override UploadResult Upload(Stream stream, string fileName)
try
{
uploader.ParseResponse(result, input);
uploader.ParseResponse(result, LastResponseInfo, input);
}
catch (Exception e)
{

View file

@ -129,7 +129,7 @@ public override UploadResult UploadText(string text, string fileName)
try
{
uploader.ParseResponse(result, input);
uploader.ParseResponse(result, LastResponseInfo, input);
}
catch (Exception e)
{

View file

@ -102,7 +102,7 @@ public override UploadResult ShortenURL(string url)
try
{
uploader.ParseResponse(result, input, true);
uploader.ParseResponse(result, LastResponseInfo, input, true);
}
catch (Exception e)
{