Adding new custom uploader parser

This commit is contained in:
Jaex 2022-01-30 12:42:18 +03:00
parent ce368ef007
commit bb091fee19
15 changed files with 760 additions and 0 deletions

View file

@ -0,0 +1,134 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2022 ShareX Team
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 ShareX.HelpersLib;
using System;
using System.Collections.Generic;
using System.Text;
namespace ShareX.UploadersLib
{
public class CustomUploaderParser2
{
private static IEnumerable<CustomUploaderFunction> Functions = Helpers.GetInstances<CustomUploaderFunction>();
public char SyntaxStart { get; private set; } = '{';
public char SyntaxEnd { get; private set; } = '}';
public char SyntaxParameterStart { get; private set; } = ':';
public char SyntaxParameterDelimiter { get; private set; } = '|';
public char SyntaxEscape { get; private set; } = '\\';
public string FileName { get; set; }
public string Input { get; set; }
public ResponseInfo ResponseInfo { get; set; }
public List<string> RegexList { get; set; }
public bool URLEncode { get; set; } // Only URL encodes file name and input
public string Parse(string text)
{
return ParseSyntax(text, 0, out _);
}
private string ParseSyntax(string text, int startPosition, out int endPosition)
{
endPosition = startPosition;
if (string.IsNullOrEmpty(text))
{
return "";
}
StringBuilder sbResult = new StringBuilder();
bool escape = false;
for (int i = startPosition; i < text.Length; i++)
{
if (!escape)
{
if (text[i] == SyntaxStart)
{
string parsed = ParseSyntax(text, i + 1, out i);
parsed = ParseFunction(parsed);
sbResult.Append(parsed);
continue;
}
else if (text[i] == SyntaxEnd)
{
endPosition = i;
return sbResult.ToString();
}
else if (text[i] == SyntaxEscape)
{
escape = true;
continue;
}
}
else
{
escape = false;
}
sbResult.Append(text[i]);
}
return sbResult.ToString();
}
private string ParseFunction(string text)
{
string functionName;
string[] parameterArray = null;
int parameterPosition = text.IndexOf(SyntaxParameterStart);
if (parameterPosition >= 0)
{
functionName = text.Remove(parameterPosition);
string parameters = text.Substring(parameterPosition + 1);
// TODO: Support delimiter escape
parameterArray = parameters.Split(SyntaxParameterDelimiter);
}
else
{
functionName = text;
}
return CallFunction(functionName, parameterArray);
}
private string CallFunction(string functionName, string[] parameters)
{
foreach (CustomUploaderFunction function in Functions)
{
if (function.Name.Equals(functionName, StringComparison.InvariantCultureIgnoreCase))
{
return function.Call(this, parameters);
}
}
throw new Exception("Invalid function name: " + functionName);
}
}
}

View file

@ -0,0 +1,34 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2022 ShareX Team
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)
namespace ShareX.UploadersLib
{
internal abstract class CustomUploaderFunction
{
public abstract string Name { get; }
public abstract string Call(CustomUploaderParser2 parser, string[] parameters);
}
}

View file

@ -0,0 +1,46 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2022 ShareX Team
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 ShareX.HelpersLib;
namespace ShareX.UploadersLib
{
internal class CustomUploaderFunctionBase64 : CustomUploaderFunction
{
public override string Name { get; } = "base64";
public override string Call(CustomUploaderParser2 parser, string[] parameters)
{
string text = parameters[0];
if (!string.IsNullOrEmpty(text))
{
return TranslatorHelper.TextToBase64(text);
}
return null;
}
}
}

View file

@ -0,0 +1,44 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2022 ShareX Team
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 ShareX.HelpersLib;
namespace ShareX.UploadersLib
{
internal class CustomUploaderFunctionFileName : CustomUploaderFunction
{
public override string Name { get; } = "filename";
public override string Call(CustomUploaderParser2 parser, string[] parameters)
{
if (parser.URLEncode)
{
return URLHelpers.URLEncode(parser.FileName);
}
return parser.FileName;
}
}
}

