ShareX/ShareX.UploadersLib/CustomUploader/CustomUploaderItem.cs

411 lines
14 KiB
C#
Raw Normal View History

#region License Information (GPL v3)
2013-11-03 23:53:49 +13:00
/*
ShareX - A program that allows you to take screenshots and share any file type
2020-02-05 20:19:48 +13:00
Copyright (c) 2007-2020 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)
2019-01-09 04:57:49 +13:00
using Newtonsoft.Json;
2014-12-11 09:25:20 +13:00
using ShareX.HelpersLib;
2017-10-29 09:40:50 +13:00
using ShareX.UploadersLib.Properties;
using System;
2013-11-03 23:53:49 +13:00
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Windows.Forms;
2013-11-03 23:53:49 +13:00
2014-12-11 09:25:20 +13:00
namespace ShareX.UploadersLib
2013-11-03 23:53:49 +13:00
{
public class CustomUploaderItem
{
[DefaultValue("")]
public string Version { get; set; }
[DefaultValue("")]
2017-09-03 12:13:07 +12:00
public string Name { get; set; }
public bool ShouldSerializeName() => !string.IsNullOrEmpty(Name) && Name != URLHelpers.GetHostName(RequestURL);
[DefaultValue(CustomUploaderDestinationType.None)]
public CustomUploaderDestinationType DestinationType { get; set; }
[DefaultValue(HttpMethod.POST), JsonProperty(DefaultValueHandling = DefaultValueHandling.Include)]
2019-01-09 04:57:49 +13:00
public HttpMethod RequestMethod { get; set; } = HttpMethod.POST;
// For backward compatibility
[JsonProperty]
private HttpMethod RequestType { set => RequestMethod = value; }
[DefaultValue("")]
2013-11-03 23:53:49 +13:00
public string RequestURL { get; set; }
[DefaultValue(null)]
public Dictionary<string, string> Parameters { get; set; }
public bool ShouldSerializeParameters() => Parameters != null && Parameters.Count > 0;
[DefaultValue(null)]
public Dictionary<string, string> Headers { get; set; }
public bool ShouldSerializeHeaders() => Headers != null && Headers.Count > 0;
2019-01-09 04:02:21 +13:00
[DefaultValue(CustomUploaderBody.None)]
public CustomUploaderBody Body { get; set; }
2019-01-09 04:02:21 +13:00
[DefaultValue(null)]
public Dictionary<string, string> Arguments { get; set; }
public bool ShouldSerializeArguments() => (Body == CustomUploaderBody.MultipartFormData || Body == CustomUploaderBody.FormURLEncoded) &&
Arguments != null && Arguments.Count > 0;
[DefaultValue("")]
2013-11-03 23:53:49 +13:00
public string FileFormName { get; set; }
2019-01-09 04:02:21 +13:00
public bool ShouldSerializeFileFormName() => Body == CustomUploaderBody.MultipartFormData && !string.IsNullOrEmpty(FileFormName);
[DefaultValue("")]
public string Data { get; set; }
public bool ShouldSerializeData() => (Body == CustomUploaderBody.JSON || Body == CustomUploaderBody.XML) && !string.IsNullOrEmpty(Data);
// For backward compatibility
public ResponseType ResponseType { private get; set; }
[DefaultValue(null)]
2017-09-03 12:13:07 +12:00
public List<string> RegexList { get; set; }
public bool ShouldSerializeRegexList() => RegexList != null && RegexList.Count > 0;
[DefaultValue("")]
2013-11-03 23:53:49 +13:00
public string URL { get; set; }
[DefaultValue("")]
2013-11-03 23:53:49 +13:00
public string ThumbnailURL { get; set; }
[DefaultValue("")]
2013-11-03 23:53:49 +13:00
public string DeletionURL { get; set; }
[DefaultValue("")]
public string ErrorMessage { get; set; }
private CustomUploaderItem()
2013-11-03 23:53:49 +13:00
{
}
public static CustomUploaderItem Init()
{
return new CustomUploaderItem()
{
Version = Application.ProductVersion,
RequestMethod = HttpMethod.POST,
Body = CustomUploaderBody.MultipartFormData
};
}
2013-11-03 23:53:49 +13:00
public override string ToString()
{
if (!string.IsNullOrEmpty(Name))
{
return Name;
}
string name = URLHelpers.GetHostName(RequestURL);
2013-11-03 23:53:49 +13:00
if (!string.IsNullOrEmpty(name))
{
return name;
}
return "Name";
}
public string GetFileName()
{
return ToString() + ".sxcu";
}
public string GetRequestURL(CustomUploaderInput input)
{
if (string.IsNullOrEmpty(RequestURL))
{
2017-10-29 09:40:50 +13:00
throw new Exception(Resources.CustomUploaderItem_GetRequestURL_RequestURLMustBeConfigured);
}
2019-01-09 03:35:17 +13:00
CustomUploaderParser parser = new CustomUploaderParser(input);
parser.URLEncode = true;
string url = parser.Parse(RequestURL);
2019-01-09 03:35:17 +13:00
url = URLHelpers.FixPrefix(url);
2019-01-09 03:35:17 +13:00
Dictionary<string, string> parameters = GetParameters(input);
return URLHelpers.CreateQueryString(url, parameters);
}
public Dictionary<string, string> GetParameters(CustomUploaderInput input)
{
Dictionary<string, string> parameters = new Dictionary<string, string>();
if (Parameters != null)
{
CustomUploaderParser parser = new CustomUploaderParser(input);
parser.UseNameParser = true;
foreach (KeyValuePair<string, string> parameter in Parameters)
{
parameters.Add(parameter.Key, parser.Parse(parameter.Value));
}
}
2018-11-29 02:14:19 +13:00
return parameters;
}
public string GetContentType()
{
switch (Body)
{
case CustomUploaderBody.MultipartFormData:
2019-03-15 00:06:54 +13:00
return RequestHelpers.ContentTypeMultipartFormData;
case CustomUploaderBody.FormURLEncoded:
2019-03-15 00:06:54 +13:00
return RequestHelpers.ContentTypeURLEncoded;
case CustomUploaderBody.JSON:
2019-03-15 00:06:54 +13:00
return RequestHelpers.ContentTypeJSON;
case CustomUploaderBody.XML:
2019-03-15 00:06:54 +13:00
return RequestHelpers.ContentTypeXML;
case CustomUploaderBody.Binary:
2019-03-15 00:06:54 +13:00
return RequestHelpers.ContentTypeOctetStream;
}
return null;
}
public string GetData(CustomUploaderInput input)
{
CustomUploaderParser parser = new CustomUploaderParser(input);
parser.UseNameParser = true;
2019-01-09 04:02:21 +13:00
parser.JSONEncode = Body == CustomUploaderBody.JSON;
parser.XMLEncode = Body == CustomUploaderBody.XML;
return parser.Parse(Data);
}
public string GetFileFormName()
{
if (string.IsNullOrEmpty(FileFormName))
{
2017-10-29 09:40:50 +13:00
throw new Exception(Resources.CustomUploaderItem_GetFileFormName_FileFormNameMustBeConfigured);
}
return FileFormName;
}
public Dictionary<string, string> GetArguments(CustomUploaderInput input)
2013-11-03 23:53:49 +13:00
{
Dictionary<string, string> arguments = new Dictionary<string, string>();
2015-12-23 11:21:19 +13:00
if (Arguments != null)
2013-11-03 23:53:49 +13:00
{
CustomUploaderParser parser = new CustomUploaderParser(input);
parser.UseNameParser = true;
2015-12-23 11:21:19 +13:00
foreach (KeyValuePair<string, string> arg in Arguments)
{
arguments.Add(arg.Key, parser.Parse(arg.Value));
2015-12-23 11:21:19 +13:00
}
2013-11-03 23:53:49 +13:00
}
return arguments;
}
public NameValueCollection GetHeaders(CustomUploaderInput input)
{
if (Headers != null && Headers.Count > 0)
{
NameValueCollection collection = new NameValueCollection();
2018-11-29 02:14:19 +13:00
CustomUploaderParser parser = new CustomUploaderParser(input);
parser.UseNameParser = true;
foreach (KeyValuePair<string, string> header in Headers)
{
collection.Add(header.Key, parser.Parse(header.Value));
}
return collection;
}
return null;
}
public void ParseResponse(UploadResult result, ResponseInfo responseInfo, CustomUploaderInput input, bool isShortenedURL = false)
2013-11-03 23:53:49 +13:00
{
if (result != null && responseInfo != null)
2013-11-03 23:53:49 +13:00
{
result.ResponseInfo = responseInfo;
2013-11-03 23:53:49 +13:00
2020-09-22 19:16:39 +12:00
if (responseInfo.ResponseText == null)
2013-12-08 05:08:38 +13:00
{
2020-09-22 19:16:39 +12:00
responseInfo.ResponseText = "";
}
2020-09-22 19:16:39 +12:00
CustomUploaderParser parser = new CustomUploaderParser(responseInfo, RegexList);
parser.Filename = input.Filename;
parser.URLEncode = true;
2020-09-22 19:16:39 +12:00
if (responseInfo.IsSuccess)
{
string url;
if (!string.IsNullOrEmpty(URL))
{
url = parser.Parse(URL);
}
else
{
url = parser.ResponseInfo.ResponseText;
}
if (isShortenedURL)
{
result.ShortenedURL = url;
}
else
{
result.URL = url;
}
result.ThumbnailURL = parser.Parse(ThumbnailURL);
result.DeletionURL = parser.Parse(DeletionURL);
2013-12-08 05:08:38 +13:00
}
2020-09-22 19:16:39 +12:00
else
{
if (!string.IsNullOrEmpty(ErrorMessage))
{
string errorMessage = "Error message:\r\n" + parser.Parse(ErrorMessage);
result.Errors.Add(errorMessage);
}
}
2013-11-03 23:53:49 +13:00
}
2013-12-08 05:08:38 +13:00
}
public void CheckBackwardCompatibility()
{
2019-01-09 03:35:17 +13:00
CheckRequestURL();
if (string.IsNullOrEmpty(Version) || Helpers.CompareVersion(Version, "12.3.1") <= 0)
{
2019-01-09 04:57:49 +13:00
if (RequestMethod == HttpMethod.POST)
{
2019-01-09 04:02:21 +13:00
Body = CustomUploaderBody.MultipartFormData;
}
else
{
2019-01-09 04:02:21 +13:00
Body = CustomUploaderBody.None;
if (Arguments != null)
{
2019-02-18 23:08:21 +13:00
if (Parameters == null)
{
Parameters = new Dictionary<string, string>();
}
foreach (KeyValuePair<string, string> pair in Arguments)
{
if (!Parameters.ContainsKey(pair.Key))
{
Parameters.Add(pair.Key, pair.Value);
}
}
Arguments = null;
}
}
if (ResponseType == ResponseType.RedirectionURL)
{
if (string.IsNullOrEmpty(URL))
{
URL = "$responseurl$";
}
URL = URL.Replace("$response$", "$responseurl$");
ThumbnailURL = ThumbnailURL?.Replace("$response$", "$responseurl$");
DeletionURL = DeletionURL?.Replace("$response$", "$responseurl$");
}
else if (ResponseType == ResponseType.Headers)
{
URL = "Response type option is deprecated, please use \\$header:header_name\\$ syntax instead.";
}
else if (ResponseType == ResponseType.LocationHeader)
{
if (string.IsNullOrEmpty(URL))
{
URL = "$header:Location$";
}
URL = URL.Replace("$response$", "$header:Location$");
ThumbnailURL = ThumbnailURL?.Replace("$response$", "$header:Location$");
DeletionURL = DeletionURL?.Replace("$response$", "$header:Location$");
}
ResponseType = ResponseType.Text;
2019-01-09 03:35:17 +13:00
Version = Application.ProductVersion;
}
2019-01-09 03:35:17 +13:00
}
private void CheckRequestURL()
{
if (!string.IsNullOrEmpty(RequestURL))
{
NameValueCollection nvc = URLHelpers.ParseQueryString(RequestURL);
2019-01-09 03:35:17 +13:00
if (nvc != null && nvc.Count > 0)
{
if (Parameters == null)
{
Parameters = new Dictionary<string, string>();
}
foreach (string key in nvc)
{
if (key == null)
{
2019-01-09 09:22:41 +13:00
foreach (string value in nvc.GetValues(key))
{
2019-01-09 09:22:41 +13:00
if (!Parameters.ContainsKey(value))
{
Parameters.Add(value, "");
}
}
}
else if (!Parameters.ContainsKey(key))
{
2019-01-09 09:22:41 +13:00
string value = nvc[key];
Parameters.Add(key, value);
}
}
RequestURL = URLHelpers.RemoveQueryString(RequestURL);
}
}
}
2013-11-03 23:53:49 +13:00
}
}