mirror of
https://github.com/ShareX/ShareX.git
synced 2024-06-28 03:00:22 +12:00
Added Data URL support for browser extension base64 image uploads
This commit is contained in:
parent
5fb4fd9848
commit
aa51696d72
|
@ -26,10 +26,10 @@
|
|||
using System;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Net.Sockets;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ShareX.HelpersLib
|
||||
|
@ -92,16 +92,7 @@ public static async Task<Bitmap> DownloadImageAsync(string url)
|
|||
{
|
||||
string mediaType = responseMessage.Content.Headers.ContentType.MediaType;
|
||||
|
||||
string[] supportedImageTypes = new string[]
|
||||
{
|
||||
"image/png",
|
||||
"image/jpeg",
|
||||
"image/gif",
|
||||
"image/bmp",
|
||||
"image/tiff"
|
||||
};
|
||||
|
||||
if (supportedImageTypes.Contains(mediaType, StringComparer.OrdinalIgnoreCase))
|
||||
if (MimeTypes.IsImageMimeType(mediaType))
|
||||
{
|
||||
byte[] data = await responseMessage.Content.ReadAsByteArrayAsync();
|
||||
MemoryStream memoryStream = new MemoryStream(data);
|
||||
|
@ -152,6 +143,43 @@ public static async Task<string> GetFileNameFromWebServerAsync(string url)
|
|||
return fileName;
|
||||
}
|
||||
|
||||
// https://en.wikipedia.org/wiki/Data_URI_scheme
|
||||
public static Bitmap DataURLToImage(string url)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(url) && url.StartsWith("data:", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
Match match = Regex.Match(url, @"^data:(?<mediaType>[\w\/]+);base64,(?<data>.+)$", RegexOptions.IgnoreCase);
|
||||
|
||||
if (match.Success)
|
||||
{
|
||||
string mediaType = match.Groups["mediaType"].Value;
|
||||
|
||||
if (MimeTypes.IsImageMimeType(mediaType))
|
||||
{
|
||||
string data = match.Groups["data"].Value;
|
||||
|
||||
if (!string.IsNullOrEmpty(data))
|
||||
{
|
||||
try
|
||||
{
|
||||
byte[] dataBytes = Convert.FromBase64String(data);
|
||||
|
||||
using (MemoryStream ms = new MemoryStream(dataBytes))
|
||||
{
|
||||
return new Bitmap(ms);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static bool IsSuccessStatusCode(HttpStatusCode statusCode)
|
||||
{
|
||||
int statusCodeNum = (int)statusCode;
|
||||
|
|
|
@ -79,8 +79,18 @@ public static string GetMimeTypeFromFileName(string fileName)
|
|||
return DefaultMimeType;
|
||||
}
|
||||
|
||||
public static bool IsImageMimeType(string mimeType)
|
||||
{
|
||||
return !string.IsNullOrEmpty(mimeType) &&
|
||||
(mimeType.Equals(Mappings["png"], StringComparison.OrdinalIgnoreCase) ||
|
||||
mimeType.Equals(Mappings["jpeg"], StringComparison.OrdinalIgnoreCase) ||
|
||||
mimeType.Equals(Mappings["gif"], StringComparison.OrdinalIgnoreCase) ||
|
||||
mimeType.Equals(Mappings["bmp"], StringComparison.OrdinalIgnoreCase) ||
|
||||
mimeType.Equals(Mappings["tiff"], StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
|
||||
// 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.OrdinalIgnoreCase)
|
||||
{
|
||||
{ "123", "application/vnd.lotus-1-2-3" },
|
||||
{ "3dml", "text/vnd.in3d.3dml" },
|
||||
|
|
|
@ -1956,9 +1956,9 @@ public static async Task HandleNativeMessagingInput(string filePath, TaskSetting
|
|||
case NativeMessagingAction.UploadImage:
|
||||
if (!string.IsNullOrEmpty(nativeMessagingInput.URL))
|
||||
{
|
||||
Bitmap bmp = null;
|
||||
Bitmap bmp = WebHelpers.DataURLToImage(nativeMessagingInput.URL);
|
||||
|
||||
if (taskSettings.AdvancedSettings.ProcessImagesDuringExtensionUpload)
|
||||
if (bmp == null && taskSettings.AdvancedSettings.ProcessImagesDuringExtensionUpload)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue