ShareX/ShareX.UploadersLib/FileUploaders/AzureStorage.cs

216 lines
8.4 KiB
C#
Raw Normal View History

#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2024-01-03 12:57:14 +13:00
Copyright (c) 2007-2024 ShareX Team
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)
using ShareX.HelpersLib;
2017-03-30 10:03:08 +13:00
using ShareX.UploadersLib.Properties;
2017-01-27 00:41:49 +13:00
using System;
using System.Collections.Specialized;
2017-01-27 00:41:49 +13:00
using System.Drawing;
using System.Globalization;
2017-01-27 00:41:49 +13:00
using System.IO;
using System.Security.Cryptography;
2017-01-27 00:41:49 +13:00
using System.Text;
using System.Windows.Forms;
namespace ShareX.UploadersLib.FileUploaders
{
2017-03-11 00:28:23 +13:00
public class AzureStorageUploaderService : FileUploaderService
{
public override FileDestination EnumValue { get; } = FileDestination.AzureStorage;
public override Image ServiceImage => Resources.AzureStorage;
public override bool CheckConfig(UploadersConfig config)
{
return !string.IsNullOrEmpty(config.AzureStorageAccountName) &&
!string.IsNullOrEmpty(config.AzureStorageAccountAccessKey) &&
!string.IsNullOrEmpty(config.AzureStorageContainer);
}
public override GenericUploader CreateUploader(UploadersConfig config, TaskReferenceHelper taskInfo)
{
return new AzureStorage(config.AzureStorageAccountName, config.AzureStorageAccountAccessKey, config.AzureStorageContainer,
config.AzureStorageEnvironment, config.AzureStorageCustomDomain, config.AzureStorageUploadPath, config.AzureStorageCacheControl);
2017-03-11 00:28:23 +13:00
}
public override TabPage GetUploadersConfigTabPage(UploadersConfigForm form) => form.tpAzureStorage;
}
2017-01-27 00:41:49 +13:00
public sealed class AzureStorage : FileUploader
{
2017-03-20 12:53:32 +13:00
private const string APIVersion = "2016-05-31";
2017-01-27 00:41:49 +13:00
2017-03-11 00:28:23 +13:00
public string AzureStorageAccountName { get; private set; }
public string AzureStorageAccountAccessKey { get; private set; }
public string AzureStorageContainer { get; private set; }
public string AzureStorageEnvironment { get; private set; }
2017-07-07 12:58:14 +12:00
public string AzureStorageCustomDomain { get; private set; }
public string AzureStorageUploadPath { get; private set; }
public string AzureStorageCacheControl { get; private set; }
2017-03-11 00:28:23 +13:00
public AzureStorage(string azureStorageAccountName, string azureStorageAccessKey, string azureStorageContainer, string azureStorageEnvironment,
string customDomain, string uploadPath, string cacheControl)
2017-01-27 00:41:49 +13:00
{
2017-03-11 00:28:23 +13:00
AzureStorageAccountName = azureStorageAccountName;
AzureStorageAccountAccessKey = azureStorageAccessKey;
AzureStorageContainer = azureStorageContainer;
AzureStorageEnvironment = (!string.IsNullOrEmpty(azureStorageEnvironment)) ? azureStorageEnvironment : "blob.core.windows.net";
2017-07-07 12:58:14 +12:00
AzureStorageCustomDomain = customDomain;
AzureStorageUploadPath = uploadPath;
AzureStorageCacheControl = cacheControl;
2017-01-27 00:41:49 +13:00
}
public override UploadResult Upload(Stream stream, string fileName)
{
2017-03-11 00:28:23 +13:00
if (string.IsNullOrEmpty(AzureStorageAccountName)) Errors.Add("'Account Name' must not be empty");
if (string.IsNullOrEmpty(AzureStorageAccountAccessKey)) Errors.Add("'Access key' must not be empty");
if (string.IsNullOrEmpty(AzureStorageContainer)) Errors.Add("'Container' must not be empty");
2017-01-27 00:41:49 +13:00
if (IsError)
{
return null;
}
2017-03-11 00:28:23 +13:00
string date = DateTime.UtcNow.ToString("R", CultureInfo.InvariantCulture);
2018-06-20 07:16:11 +12:00
string uploadPath = GetUploadPath(fileName);
string requestURL = GenerateURL(uploadPath, true);
string resultURL = GenerateURL(uploadPath);
OnEarlyURLCopyRequested(resultURL);
2024-03-03 05:12:41 +13:00
string contentType = MimeTypes.GetMimeTypeFromFileName(fileName);
2017-03-11 00:28:23 +13:00
NameValueCollection requestHeaders = new NameValueCollection();
requestHeaders["x-ms-date"] = date;
2017-03-20 12:53:32 +13:00
requestHeaders["x-ms-version"] = APIVersion;
2017-03-11 00:28:23 +13:00
requestHeaders["x-ms-blob-type"] = "BlockBlob";
2023-03-28 19:56:14 +13:00
string canonicalizedHeaders = $"x-ms-blob-type:BlockBlob\nx-ms-date:{date}\nx-ms-version:{APIVersion}\n";
if (!string.IsNullOrEmpty(AzureStorageCacheControl))
{
requestHeaders["x-ms-blob-cache-control"] = AzureStorageCacheControl;
canonicalizedHeaders = $"x-ms-blob-cache-control:{AzureStorageCacheControl}\n{canonicalizedHeaders}";
}
2018-06-22 01:20:58 +12:00
string canonicalizedResource = $"/{AzureStorageAccountName}/{AzureStorageContainer}/{uploadPath}";
2017-03-11 00:28:23 +13:00
string stringToSign = GenerateStringToSign(canonicalizedHeaders, canonicalizedResource, stream.Length.ToString(), contentType);
2017-03-11 00:28:23 +13:00
requestHeaders["Authorization"] = $"SharedKey {AzureStorageAccountName}:{stringToSign}";
SendRequest(HttpMethod.PUT, requestURL, stream, contentType, null, requestHeaders);
if (LastResponseInfo != null && LastResponseInfo.IsSuccess)
{
return new UploadResult
{
IsSuccess = true,
URL = resultURL
};
}
2018-06-15 22:12:23 +12:00
Errors.Add("Upload failed.");
return null;
}
private string GenerateStringToSign(string canonicalizedHeaders, string canonicalizedResource, string contentLength = "", string contentType = "")
{
2017-03-11 00:28:23 +13:00
string stringToSign = "PUT" + "\n" +
"\n" +
"\n" +
2017-03-11 00:28:23 +13:00
(contentLength ?? "") + "\n" +
"\n" +
2017-03-11 00:28:23 +13:00
(contentType ?? "") + "\n" +
"\n" +
"\n" +
"\n" +
"\n" +
"\n" +
"\n" +
canonicalizedHeaders +
canonicalizedResource;
2017-03-11 00:28:23 +13:00
return HashRequest(stringToSign);
2017-01-27 00:41:49 +13:00
}
2017-03-11 00:28:23 +13:00
private string HashRequest(string stringToSign)
2017-01-27 00:41:49 +13:00
{
2017-03-11 00:28:23 +13:00
string hashedString;
2017-01-27 00:41:49 +13:00
2017-03-11 00:28:23 +13:00
using (HashAlgorithm hashAlgorithm = new HMACSHA256(Convert.FromBase64String(AzureStorageAccountAccessKey)))
{
byte[] messageBuffer = Encoding.UTF8.GetBytes(stringToSign);
hashedString = Convert.ToBase64String(hashAlgorithm.ComputeHash(messageBuffer));
}
2017-01-27 00:41:49 +13:00
2017-03-11 00:28:23 +13:00
return hashedString;
}
private string GetUploadPath(string fileName)
{
string uploadPath;
if (!string.IsNullOrEmpty(AzureStorageUploadPath))
{
string path = NameParser.Parse(NameParserType.FilePath, AzureStorageUploadPath.Trim('/'));
uploadPath = URLHelpers.CombineURL(path, fileName);
}
else
{
uploadPath = fileName;
}
2018-06-22 03:20:48 +12:00
return Uri.EscapeUriString(uploadPath);
}
2018-06-20 07:16:11 +12:00
2018-06-22 01:20:58 +12:00
public string GenerateURL(string uploadPath, bool isRequest = false)
2018-06-20 07:16:11 +12:00
{
string url;
2018-06-22 01:20:58 +12:00
if (!isRequest && !string.IsNullOrEmpty(AzureStorageCustomDomain))
2018-06-20 07:16:11 +12:00
{
2018-06-22 01:20:58 +12:00
url = URLHelpers.CombineURL(AzureStorageCustomDomain, uploadPath);
url = URLHelpers.FixPrefix(url);
2018-06-20 07:16:11 +12:00
}
2018-06-22 01:20:58 +12:00
else if (!isRequest && AzureStorageContainer == "$root")
2018-06-20 07:16:11 +12:00
{
url = $"https://{AzureStorageAccountName}.{AzureStorageEnvironment}/{uploadPath}";
}
else
{
url = $"https://{AzureStorageAccountName}.{AzureStorageEnvironment}/{AzureStorageContainer}/{uploadPath}";
}
return url;
}
public string GetPreviewURL()
{
string uploadPath = GetUploadPath("example.png");
2018-06-22 01:20:58 +12:00
return GenerateURL(uploadPath);
2018-06-20 07:16:11 +12:00
}
2017-01-27 00:41:49 +13:00
}
}