using Hutopy.Application.AzureBlobStorage.Constants;
using Hutopy.Application.Common.Interfaces;
namespace Hutopy.Application.Users.Commands;
///
/// Upload a profile picture. If the user has the url already, set the ProfilePictureUrl in the user only without upload.
///
public class UploadProfilePictureCommand : IRequest
{
public required Stream ProfilePicture { get; init; }
public string ProfilePictureUrl { get; init; } = string.Empty;
}
public class UploadProfilePictureCommandHandler(IIdentityService identityService, IAzureBlobStorageService azureBlobStorageService) : IRequestHandler
{
public async Task Handle(UploadProfilePictureCommand request, CancellationToken cancellationToken)
{
if (!string.IsNullOrEmpty(request.ProfilePictureUrl))
{
await identityService.UpdateCurrentUserProfilePictureUrlAsync(request.ProfilePictureUrl);
return request.ProfilePictureUrl;
}
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, ContentTypes.ImagePng);
await identityService.UpdateCurrentUserProfilePictureUrlAsync(url);
return url;
}
}