Adds multiple media urls for content

This commit is contained in:
Jonathan Bourdon
2024-07-31 17:38:58 -04:00
parent 042fd53463
commit bbcc7a8a33
19 changed files with 319 additions and 111 deletions

View File

@@ -3,4 +3,5 @@
public static class ContainerNames
{
public static string Users = "users";
public static string Creators = "creators";
}

View File

@@ -3,5 +3,5 @@
public static class SubDirectoryNames
{
public static string Profile = "profile";
public static string Posts = "posts";
public static string Contents = "contents";
}

View File

@@ -2,6 +2,6 @@
public interface IAzureBlobStorageService
{
Task<string> UploadFileAsync(string containerName, string blobName, Stream fileStream, string contentType);
Task<MemoryStream> DownloadFileAsync(string containerName, string blobName);
Task<string> UploadFileAsync(string containerName, string blobName, MemoryStream memoryStream, string contentType, CancellationToken ct = default);
Task<MemoryStream> DownloadFileAsync(string containerName, string blobName, CancellationToken ct = default);
}

View File

@@ -10,7 +10,7 @@ namespace Hutopy.Application.Users.Commands;
/// </summary>
public class UploadBannerPictureCommand : IRequest<IResult>
{
public required Stream BannerPicture { get; init; }
public required MemoryStream BannerPicture { get; init; }
public string BannerPictureUrl { get; init; } = string.Empty;
}
@@ -32,7 +32,12 @@ public class UploadBannerPictureCommandHandler(IHttpContextAccessor contextAcces
var blobName = $"{currentUserId}/{SubDirectoryNames.Profile}/{CommonFileNames.BannerPicture}";
var url = await azureBlobStorageService.UploadFileAsync(ContainerNames.Users, blobName, request.BannerPicture, contentType);
var url = await azureBlobStorageService.UploadFileAsync(
ContainerNames.Users,
blobName,
request.BannerPicture,
contentType,
cancellationToken);
await identityService.UpdateCurrentUserBannerPictureUrlAsync(url);

View File

@@ -10,13 +10,13 @@ namespace Hutopy.Application.Users.Commands;
/// </summary>
public class UploadProfilePictureCommand : IRequest<IResult>
{
public required Stream ProfilePicture { get; init; }
public required MemoryStream ProfilePicture { get; init; }
public string ProfilePictureUrl { get; init; } = string.Empty;
}
public class UploadProfilePictureCommandHandler(IHttpContextAccessor contextAccessor, IIdentityService identityService, IAzureBlobStorageService azureBlobStorageService) : IRequestHandler<UploadProfilePictureCommand, IResult>
{
public async Task<IResult> Handle(UploadProfilePictureCommand request, CancellationToken cancellationToken)
public async Task<IResult> Handle(UploadProfilePictureCommand request, CancellationToken ct)
{
// If an url to the picture is provided, use it right away and don't upload anything.
if (!string.IsNullOrEmpty(request.ProfilePictureUrl))
@@ -32,7 +32,12 @@ public class UploadProfilePictureCommandHandler(IHttpContextAccessor contextAcce
var blobName = $"{currentUserId}/{SubDirectoryNames.Profile}/{CommonFileNames.ProfilePicture}";
var url = await azureBlobStorageService.UploadFileAsync(ContainerNames.Users, blobName, request.ProfilePicture, contentType);
var url = await azureBlobStorageService.UploadFileAsync(
ContainerNames.Users,
blobName,
request.ProfilePicture,
contentType,
ct);
await identityService.UpdateCurrentUserProfilePictureUrlAsync(url);

View File

@@ -10,14 +10,17 @@ namespace Hutopy.Application.Users.Commands;
/// </summary>
public class UploadWebsiteIconCommand : IRequest<IResult>
{
public required Stream WebsiteIcon { get; init; }
public required MemoryStream WebsiteIcon { get; init; }
public string WebsitePictureUrl { get; init; } = string.Empty;
}
public class UploadWebsiteIconCommandHandler(IHttpContextAccessor contextAccessor, IIdentityService identityService, IAzureBlobStorageService azureBlobStorageService) : IRequestHandler<UploadWebsiteIconCommand, IResult>
public class UploadWebsiteIconCommandHandler(
IHttpContextAccessor contextAccessor,
IIdentityService identityService,
IAzureBlobStorageService azureBlobStorageService) : IRequestHandler<UploadWebsiteIconCommand, IResult>
{
public async Task<IResult> Handle(UploadWebsiteIconCommand request, CancellationToken cancellationToken)
public async Task<IResult> Handle(UploadWebsiteIconCommand request, CancellationToken ct)
{
// If an url to the picture is provided, use it right away and don't upload anything.
if (!string.IsNullOrEmpty(request.WebsitePictureUrl))
@@ -25,19 +28,23 @@ public class UploadWebsiteIconCommandHandler(IHttpContextAccessor contextAccesso
await identityService.UpdateCurrentUserWebsiteIconUrlAsync(request.WebsitePictureUrl);
return Results.Ok(request.WebsitePictureUrl);
}
var contentType = contextAccessor.EnsureContentType();
var identityUser = await identityService.GetCurrentUserAsync();
var currentUserId = new Guid(identityUser?.Id ?? "").ToString();
var blobName = $"{currentUserId}/{SubDirectoryNames.Profile}/{CommonFileNames.WebsiteIcon}";
var url = await azureBlobStorageService.UploadFileAsync(ContainerNames.Users, blobName, request.WebsiteIcon, contentType);
var url = await azureBlobStorageService.UploadFileAsync(
ContainerNames.Users,
blobName,
request.WebsiteIcon,
contentType,
ct);
await identityService.UpdateCurrentUserWebsiteIconUrlAsync(url);
return Results.Ok(request.WebsitePictureUrl);
}
}