View file

@ -0,0 +1,44 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2022 ShareX Team
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)
namespace ShareX.UploadersLib
{
internal class CustomUploaderFunctionHeader : CustomUploaderFunction
{
public override string Name { get; } = "header";
public override string Call(CustomUploaderParser2 parser, string[] parameters)
{
string header = parameters[0];
if (parser.ResponseInfo.Headers != null)
{
return parser.ResponseInfo.Headers[header];
}
return null;
}
}
}

View file

@ -0,0 +1,44 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2022 ShareX Team
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 ShareX.HelpersLib;
namespace ShareX.UploadersLib
{
internal class CustomUploaderFunctionInput : CustomUploaderFunction
{
public override string Name { get; } = "input";
public override string Call(CustomUploaderParser2 parser, string[] parameters)
{
if (parser.URLEncode)
{
return URLHelpers.URLEncode(parser.Input);
}
return parser.Input;
}
}
}

View file

@ -0,0 +1,51 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2022 ShareX Team
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 Newtonsoft.Json.Linq;
namespace ShareX.UploadersLib
{
internal class CustomUploaderFunctionJson : CustomUploaderFunction
{
public override string Name { get; } = "json";
public override string Call(CustomUploaderParser2 parser, string[] parameters)
{
string jsonPath = parameters[0];
if (!string.IsNullOrEmpty(jsonPath))
{
if (!jsonPath.StartsWith("$."))
{
jsonPath = "$." + jsonPath;
}
return (string)JToken.Parse(parser.ResponseInfo.ResponseText).SelectToken(jsonPath);
}
return null;
}
}
}

View file

@ -0,0 +1,60 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2022 ShareX Team
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 ShareX.HelpersLib;
using System.Windows.Forms;
namespace ShareX.UploadersLib
{
internal class CustomUploaderFunctionPrompt : CustomUploaderFunction
{
public override string Name { get; } = "prompt";
public override string Call(CustomUploaderParser2 parser, string[] parameters)
{
string title = "ShareX - Prompt", defaultValue = "";
if (parameters.Length > 0)
{
title = parameters[0];
if (parameters.Length > 1)
{
defaultValue = parameters[1];
}
}
using (InputBox inputBox = new InputBox(title, defaultValue))
{
if (inputBox.ShowDialog() == DialogResult.OK)
{
return inputBox.InputText;
}
}
return defaultValue;
}
}
}

View file

@ -0,0 +1,44 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2022 ShareX Team
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 ShareX.HelpersLib;
namespace ShareX.UploadersLib
{
internal class CustomUploaderFunctionRandom : CustomUploaderFunction
{
public override string Name { get; } = "random";
public override string Call(CustomUploaderParser2 parser, string[] parameters)
{
if (parameters.Length > 0)
{
return RandomFast.Pick(parameters);
}
return null;
}
}
}

View file

@ -0,0 +1,64 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2022 ShareX Team
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 System.Text.RegularExpressions;
namespace ShareX.UploadersLib
{
internal class CustomUploaderFunctionRegex : CustomUploaderFunction
{
public override string Name { get; } = "regex";
public override string Call(CustomUploaderParser2 parser, string[] parameters)
{
if (parameters.Length > 0)
{
string regexIndex = parameters[0];
if (!string.IsNullOrEmpty(regexIndex) && int.TryParse(regexIndex, out int regexIndexNumber))
{
string pattern = parser.RegexList[regexIndexNumber - 1];
Match match = Regex.Match(parser.ResponseInfo.ResponseText, pattern);
if (parameters.Length > 1)
{
string regexGroup = parameters[1];
if (!string.IsNullOrEmpty(regexGroup) && int.TryParse(regexGroup, out int regexGroupNumber))
{
return match.Groups[regexGroupNumber].Value;
}
return match.Groups[regexGroup].Value;
}
return match.Value;
}
}
return null;
}
}
}

