mirror of https://github.com/ShareX/ShareX.git
Code refactoring
This commit is contained in:
parent
848f62fde3
commit
5fb4fd9848
|
@ -152,6 +152,12 @@ namespace ShareX.HelpersLib
|
|||
return fileName;
|
||||
}
|
||||
|
||||
public static bool IsSuccessStatusCode(HttpStatusCode statusCode)
|
||||
{
|
||||
int statusCodeNum = (int)statusCode;
|
||||
return statusCodeNum >= 200 && statusCodeNum <= 299;
|
||||
}
|
||||
|
||||
public static int GetRandomUnusedPort()
|
||||
{
|
||||
TcpListener listener = new TcpListener(IPAddress.Loopback, 0);
|
||||
|
|
|
@ -23,25 +23,27 @@
|
|||
|
||||
#endregion License Information (GPL v3)
|
||||
|
||||
using Microsoft.Win32;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace ShareX.UploadersLib
|
||||
namespace ShareX.HelpersLib
|
||||
{
|
||||
public static class MimeTypes
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
@ -50,6 +52,33 @@ namespace ShareX.UploadersLib
|
|||
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
|
||||
private static Dictionary<string, string> Mappings = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase)
|
||||
{
|
|
@ -273,7 +273,7 @@ namespace ShareX.UploadersLib
|
|||
}
|
||||
contentLength = Math.Min(contentLength, data.Length - contentPosition);
|
||||
|
||||
string contentType = RequestHelpers.GetMimeType(fileName);
|
||||
string contentType = MimeTypes.GetMimeTypeFromFileName(fileName);
|
||||
|
||||
if (headers == null)
|
||||
{
|
||||
|
|
|
@ -134,7 +134,7 @@ namespace ShareX.UploadersLib.FileUploaders
|
|||
string scope = URLHelpers.CombineURL(credentialDate, region, "s3", "aws4_request");
|
||||
string credential = URLHelpers.CombineURL(Settings.AccessKeyID, scope);
|
||||
string timeStamp = DateTime.UtcNow.ToString("yyyyMMddTHHmmssZ", CultureInfo.InvariantCulture);
|
||||
string contentType = RequestHelpers.GetMimeType(fileName);
|
||||
string contentType = MimeTypes.GetMimeTypeFromFileName(fileName);
|
||||
string hashedPayload;
|
||||
|
||||
if (Settings.SignedPayload)
|
||||
|
|
|
@ -100,7 +100,7 @@ namespace ShareX.UploadersLib.FileUploaders
|
|||
|
||||
OnEarlyURLCopyRequested(resultURL);
|
||||
|
||||
string contentType = RequestHelpers.GetMimeType(fileName);
|
||||
string contentType = MimeTypes.GetMimeTypeFromFileName(fileName);
|
||||
|
||||
NameValueCollection requestHeaders = new NameValueCollection();
|
||||
requestHeaders["x-ms-date"] = date;
|
||||
|
|
|
@ -418,7 +418,7 @@ namespace ShareX.UploadersLib.FileUploaders
|
|||
["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,
|
||||
contentType: contentType, headers: headers, data: file, allowNon2xxResponses: true))
|
||||
|
@ -493,7 +493,10 @@ namespace ShareX.UploadersLib.FileUploaders
|
|||
/// <exception cref="IOException">If the response body cannot be read.</exception>
|
||||
private static B2Error ParseB2Error(HttpWebResponse res)
|
||||
{
|
||||
if (RequestHelpers.IsSuccessStatusCode(res.StatusCode)) return null;
|
||||
if (WebHelpers.IsSuccessStatusCode(res.StatusCode))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
|
|
|
@ -87,7 +87,7 @@ namespace ShareX.UploadersLib.FileUploaders
|
|||
}
|
||||
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));
|
||||
}
|
||||
else
|
||||
|
|
|
@ -114,7 +114,7 @@ namespace ShareX.UploadersLib.FileUploaders
|
|||
NameValueCollection headers = RequestHelpers.CreateAuthenticationHeader(Username, Password);
|
||||
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);
|
||||
|
||||
|
|
|
@ -76,7 +76,7 @@ namespace ShareX.UploadersLib.FileUploaders
|
|||
metaDataReq.Files = new UploadMetadataRequestFile0();
|
||||
metaDataReq.Files.File0 = new UploadMetadataRequestFile();
|
||||
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.Removable = Settings.Removable;
|
||||
metaDataReq.OneShot = Settings.OneShot;
|
||||
|
|
|
@ -23,7 +23,6 @@
|
|||
|
||||
#endregion License Information (GPL v3)
|
||||
|
||||
using Microsoft.Win32;
|
||||
using ShareX.HelpersLib;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
@ -187,14 +186,14 @@ namespace ShareX.UploadersLib
|
|||
|
||||
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";
|
||||
return Encoding.UTF8.GetBytes(content);
|
||||
}
|
||||
|
||||
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";
|
||||
content += $"--{boundary}\r\nContent-Type: {mimeType}\r\n\r\n";
|
||||
return Encoding.UTF8.GetBytes(content);
|
||||
|
@ -226,38 +225,5 @@ namespace ShareX.UploadersLib
|
|||
headers["Authorization"] = "Basic " + authorization;
|
||||
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 @@
|
|||
|
||||
#endregion License Information (GPL v3)
|
||||
|
||||
using ShareX.HelpersLib;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
|
||||
|
@ -32,7 +33,7 @@ namespace ShareX.UploadersLib
|
|||
{
|
||||
public HttpStatusCode StatusCode { 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 WebHeaderCollection Headers { get; set; }
|
||||
public string ResponseText { get; set; }
|
||||
|
|
|
@ -83,7 +83,7 @@ namespace ShareX.UploadersLib.ImageUploaders
|
|||
}
|
||||
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));
|
||||
}
|
||||
else
|
||||
|
|
|
@ -112,7 +112,7 @@ namespace ShareX.UploadersLib.TextUploaders
|
|||
byte[] bytes = Encoding.UTF8.GetBytes(text);
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue