From 9481840244ba0a2d2c4be3bd91828fa2f3684f60 Mon Sep 17 00:00:00 2001 From: Dominic Villemure Date: Sun, 30 Jun 2024 23:40:57 -0400 Subject: [PATCH] Added content type --- .../AzureBlobStorage/Constants/CommonFileNames.cs | 6 +++--- .../AzureBlobStorage/Constants/ContentTypes.cs | 6 ++++++ .../Common/Interfaces/IAzureBlobStorageService.cs | 2 +- .../Users/Commands/UploadBannerPicture.cs | 2 +- .../Users/Commands/UploadProfilePicture.cs | 2 +- .../Users/Commands/UploadWebsiteIcon.cs | 2 +- .../AzureBlob/AzureBlobStorageService.cs | 14 ++++++++++++-- 7 files changed, 25 insertions(+), 9 deletions(-) create mode 100644 src/Application/AzureBlobStorage/Constants/ContentTypes.cs diff --git a/src/Application/AzureBlobStorage/Constants/CommonFileNames.cs b/src/Application/AzureBlobStorage/Constants/CommonFileNames.cs index b6d44aa..8ffc63c 100644 --- a/src/Application/AzureBlobStorage/Constants/CommonFileNames.cs +++ b/src/Application/AzureBlobStorage/Constants/CommonFileNames.cs @@ -2,7 +2,7 @@ public static class CommonFileNames { - public static string ProfilePicture = "profilePicture"; - public static string BannerPicture = "bannerPicture"; - public static string WebsiteIcon = "websiteIcon"; + public static string ProfilePicture = "profilePicture.png"; + public static string BannerPicture = "bannerPicture.png"; + public static string WebsiteIcon = "websiteIcon.png"; } diff --git a/src/Application/AzureBlobStorage/Constants/ContentTypes.cs b/src/Application/AzureBlobStorage/Constants/ContentTypes.cs new file mode 100644 index 0000000..ff3bcf7 --- /dev/null +++ b/src/Application/AzureBlobStorage/Constants/ContentTypes.cs @@ -0,0 +1,6 @@ +namespace Hutopy.Application.AzureBlobStorage.Constants; + +public static class ContentTypes +{ + public static string ImagePng = "image/png"; +} diff --git a/src/Application/Common/Interfaces/IAzureBlobStorageService.cs b/src/Application/Common/Interfaces/IAzureBlobStorageService.cs index c8bdd20..a397ce0 100644 --- a/src/Application/Common/Interfaces/IAzureBlobStorageService.cs +++ b/src/Application/Common/Interfaces/IAzureBlobStorageService.cs @@ -2,6 +2,6 @@ public interface IAzureBlobStorageService { - Task UploadFileAsync(string containerName, string blobName, Stream fileStream); + Task UploadFileAsync(string containerName, string blobName, Stream fileStream, string contentType); Task DownloadFileAsync(string containerName, string blobName); } diff --git a/src/Application/Users/Commands/UploadBannerPicture.cs b/src/Application/Users/Commands/UploadBannerPicture.cs index 66922a6..ae8e783 100644 --- a/src/Application/Users/Commands/UploadBannerPicture.cs +++ b/src/Application/Users/Commands/UploadBannerPicture.cs @@ -17,7 +17,7 @@ public class UploadBannerPictureCommandHandler(IIdentityService identityService, var blobName = $"{currentUserId}/{SubDirectoryNames.Profile}/{CommonFileNames.BannerPicture}"; - var url = await azureBlobStorageService.UploadFileAsync(ContainerNames.Users, blobName, request.BannerPicture); + var url = await azureBlobStorageService.UploadFileAsync(ContainerNames.Users, blobName, request.BannerPicture, ContentTypes.ImagePng); await identityService.UpdateCurrentUserBannerPictureUrlAsync(url); diff --git a/src/Application/Users/Commands/UploadProfilePicture.cs b/src/Application/Users/Commands/UploadProfilePicture.cs index 6fc4714..154ace7 100644 --- a/src/Application/Users/Commands/UploadProfilePicture.cs +++ b/src/Application/Users/Commands/UploadProfilePicture.cs @@ -17,7 +17,7 @@ public class UploadProfilePictureCommandHandler(IIdentityService identityService var blobName = $"{currentUserId}/{SubDirectoryNames.Profile}/{CommonFileNames.ProfilePicture}"; - var url = await azureBlobStorageService.UploadFileAsync(ContainerNames.Users, blobName, request.ProfilePicture); + var url = await azureBlobStorageService.UploadFileAsync(ContainerNames.Users, blobName, request.ProfilePicture, ContentTypes.ImagePng); await identityService.UpdateCurrentUserProfilePictureUrlAsync(url); diff --git a/src/Application/Users/Commands/UploadWebsiteIcon.cs b/src/Application/Users/Commands/UploadWebsiteIcon.cs index 78a83a2..26caa25 100644 --- a/src/Application/Users/Commands/UploadWebsiteIcon.cs +++ b/src/Application/Users/Commands/UploadWebsiteIcon.cs @@ -17,7 +17,7 @@ public class UploadWebsiteIconCommandHandler(IIdentityService identityService, I var blobName = $"{currentUserId}/{SubDirectoryNames.Profile}/{CommonFileNames.WebsiteIcon}"; - var url = await azureBlobStorageService.UploadFileAsync(ContainerNames.Users, blobName, request.WebsiteIcon); + var url = await azureBlobStorageService.UploadFileAsync(ContainerNames.Users, blobName, request.WebsiteIcon, ContentTypes.ImagePng); await identityService.UpdateCurrentUserWebsiteIconUrlAsync(url); diff --git a/src/Infrastructure/AzureBlob/AzureBlobStorageService.cs b/src/Infrastructure/AzureBlob/AzureBlobStorageService.cs index b25521c..18d7856 100644 --- a/src/Infrastructure/AzureBlob/AzureBlobStorageService.cs +++ b/src/Infrastructure/AzureBlob/AzureBlobStorageService.cs @@ -25,8 +25,9 @@ public class AzureBlobStorageService : IAzureBlobStorageService /// The blob name (path within the container, include the file name). /// The name of the container where the file is stored. /// The stream. + /// The content type. /// - public async Task UploadFileAsync(string containerName, string blobName, Stream fileStream) + public async Task UploadFileAsync(string containerName, string blobName, Stream fileStream, string contentType) { // Get a reference to a container var containerClient = _blobServiceClient.GetBlobContainerClient(containerName); @@ -36,9 +37,18 @@ public class AzureBlobStorageService : IAzureBlobStorageService // Get a reference to a blob var blobClient = containerClient.GetBlobClient(blobName); + + // Define the BlobHttpHeaders to include the content type + var blobHttpHeaders = new BlobHttpHeaders + { + ContentType = contentType + }; // Upload the file - await blobClient.UploadAsync(fileStream, true); + await blobClient.UploadAsync(fileStream, new BlobUploadOptions + { + HttpHeaders = blobHttpHeaders + }); // Return the URI of the uploaded blob return blobClient.Uri.ToString();