View file

@ -0,0 +1,37 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2022 ShareX Team
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)
namespace ShareX.UploadersLib
{
internal class CustomUploaderFunctionResponse : CustomUploaderFunction
{
public override string Name { get; } = "response";
public override string Call(CustomUploaderParser2 parser, string[] parameters)
{
return parser.ResponseInfo.ResponseText;
}
}
}

View file

@ -0,0 +1,37 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2022 ShareX Team
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)
namespace ShareX.UploadersLib
{
internal class CustomUploaderFunctionResponseURL : CustomUploaderFunction
{
public override string Name { get; } = "responseurl";
public override string Call(CustomUploaderParser2 parser, string[] parameters)
{
return parser.ResponseInfo.ResponseURL;
}
}
}

View file

@ -0,0 +1,50 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2022 ShareX Team
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 System.Linq;
namespace ShareX.UploadersLib
{
internal class CustomUploaderFunctionSelect : CustomUploaderFunction
{
public override string Name { get; } = "select";
public override string Call(CustomUploaderParser2 parser, string[] parameters)
{
string[] values = parameters.Where(x => !string.IsNullOrEmpty(x)).ToArray();
if (values.Length > 0)
{
using (ParserSelectForm form = new ParserSelectForm(values))
{
form.ShowDialog();
return form.SelectedText;
}
}
return null;
}
}
}

View file

@ -0,0 +1,57 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2022 ShareX Team
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 System.IO;
using System.Xml.XPath;
namespace ShareX.UploadersLib
{
internal class CustomUploaderFunctionXml : CustomUploaderFunction
{
public override string Name { get; } = "xml";
public override string Call(CustomUploaderParser2 parser, string[] parameters)
{
string xpath = parameters[0];
if (!string.IsNullOrEmpty(xpath))
{
using (StringReader sr = new StringReader(parser.ResponseInfo.ResponseText))
{
XPathDocument doc = new XPathDocument(sr);
XPathNavigator nav = doc.CreateNavigator();
XPathNavigator node = nav.SelectSingleNode(xpath);
if (node != null)
{
return node.Value;
}
}
}
return null;
}
}
}

View file

@ -125,6 +125,20 @@
<Compile Include="BaseServices\IUploaderService.cs" />
<Compile Include="BaseUploaders\GenericUploader.cs" />
<Compile Include="BaseUploaders\URLSharer.cs" />
<Compile Include="CustomUploader\Functions\CustomUploaderFunction.cs" />
<Compile Include="CustomUploader\Functions\CustomUploaderFunctionBase64.cs" />
<Compile Include="CustomUploader\Functions\CustomUploaderFunctionFileName.cs" />
<Compile Include="CustomUploader\Functions\CustomUploaderFunctionInput.cs" />
<Compile Include="CustomUploader\Functions\CustomUploaderFunctionResponseURL.cs" />
<Compile Include="CustomUploader\Functions\CustomUploaderFunctionResponse.cs" />
<Compile Include="CustomUploader\Functions\CustomUploaderFunctionPrompt.cs" />
<Compile Include="CustomUploader\Functions\CustomUploaderFunctionSelect.cs" />
<Compile Include="CustomUploader\Functions\CustomUploaderFunctionRandom.cs" />
<Compile Include="CustomUploader\Functions\CustomUploaderFunctionXml.cs" />
<Compile Include="CustomUploader\Functions\CustomUploaderFunctionHeader.cs" />
<Compile Include="CustomUploader\Functions\CustomUploaderFunctionRegex.cs" />
<Compile Include="CustomUploader\Functions\CustomUploaderFunctionJson.cs" />
<Compile Include="CustomUploader\CustomUploaderParser2.cs" />
<Compile Include="FileUploaders\AmazonS3.cs" />
<Compile Include="FileUploaders\AmazonS3Endpoint.cs" />
<Compile Include="FileUploaders\AmazonS3Settings.cs" />