mirror of
https://github.com/ShareX/ShareX.git
synced 2024-10-01 09:47:22 +13:00
More Uploader.cs refactoring
This commit is contained in:
parent
09c5c06eab
commit
e7d99c6dba
4 changed files with 36 additions and 80 deletions
|
@ -38,27 +38,26 @@ namespace ShareX.UploadersLib
|
|||
{
|
||||
public class Uploader
|
||||
{
|
||||
protected const string UserAgent = "ShareX";
|
||||
protected const string ContentTypeMultipartFormData = "multipart/form-data";
|
||||
protected const string ContentTypeJSON = "application/json";
|
||||
protected const string ContentTypeURLEncoded = "application/x-www-form-urlencoded";
|
||||
protected const string ContentTypeOctetStream = "application/octet-stream";
|
||||
|
||||
private const string UserAgent = "ShareX";
|
||||
|
||||
public delegate void ProgressEventHandler(ProgressManager progress);
|
||||
public event ProgressEventHandler ProgressChanged;
|
||||
|
||||
public event Action<string> EarlyURLCopyRequested;
|
||||
|
||||
public List<string> Errors { get; private set; }
|
||||
public bool IsUploading { get; protected set; }
|
||||
public int BufferSize { get; set; }
|
||||
public bool AllowReportProgress { get; protected set; }
|
||||
public bool WebExceptionReturnResponse { get; protected set; }
|
||||
public bool WebExceptionThrow { get; protected set; }
|
||||
public bool StopUploadRequested { get; protected set; }
|
||||
|
||||
public List<string> Errors { get; private set; }
|
||||
public bool IsError => !StopUploadRequested && Errors != null && Errors.Count > 0;
|
||||
public int BufferSize { get; set; }
|
||||
|
||||
protected bool StopUploadRequested { get; set; }
|
||||
protected bool AllowReportProgress { get; set; }
|
||||
protected bool WebExceptionReturnResponse { get; set; }
|
||||
protected bool WebExceptionThrow { get; set; }
|
||||
|
||||
private HttpWebRequest currentRequest;
|
||||
|
||||
|
@ -126,7 +125,7 @@ protected string SendRequest(HttpMethod method, string url, Dictionary<string, s
|
|||
|
||||
try
|
||||
{
|
||||
if (method == HttpMethod.POST) // Multipart form data
|
||||
if (method == HttpMethod.POST)
|
||||
{
|
||||
response = SendRequestMultiPart(url, args, headers, cookies);
|
||||
}
|
||||
|
@ -168,6 +167,28 @@ protected string SendRequest(HttpMethod method, string url, Stream data, string
|
|||
}
|
||||
}
|
||||
|
||||
protected string SendRequestURLEncoded(HttpMethod method, string url, Dictionary<string, string> args, NameValueCollection headers = null, CookieCollection cookies = null,
|
||||
ResponseType responseType = ResponseType.Text)
|
||||
{
|
||||
string query = CreateQuery(args);
|
||||
|
||||
return SendRequest(method, url, query, ContentTypeURLEncoded, args, headers, cookies, responseType);
|
||||
}
|
||||
|
||||
protected NameValueCollection SendRequestGetHeaders(HttpMethod method, string url, Stream data, string contentType, Dictionary<string, string> args,
|
||||
NameValueCollection headers = null, CookieCollection cookies = null)
|
||||
{
|
||||
using (HttpWebResponse response = GetResponse(method, url, data, contentType, null, headers, cookies))
|
||||
{
|
||||
if (response != null)
|
||||
{
|
||||
return response.Headers;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
protected bool SendRequestDownload(HttpMethod method, string url, Stream downloadStream, Dictionary<string, string> args = null,
|
||||
NameValueCollection headers = null, CookieCollection cookies = null, string contentType = null)
|
||||
{
|
||||
|
@ -183,34 +204,6 @@ protected bool SendRequestDownload(HttpMethod method, string url, Stream downloa
|
|||
return false;
|
||||
}
|
||||
|
||||
protected string SendRequestURLEncoded(HttpMethod method, string url, Dictionary<string, string> args, NameValueCollection headers = null, CookieCollection cookies = null,
|
||||
ResponseType responseType = ResponseType.Text)
|
||||
{
|
||||
string query = CreateQuery(args);
|
||||
byte[] data = Encoding.UTF8.GetBytes(query);
|
||||
|
||||
using (MemoryStream stream = new MemoryStream())
|
||||
{
|
||||
stream.Write(data, 0, data.Length);
|
||||
|
||||
return SendRequest(method, url, stream, ContentTypeURLEncoded, null, headers, cookies, responseType);
|
||||
}
|
||||
}
|
||||
|
||||
protected NameValueCollection SendRequestGetHeaders(HttpMethod method, string url, Stream data, string contentType, Dictionary<string, string> args,
|
||||
NameValueCollection headers = null, CookieCollection cookies = null)
|
||||
{
|
||||
using (HttpWebResponse response = GetResponse(method, url, data, contentType, null, headers, cookies))
|
||||
{
|
||||
if (response != null)
|
||||
{
|
||||
return response.Headers;
|
||||
}
|
||||
|
||||
return new NameValueCollection();
|
||||
}
|
||||
}
|
||||
|
||||
private HttpWebResponse SendRequestMultiPart(string url, Dictionary<string, string> args, NameValueCollection headers = null, CookieCollection cookies = null)
|
||||
{
|
||||
string boundary = CreateBoundary();
|
||||
|
@ -524,7 +517,7 @@ private string ResponseToString(WebResponse response, ResponseType responseType
|
|||
return null;
|
||||
}
|
||||
|
||||
protected string CreateQuery(Dictionary<string, string> args)
|
||||
private string CreateQuery(Dictionary<string, string> args)
|
||||
{
|
||||
if (args != null && args.Count > 0)
|
||||
{
|
||||
|
@ -546,38 +539,6 @@ protected string CreateQuery(string url, Dictionary<string, string> args)
|
|||
return url;
|
||||
}
|
||||
|
||||
protected string CreateQuery(NameValueCollection args)
|
||||
{
|
||||
if (args != null && args.Count > 0)
|
||||
{
|
||||
List<string> commands = new List<string>();
|
||||
|
||||
foreach (string key in args.AllKeys)
|
||||
{
|
||||
string[] values = args.GetValues(key);
|
||||
string isArray = values.Length > 1 ? "[]" : "";
|
||||
|
||||
commands.AddRange(values.Select(value => key + isArray + "=" + HttpUtility.UrlEncode(value)));
|
||||
}
|
||||
|
||||
return string.Join("&", commands.ToArray());
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
protected string CreateQuery(string url, NameValueCollection args)
|
||||
{
|
||||
string query = CreateQuery(args);
|
||||
|
||||
if (!string.IsNullOrEmpty(query))
|
||||
{
|
||||
return url + "?" + query;
|
||||
}
|
||||
|
||||
return url;
|
||||
}
|
||||
|
||||
protected NameValueCollection CreateAuthenticationHeader(string username, string password)
|
||||
{
|
||||
string authInfo = username + ":" + password;
|
||||
|
|
|
@ -200,7 +200,7 @@ public override UploadResult Upload(Stream stream, string fileName)
|
|||
putRequest.Headers["x-amz-storage-class"] = GetObjectStorageClass();
|
||||
|
||||
NameValueCollection responseHeaders = SendRequestGetHeaders(HttpMethod.PUT, client.GetPreSignedURL(putRequest), stream, Helpers.GetMimeType(fileName), null, requestHeaders);
|
||||
if (responseHeaders.Count == 0)
|
||||
if (responseHeaders == null || responseHeaders.Count == 0)
|
||||
{
|
||||
Errors.Add("Upload to Amazon S3 failed. Check your access credentials.");
|
||||
return null;
|
||||
|
|
|
@ -84,8 +84,7 @@ private void TranscodeFile(string key, UploadResult result)
|
|||
if (NoResize) args.Add("noResize", "true");
|
||||
if (IgnoreExisting) args.Add("noMd5", "true");
|
||||
|
||||
string url = CreateQuery("https://upload.gfycat.com/transcodeRelease/" + key, args);
|
||||
string transcodeJson = SendRequest(HttpMethod.GET, url);
|
||||
string transcodeJson = SendRequest(HttpMethod.GET, "https://upload.gfycat.com/transcodeRelease/" + key, args);
|
||||
GfycatTranscodeResponse transcodeResponse = JsonConvert.DeserializeObject<GfycatTranscodeResponse>(transcodeJson);
|
||||
|
||||
if (transcodeResponse.IsOk)
|
||||
|
|
|
@ -223,9 +223,7 @@ public string CreateShareableLink(string id, OneDriveLinkType linkType = OneDriv
|
|||
break;
|
||||
}
|
||||
|
||||
string url = CreateQuery(string.Format("https://apis.live.net/v5.0/{0}/{1}", id, linkTypeValue), args);
|
||||
|
||||
string response = SendRequest(HttpMethod.GET, url);
|
||||
string response = SendRequest(HttpMethod.GET, $"https://apis.live.net/v5.0/{id}/{linkTypeValue}", args);
|
||||
|
||||
OneDriveShareableLinkInfo shareableLinkInfo = JsonConvert.DeserializeObject<OneDriveShareableLinkInfo>(response);
|
||||
|
||||
|
@ -246,9 +244,7 @@ public OneDrivePathInfo GetPathInfo(string path)
|
|||
|
||||
if (!path.EndsWith("files")) path += "/files";
|
||||
|
||||
string url = CreateQuery(URLHelpers.CombineURL("https://apis.live.net/v5.0", path), args);
|
||||
|
||||
string response = SendRequest(HttpMethod.GET, url);
|
||||
string response = SendRequest(HttpMethod.GET, URLHelpers.CombineURL("https://apis.live.net/v5.0", path), args);
|
||||
|
||||
if (response != null)
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue