#67 added blob storage service + endpoints for profile picture. WIP

This commit is contained in:
Dominic Villemure
2024-06-23 20:24:15 -04:00
parent 0a9c3ec94c
commit ab88511f22
15 changed files with 338 additions and 2 deletions

View File

@@ -0,0 +1,6 @@
namespace Hutopy.Application.AzureBlobStorage.Constants;
public static class CommonFileNames
{
public static string ProfilePicture = "profilePicture";
}

View File

@@ -0,0 +1,6 @@
namespace Hutopy.Application.AzureBlobStorage.Constants;
public static class ContainerNames
{
public static string Users = "users";
}

View File

@@ -0,0 +1,7 @@
namespace Hutopy.Application.AzureBlobStorage.Constants;
public static class SubDirectoryNames
{
public static string Profile = "profile";
public static string Posts = "posts";
}

View File

@@ -0,0 +1,7 @@
namespace Hutopy.Application.Common.Interfaces;
public interface IAzureBlobStorageService
{
Task<string> UploadFileAsync(string containerName, string blobName, Stream fileStream);
Task<MemoryStream> DownloadFileAsync(string containerName, string blobName);
}

View File

@@ -0,0 +1,25 @@
using Hutopy.Application.AzureBlobStorage.Constants;
using Hutopy.Application.Common.Interfaces;
namespace Hutopy.Application.Users.Commands;
public class UploadProfilePictureCommand : IRequest<string>
{
public required Stream ProfilePicture { get; init; }
}
public class UploadProfilePictureCommandHandler(IIdentityService identityService, IAzureBlobStorageService azureBlobStorageService) : IRequestHandler<UploadProfilePictureCommand, string>
{
public async Task<string> Handle(UploadProfilePictureCommand request, CancellationToken cancellationToken)
{
var identityUser = await identityService.GetCurrentUserAsync();
var currentUserId = new Guid(identityUser?.Id ?? "").ToString();
var blobName = $"{currentUserId}/{SubDirectoryNames.Profile}/{CommonFileNames.ProfilePicture}";
var url = await azureBlobStorageService.UploadFileAsync(ContainerNames.Users, blobName, request.ProfilePicture);
return url;
}
}

View File

@@ -0,0 +1,23 @@
using Hutopy.Application.AzureBlobStorage.Constants;
using Hutopy.Application.Common.Interfaces;
namespace Hutopy.Application.Users.Queries.GetCurrentUser;
public record GetCurrentUserProfilePictureQuery : IRequest<Stream>;
public class GetCurrentUserProfilePictureQueryHandler(
IIdentityService identityService,
IAzureBlobStorageService azureBlobStorageService
)
: IRequestHandler<GetCurrentUserProfilePictureQuery, Stream>
{
public async Task<Stream> Handle(GetCurrentUserProfilePictureQuery request, CancellationToken cancellationToken)
{
var identityUser = await identityService.GetCurrentUserAsync();
var currentUserId = new Guid(identityUser?.Id ?? "");
var blobName = $"{currentUserId.ToString()}/{SubDirectoryNames.Profile}/{CommonFileNames.ProfilePicture}";
return await azureBlobStorageService.DownloadFileAsync(ContainerNames.Users, blobName);
}
}

View File

@@ -8,7 +8,6 @@ public class UserDto
public string UserName { get; init; } = String.Empty;
public List<UserTransactionDto> UserTransactions { get; init; } = [];
public IList<string> UserRoles { get; init; } = [];
public required decimal TotalBalance { get; init; }
}