mirror of
https://github.com/ShareX/ShareX.git
synced 2024-09-30 01:07:21 +13:00
Code refactoring
This commit is contained in:
parent
848f62fde3
commit
5fb4fd9848
13 changed files with 58 additions and 53 deletions
|
@ -152,6 +152,12 @@ public static async Task<string> GetFileNameFromWebServerAsync(string url)
|
||||||
return fileName;
|
return fileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static bool IsSuccessStatusCode(HttpStatusCode statusCode)
|
||||||
|
{
|
||||||
|
int statusCodeNum = (int)statusCode;
|
||||||
|
return statusCodeNum >= 200 && statusCodeNum <= 299;
|
||||||
|
}
|
||||||
|
|
||||||
public static int GetRandomUnusedPort()
|
public static int GetRandomUnusedPort()
|
||||||
{
|
{
|
||||||
TcpListener listener = new TcpListener(IPAddress.Loopback, 0);
|
TcpListener listener = new TcpListener(IPAddress.Loopback, 0);
|
||||||
|
|
|
@ -23,25 +23,27 @@ You should have received a copy of the GNU General Public License
|
||||||
|
|
||||||
#endregion License Information (GPL v3)
|
#endregion License Information (GPL v3)
|
||||||
|
|
||||||
|
using Microsoft.Win32;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
namespace ShareX.UploadersLib
|
namespace ShareX.HelpersLib
|
||||||
{
|
{
|
||||||
public static class MimeTypes
|
public static class MimeTypes
|
||||||
{
|
{
|
||||||
public static readonly string DefaultMimeType = "application/octet-stream";
|
public static readonly string DefaultMimeType = "application/octet-stream";
|
||||||
|
|
||||||
public static string GetMimeType(string ext)
|
public static string GetMimeTypeFromExtension(string extension)
|
||||||
{
|
{
|
||||||
if (!string.IsNullOrEmpty(ext))
|
if (!string.IsNullOrEmpty(extension))
|
||||||
{
|
{
|
||||||
if (ext[0] == '.')
|
if (extension[0] == '.')
|
||||||
{
|
{
|
||||||
ext = ext.Substring(1);
|
extension = extension.Substring(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Mappings.TryGetValue(ext, out string mime))
|
if (Mappings.TryGetValue(extension, out string mime))
|
||||||
{
|
{
|
||||||
return mime;
|
return mime;
|
||||||
}
|
}
|
||||||
|
@ -50,6 +52,33 @@ public static string GetMimeType(string ext)
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static string GetMimeTypeFromFileName(string fileName)
|
||||||
|
{
|
||||||
|
if (!string.IsNullOrEmpty(fileName))
|
||||||
|
{
|
||||||
|
string extension = Path.GetExtension(fileName).ToLowerInvariant();
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(extension))
|
||||||
|
{
|
||||||
|
string mimeType = GetMimeTypeFromExtension(extension);
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(mimeType))
|
||||||
|
{
|
||||||
|
return mimeType;
|
||||||
|
}
|
||||||
|
|
||||||
|
mimeType = RegistryHelpers.GetValueString(extension, "Content Type", RegistryHive.ClassesRoot);
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(mimeType))
|
||||||
|
{
|
||||||
|
return mimeType;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return DefaultMimeType;
|
||||||
|
}
|
||||||
|
|
||||||
// http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
|
// http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
|
||||||
private static Dictionary<string, string> Mappings = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase)
|
private static Dictionary<string, string> Mappings = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase)
|
||||||
{
|
{
|
|
@ -273,7 +273,7 @@ protected UploadResult SendRequestFileRange(string url, Stream data, string file
|
||||||
}
|
}
|
||||||
contentLength = Math.Min(contentLength, data.Length - contentPosition);
|
contentLength = Math.Min(contentLength, data.Length - contentPosition);
|
||||||
|
|
||||||
string contentType = RequestHelpers.GetMimeType(fileName);
|
string contentType = MimeTypes.GetMimeTypeFromFileName(fileName);
|
||||||
|
|
||||||
if (headers == null)
|
if (headers == null)
|
||||||
{
|
{
|
||||||
|
|
|
@ -134,7 +134,7 @@ public override UploadResult Upload(Stream stream, string fileName)
|
||||||
string scope = URLHelpers.CombineURL(credentialDate, region, "s3", "aws4_request");
|
string scope = URLHelpers.CombineURL(credentialDate, region, "s3", "aws4_request");
|
||||||
string credential = URLHelpers.CombineURL(Settings.AccessKeyID, scope);
|
string credential = URLHelpers.CombineURL(Settings.AccessKeyID, scope);
|
||||||
string timeStamp = DateTime.UtcNow.ToString("yyyyMMddTHHmmssZ", CultureInfo.InvariantCulture);
|
string timeStamp = DateTime.UtcNow.ToString("yyyyMMddTHHmmssZ", CultureInfo.InvariantCulture);
|
||||||
string contentType = RequestHelpers.GetMimeType(fileName);
|
string contentType = MimeTypes.GetMimeTypeFromFileName(fileName);
|
||||||
string hashedPayload;
|
string hashedPayload;
|
||||||
|
|
||||||
if (Settings.SignedPayload)
|
if (Settings.SignedPayload)
|
||||||
|
|
|
@ -100,7 +100,7 @@ public override UploadResult Upload(Stream stream, string fileName)
|
||||||
|
|
||||||
OnEarlyURLCopyRequested(resultURL);
|
OnEarlyURLCopyRequested(resultURL);
|
||||||
|
|
||||||
string contentType = RequestHelpers.GetMimeType(fileName);
|
string contentType = MimeTypes.GetMimeTypeFromFileName(fileName);
|
||||||
|
|
||||||
NameValueCollection requestHeaders = new NameValueCollection();
|
NameValueCollection requestHeaders = new NameValueCollection();
|
||||||
requestHeaders["x-ms-date"] = date;
|
requestHeaders["x-ms-date"] = date;
|
||||||
|
|
|
@ -418,7 +418,7 @@ private B2UploadResult B2ApiUploadFile(B2UploadUrl b2UploadUrl, string destinati
|
||||||
["X-Bz-Info-b2-content-disposition"] = URLHelpers.URLEncode(contentDisposition.ToString()),
|
["X-Bz-Info-b2-content-disposition"] = URLHelpers.URLEncode(contentDisposition.ToString()),
|
||||||
};
|
};
|
||||||
|
|
||||||
string contentType = RequestHelpers.GetMimeType(destinationPath);
|
string contentType = MimeTypes.GetMimeTypeFromFileName(destinationPath);
|
||||||
|
|
||||||
using (HttpWebResponse res = GetResponse(HttpMethod.POST, b2UploadUrl.uploadUrl,
|
using (HttpWebResponse res = GetResponse(HttpMethod.POST, b2UploadUrl.uploadUrl,
|
||||||
contentType: contentType, headers: headers, data: file, allowNon2xxResponses: true))
|
contentType: contentType, headers: headers, data: file, allowNon2xxResponses: true))
|
||||||
|
@ -493,7 +493,10 @@ private static bool IsAuthorizedForUpload(B2Authorization auth, string bucketId,
|
||||||
/// <exception cref="IOException">If the response body cannot be read.</exception>
|
/// <exception cref="IOException">If the response body cannot be read.</exception>
|
||||||
private static B2Error ParseB2Error(HttpWebResponse res)
|
private static B2Error ParseB2Error(HttpWebResponse res)
|
||||||
{
|
{
|
||||||
if (RequestHelpers.IsSuccessStatusCode(res.StatusCode)) return null;
|
if (WebHelpers.IsSuccessStatusCode(res.StatusCode))
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|
|
@ -87,7 +87,7 @@ public override UploadResult Upload(Stream stream, string fileName)
|
||||||
}
|
}
|
||||||
else if (uploader.Body == CustomUploaderBody.Binary)
|
else if (uploader.Body == CustomUploaderBody.Binary)
|
||||||
{
|
{
|
||||||
result.Response = SendRequest(uploader.RequestMethod, uploader.GetRequestURL(input), stream, RequestHelpers.GetMimeType(fileName), null,
|
result.Response = SendRequest(uploader.RequestMethod, uploader.GetRequestURL(input), stream, MimeTypes.GetMimeTypeFromFileName(fileName), null,
|
||||||
uploader.GetHeaders(input));
|
uploader.GetHeaders(input));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
|
@ -114,7 +114,7 @@ public override UploadResult Upload(Stream stream, string fileName)
|
||||||
NameValueCollection headers = RequestHelpers.CreateAuthenticationHeader(Username, Password);
|
NameValueCollection headers = RequestHelpers.CreateAuthenticationHeader(Username, Password);
|
||||||
headers["OCS-APIREQUEST"] = "true";
|
headers["OCS-APIREQUEST"] = "true";
|
||||||
|
|
||||||
string response = SendRequest(HttpMethod.PUT, url, stream, RequestHelpers.GetMimeType(fileName), null, headers);
|
string response = SendRequest(HttpMethod.PUT, url, stream, MimeTypes.GetMimeTypeFromFileName(fileName), null, headers);
|
||||||
|
|
||||||
UploadResult result = new UploadResult(response);
|
UploadResult result = new UploadResult(response);
|
||||||
|
|
||||||
|
|
|
@ -76,7 +76,7 @@ public override UploadResult Upload(Stream stream, string fileName)
|
||||||
metaDataReq.Files = new UploadMetadataRequestFile0();
|
metaDataReq.Files = new UploadMetadataRequestFile0();
|
||||||
metaDataReq.Files.File0 = new UploadMetadataRequestFile();
|
metaDataReq.Files.File0 = new UploadMetadataRequestFile();
|
||||||
metaDataReq.Files.File0.FileName = fileName;
|
metaDataReq.Files.File0.FileName = fileName;
|
||||||
metaDataReq.Files.File0.FileType = RequestHelpers.GetMimeType(fileName);
|
metaDataReq.Files.File0.FileType = MimeTypes.GetMimeTypeFromFileName(fileName);
|
||||||
metaDataReq.Files.File0.FileSize = Convert.ToInt32(stream.Length);
|
metaDataReq.Files.File0.FileSize = Convert.ToInt32(stream.Length);
|
||||||
metaDataReq.Removable = Settings.Removable;
|
metaDataReq.Removable = Settings.Removable;
|
||||||
metaDataReq.OneShot = Settings.OneShot;
|
metaDataReq.OneShot = Settings.OneShot;
|
||||||
|
|
|
@ -23,7 +23,6 @@ You should have received a copy of the GNU General Public License
|
||||||
|
|
||||||
#endregion License Information (GPL v3)
|
#endregion License Information (GPL v3)
|
||||||
|
|
||||||
using Microsoft.Win32;
|
|
||||||
using ShareX.HelpersLib;
|
using ShareX.HelpersLib;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
@ -187,14 +186,14 @@ public static byte[] MakeInputContent(string boundary, Dictionary<string, string
|
||||||
|
|
||||||
public static byte[] MakeFileInputContentOpen(string boundary, string fileFormName, string fileName)
|
public static byte[] MakeFileInputContentOpen(string boundary, string fileFormName, string fileName)
|
||||||
{
|
{
|
||||||
string mimeType = GetMimeType(fileName);
|
string mimeType = MimeTypes.GetMimeTypeFromFileName(fileName);
|
||||||
string content = $"--{boundary}\r\nContent-Disposition: form-data; name=\"{fileFormName}\"; filename=\"{fileName}\"\r\nContent-Type: {mimeType}\r\n\r\n";
|
string content = $"--{boundary}\r\nContent-Disposition: form-data; name=\"{fileFormName}\"; filename=\"{fileName}\"\r\nContent-Type: {mimeType}\r\n\r\n";
|
||||||
return Encoding.UTF8.GetBytes(content);
|
return Encoding.UTF8.GetBytes(content);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] MakeRelatedFileInputContentOpen(string boundary, string contentType, string relatedData, string fileName)
|
public static byte[] MakeRelatedFileInputContentOpen(string boundary, string contentType, string relatedData, string fileName)
|
||||||
{
|
{
|
||||||
string mimeType = GetMimeType(fileName);
|
string mimeType = MimeTypes.GetMimeTypeFromFileName(fileName);
|
||||||
string content = $"--{boundary}\r\nContent-Type: {contentType}\r\n\r\n{relatedData}\r\n\r\n";
|
string content = $"--{boundary}\r\nContent-Type: {contentType}\r\n\r\n{relatedData}\r\n\r\n";
|
||||||
content += $"--{boundary}\r\nContent-Type: {mimeType}\r\n\r\n";
|
content += $"--{boundary}\r\nContent-Type: {mimeType}\r\n\r\n";
|
||||||
return Encoding.UTF8.GetBytes(content);
|
return Encoding.UTF8.GetBytes(content);
|
||||||
|
@ -226,38 +225,5 @@ public static NameValueCollection CreateAuthenticationHeader(string username, st
|
||||||
headers["Authorization"] = "Basic " + authorization;
|
headers["Authorization"] = "Basic " + authorization;
|
||||||
return headers;
|
return headers;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string GetMimeType(string fileName)
|
|
||||||
{
|
|
||||||
if (!string.IsNullOrEmpty(fileName))
|
|
||||||
{
|
|
||||||
string ext = Path.GetExtension(fileName).ToLowerInvariant();
|
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(ext))
|
|
||||||
{
|
|
||||||
string mimeType = MimeTypes.GetMimeType(ext);
|
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(mimeType))
|
|
||||||
{
|
|
||||||
return mimeType;
|
|
||||||
}
|
|
||||||
|
|
||||||
mimeType = RegistryHelpers.GetValueString(ext, "Content Type", RegistryHive.ClassesRoot);
|
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(mimeType))
|
|
||||||
{
|
|
||||||
return mimeType;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return MimeTypes.DefaultMimeType;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static bool IsSuccessStatusCode(HttpStatusCode statusCode)
|
|
||||||
{
|
|
||||||
int statusCodeNum = (int)statusCode;
|
|
||||||
return statusCodeNum >= 200 && statusCodeNum <= 299;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -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 ShareX.HelpersLib;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
|
@ -32,7 +33,7 @@ public class ResponseInfo
|
||||||
{
|
{
|
||||||
public HttpStatusCode StatusCode { get; set; }
|
public HttpStatusCode StatusCode { get; set; }
|
||||||
public string StatusDescription { get; set; }
|
public string StatusDescription { get; set; }
|
||||||
public bool IsSuccess => RequestHelpers.IsSuccessStatusCode(StatusCode);
|
public bool IsSuccess => WebHelpers.IsSuccessStatusCode(StatusCode);
|
||||||
public string ResponseURL { get; set; }
|
public string ResponseURL { get; set; }
|
||||||
public WebHeaderCollection Headers { get; set; }
|
public WebHeaderCollection Headers { get; set; }
|
||||||
public string ResponseText { get; set; }
|
public string ResponseText { get; set; }
|
||||||
|
|
|
@ -83,7 +83,7 @@ public override UploadResult Upload(Stream stream, string fileName)
|
||||||
}
|
}
|
||||||
else if (uploader.Body == CustomUploaderBody.Binary)
|
else if (uploader.Body == CustomUploaderBody.Binary)
|
||||||
{
|
{
|
||||||
result.Response = SendRequest(uploader.RequestMethod, uploader.GetRequestURL(input), stream, RequestHelpers.GetMimeType(fileName),
|
result.Response = SendRequest(uploader.RequestMethod, uploader.GetRequestURL(input), stream, MimeTypes.GetMimeTypeFromFileName(fileName),
|
||||||
null, uploader.GetHeaders(input));
|
null, uploader.GetHeaders(input));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
|
@ -112,7 +112,7 @@ public override UploadResult UploadText(string text, string fileName)
|
||||||
byte[] bytes = Encoding.UTF8.GetBytes(text);
|
byte[] bytes = Encoding.UTF8.GetBytes(text);
|
||||||
using (MemoryStream stream = new MemoryStream(bytes))
|
using (MemoryStream stream = new MemoryStream(bytes))
|
||||||
{
|
{
|
||||||
result.Response = SendRequest(uploader.RequestMethod, uploader.GetRequestURL(input), stream, RequestHelpers.GetMimeType(fileName),
|
result.Response = SendRequest(uploader.RequestMethod, uploader.GetRequestURL(input), stream, MimeTypes.GetMimeTypeFromFileName(fileName),
|
||||||
null, uploader.GetHeaders(input));
|
null, uploader.GetHeaders(input));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue