Uploads
This commit is contained in:
@@ -10,6 +10,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Ardalis.Result" Version="10.1.0" />
|
<PackageReference Include="Ardalis.Result" Version="10.1.0" />
|
||||||
<PackageReference Include="DispatchR.Mediator" Version="2.1.1" />
|
<PackageReference Include="DispatchR.Mediator" Version="2.1.1" />
|
||||||
|
<PackageReference Include="MassTransit" Version="9.0.1" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.5" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.5" />
|
||||||
<PackageReference Include="X.PagedList" Version="10.5.9" />
|
<PackageReference Include="X.PagedList" Version="10.5.9" />
|
||||||
<PackageReference Include="X.PagedList.EF" Version="10.5.9" />
|
<PackageReference Include="X.PagedList.EF" Version="10.5.9" />
|
||||||
@@ -20,7 +21,6 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Folder Include="Configuration\" />
|
|
||||||
<Folder Include="Contracts\Application\" />
|
<Folder Include="Contracts\Application\" />
|
||||||
<Folder Include="PlayLists\Commands\CreatePlayList\" />
|
<Folder Include="PlayLists\Commands\CreatePlayList\" />
|
||||||
<Folder Include="PlayLists\Commands\DeletePlayList\" />
|
<Folder Include="PlayLists\Commands\DeletePlayList\" />
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
namespace Musify.Application.Configuration
|
||||||
|
{
|
||||||
|
public class PlayListConfiguration
|
||||||
|
{
|
||||||
|
public PlayListRoutes Routes { get; set; } = new PlayListRoutes();
|
||||||
|
|
||||||
|
public int SmallPictureWidth { get; set; } = 128;
|
||||||
|
|
||||||
|
public int SmallPictureHeight { get; set; } = 128;
|
||||||
|
|
||||||
|
public int MediumPictureWidth { get; private set; } = 256;
|
||||||
|
|
||||||
|
public int MediumPictureHeight { get; private set; } = 256;
|
||||||
|
|
||||||
|
public int LargePictureWidth { get; private set; } = 512;
|
||||||
|
|
||||||
|
public float LargePictureHeight { get; private set; } = 512;
|
||||||
|
}
|
||||||
|
|
||||||
|
public class PlayListRoutes
|
||||||
|
{
|
||||||
|
public string ParentFolder { get; set; } = "PlayLists";
|
||||||
|
|
||||||
|
public string SmallPictures { get; set; } = "SmallPictures";
|
||||||
|
|
||||||
|
public string MediumPictures { get; set; } = "MediumPictures";
|
||||||
|
|
||||||
|
public string LargePictures { get; set; } = "LargePictures";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
namespace Musify.Application.Configuration
|
||||||
|
{
|
||||||
|
public class StorageConfiguration
|
||||||
|
{
|
||||||
|
public string BucketName { get; set; } = "musify-S3";
|
||||||
|
|
||||||
|
public StorageRoutes Routes { get; set; } = new StorageRoutes();
|
||||||
|
}
|
||||||
|
|
||||||
|
public class StorageRoutes
|
||||||
|
{
|
||||||
|
public string Uploads { get; set; } = "Uploads";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,6 +13,8 @@ namespace Musify.Application.Contracts.Infrastructure
|
|||||||
|
|
||||||
DbSet<PlayListHasTrack> PlayListHasTracks { get; }
|
DbSet<PlayListHasTrack> PlayListHasTracks { get; }
|
||||||
|
|
||||||
|
DbSet<Upload> Uploads { get; }
|
||||||
|
|
||||||
Task<int> SaveChangesAsync(CancellationToken cancellationToken);
|
Task<int> SaveChangesAsync(CancellationToken cancellationToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
namespace Musify.Application.Contracts.Infrastructure
|
||||||
|
{
|
||||||
|
public interface IEventBus
|
||||||
|
{
|
||||||
|
Task PublishAsync<T>(T message, CancellationToken cancellationToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,14 +4,14 @@ namespace Musify.Application.Contracts.Infrastructure
|
|||||||
{
|
{
|
||||||
public interface IStorageHandler
|
public interface IStorageHandler
|
||||||
{
|
{
|
||||||
Task<Result<Stream>> GetFile(string bucketName, string keyName, CancellationToken cancellationToken);
|
Task<Result<Stream>> GetFileAsync(string bucketName, string keyName, CancellationToken cancellationToken);
|
||||||
|
|
||||||
Task<string> GetUrl(string bucketName, string keyName, TimeSpan ExpirationTime, CancellationToken cancellationToken);
|
Task<string> GetUrlAsync(string bucketName, string keyName, TimeSpan ExpirationTime, CancellationToken cancellationToken);
|
||||||
|
|
||||||
Task<Result<string>> UploadFile(Stream sourceStream, string contentType, string bucketName, string keyName, CancellationToken cancellationToken);
|
Task<Result<string>> UploadFileAsync(Stream sourceStream, string contentType, string bucketName, string keyName, CancellationToken cancellationToken);
|
||||||
|
|
||||||
Task<Result> TransferFiles(string sourceDirectory, string bucketName, string route, CancellationToken cancellationToken);
|
Task<Result> TransferFilesAsync(string sourceDirectory, string bucketName, string route, CancellationToken cancellationToken);
|
||||||
|
|
||||||
Task<Result> RemoveFile(string bucketName, string keyName, CancellationToken cancellationToken);
|
Task<Result> RemoveFileAsync(string bucketName, string keyName, CancellationToken cancellationToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
using Ardalis.Result;
|
||||||
|
using DispatchR.Abstractions.Send;
|
||||||
|
|
||||||
|
namespace Musify.Application.Pictures.Commands.ResizeImage
|
||||||
|
{
|
||||||
|
public record ResizeCommand(
|
||||||
|
Stream PictureStream,
|
||||||
|
string ContentType,
|
||||||
|
int Width,
|
||||||
|
int Height,
|
||||||
|
string SaveRoute)
|
||||||
|
: IRequest<ResizeCommand, Result<Guid>>;
|
||||||
|
}
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
using Ardalis.Result;
|
||||||
|
using DispatchR.Abstractions.Send;
|
||||||
|
using MassTransit;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Musify.Application.Configuration;
|
||||||
|
using Musify.Application.Contracts.Infrastructure;
|
||||||
|
using Musify.Application.Pictures.Events;
|
||||||
|
using Musify.Domain.Entities;
|
||||||
|
|
||||||
|
namespace Musify.Application.Pictures.Commands.ResizeImage
|
||||||
|
{
|
||||||
|
public class ResizeCommandHandler(IStorageHandler storageHandler, IDatabase database, IPublishEndpoint publishEndpoint,
|
||||||
|
ILogger<ResizeCommandHandler> logger, StorageConfiguration storageConfiguration)
|
||||||
|
: IRequestHandler<ResizeCommand, Task<Result<Guid> >>
|
||||||
|
{
|
||||||
|
public async Task<Result<Guid>> Handle(ResizeCommand request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var pictureId = Guid.NewGuid();
|
||||||
|
|
||||||
|
var upload = new Upload
|
||||||
|
{
|
||||||
|
Id = pictureId,
|
||||||
|
KeyName = $"{storageConfiguration.Routes.Uploads}/{pictureId}",
|
||||||
|
ContentType = request.ContentType,
|
||||||
|
BucketName = storageConfiguration.BucketName,
|
||||||
|
};
|
||||||
|
|
||||||
|
await database.Uploads.AddAsync(upload, cancellationToken);
|
||||||
|
|
||||||
|
await database.SaveChangesAsync(cancellationToken);
|
||||||
|
|
||||||
|
var uploadResult = await storageHandler.UploadFileAsync(
|
||||||
|
request.PictureStream,
|
||||||
|
request.ContentType,
|
||||||
|
storageConfiguration.BucketName,
|
||||||
|
pictureId.ToString(),
|
||||||
|
cancellationToken);
|
||||||
|
|
||||||
|
if (!uploadResult.IsSuccess)
|
||||||
|
{
|
||||||
|
logger.LogError("Failed to upload the picture with ID {PictureId} to bucket {BucketName}.", pictureId, storageConfiguration.BucketName);
|
||||||
|
return Result.Error("Failed to upload the picture.");
|
||||||
|
}
|
||||||
|
|
||||||
|
await publishEndpoint.Publish(new ImageResizeEvent(
|
||||||
|
pictureId,
|
||||||
|
request.Width,
|
||||||
|
request.Height,
|
||||||
|
request.SaveRoute),
|
||||||
|
cancellationToken);
|
||||||
|
|
||||||
|
return Result.Success();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
namespace Musify.Application.Pictures.Events
|
||||||
|
{
|
||||||
|
public record ImageResizeEvent(
|
||||||
|
Guid UploadId,
|
||||||
|
int Width,
|
||||||
|
int Height,
|
||||||
|
string SaveRoute);
|
||||||
|
}
|
||||||
@@ -10,6 +10,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="AWSSDK.S3" Version="4.0.19" />
|
<PackageReference Include="AWSSDK.S3" Version="4.0.19" />
|
||||||
<PackageReference Include="Keycloak.Net.Core" Version="1.0.38" />
|
<PackageReference Include="Keycloak.Net.Core" Version="1.0.38" />
|
||||||
|
<PackageReference Include="MassTransit" Version="9.0.1" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.5" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.5" />
|
||||||
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.12" />
|
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.12" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@@ -0,0 +1,99 @@
|
|||||||
|
using Ardalis.Result;
|
||||||
|
using MassTransit;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Musify.Application.Contracts.Infrastructure;
|
||||||
|
using Musify.Application.Pictures.Events;
|
||||||
|
using Musify.Domain.Entities;
|
||||||
|
|
||||||
|
namespace Musify.Infrastructure.Messaging.Consumers
|
||||||
|
{
|
||||||
|
public class ImageResizedConsumer(IStorageHandler storageHandler, IDatabase database, IPictureHandler pictureHandler, ILogger<ImageResizedConsumer> logger)
|
||||||
|
: IConsumer<ImageResizeEvent>
|
||||||
|
{
|
||||||
|
public async Task Consume(ConsumeContext<ImageResizeEvent> consumeContext)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var upload = await database.Uploads.AsNoTracking().SingleOrDefaultAsync(u => u.Id == consumeContext.Message.UploadId);
|
||||||
|
|
||||||
|
if (upload is null)
|
||||||
|
{
|
||||||
|
logger.LogWarning("Upload not found in database: UploadId={UploadId}", consumeContext.Message.UploadId);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (upload.State != UploadState.Pending)
|
||||||
|
{
|
||||||
|
logger.LogInformation("Upload is not in a failed state, skipping processing: UploadId={UploadId}, State={State}", consumeContext.Message.UploadId, upload.State);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await UpdateState(database, upload, UploadState.Processing, consumeContext.CancellationToken);
|
||||||
|
|
||||||
|
var file = await storageHandler.GetFileAsync(
|
||||||
|
upload.BucketName,
|
||||||
|
upload.KeyName,
|
||||||
|
consumeContext.CancellationToken);
|
||||||
|
|
||||||
|
if (file.IsNotFound())
|
||||||
|
{
|
||||||
|
logger.LogWarning("File not found in storage: BucketName={BucketName}, KeyName={KeyName}", upload.BucketName, upload.KeyName);
|
||||||
|
|
||||||
|
await UpdateState(database, upload, UploadState.Failed, consumeContext.CancellationToken);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var pictureResult = await pictureHandler.ResizePictureAsync(
|
||||||
|
file.Value,
|
||||||
|
consumeContext.Message.Width,
|
||||||
|
consumeContext.Message.Height,
|
||||||
|
consumeContext.CancellationToken);
|
||||||
|
|
||||||
|
if (!pictureResult.IsSuccess)
|
||||||
|
{
|
||||||
|
string errors = string.Join("; ", pictureResult.Errors);
|
||||||
|
logger.LogError("Failed to resize picture: {ErrorMessage}", errors);
|
||||||
|
|
||||||
|
await UpdateState(database, upload, UploadState.Failed, consumeContext.CancellationToken);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var saveResult = await storageHandler.UploadFileAsync(
|
||||||
|
pictureResult.Value,
|
||||||
|
"image/webp",
|
||||||
|
upload.BucketName,
|
||||||
|
$"{consumeContext.Message.SaveRoute}/{upload.Id}.webp",
|
||||||
|
consumeContext.CancellationToken);
|
||||||
|
|
||||||
|
if (!saveResult.IsSuccess)
|
||||||
|
{
|
||||||
|
string errors = string.Join("; ", saveResult.Errors);
|
||||||
|
logger.LogError("Failed to upload resized picture: {ErrorMessage}", errors);
|
||||||
|
|
||||||
|
await UpdateState(database, upload, UploadState.Failed, consumeContext.CancellationToken);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await UpdateState(database, upload, UploadState.Completed, consumeContext.CancellationToken);
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
logger.LogError(exception, "An error occurred while consuming the ImageResizedConsumer message.");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task UpdateState(IDatabase database, Upload upload, UploadState uploadState, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
upload.State = uploadState;
|
||||||
|
|
||||||
|
database.Uploads.Update(upload);
|
||||||
|
|
||||||
|
await database.SaveChangesAsync(cancellationToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
using MassTransit;
|
||||||
|
using Musify.Application.Contracts.Infrastructure;
|
||||||
|
|
||||||
|
namespace Musify.Infrastructure.Messaging
|
||||||
|
{
|
||||||
|
public class MassTransitEventBus(IPublishEndpoint publishEndpoint)
|
||||||
|
: IEventBus
|
||||||
|
{
|
||||||
|
public async Task PublishAsync<T>(T message, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(message, nameof(message));
|
||||||
|
|
||||||
|
await publishEndpoint.Publish(message, cancellationToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,5 +14,7 @@ namespace Musify.Infrastructure.Persistence
|
|||||||
public DbSet<PlayList> PlayLists => Set<PlayList>();
|
public DbSet<PlayList> PlayLists => Set<PlayList>();
|
||||||
|
|
||||||
public DbSet<PlayListHasTrack> PlayListHasTracks => Set<PlayListHasTrack>();
|
public DbSet<PlayListHasTrack> PlayListHasTracks => Set<PlayListHasTrack>();
|
||||||
|
|
||||||
|
public DbSet<Upload> Uploads => Set<Upload>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -10,7 +10,7 @@ namespace Musify.Infrastructure.Storage
|
|||||||
public class StorageHandler(IAmazonS3 amazonS3, ILogger<StorageHandler> logger)
|
public class StorageHandler(IAmazonS3 amazonS3, ILogger<StorageHandler> logger)
|
||||||
: IStorageHandler
|
: IStorageHandler
|
||||||
{
|
{
|
||||||
public async Task<Result<Stream>> GetFile(string bucketName, string keyName, CancellationToken cancellationToken)
|
public async Task<Result<Stream>> GetFileAsync(string bucketName, string keyName, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var request = new GetObjectRequest
|
var request = new GetObjectRequest
|
||||||
{
|
{
|
||||||
@@ -34,7 +34,7 @@ namespace Musify.Infrastructure.Storage
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<string> GetUrl(string bucketName, string keyName, TimeSpan expirationTime, CancellationToken cancellationToken)
|
public async Task<string> GetUrlAsync(string bucketName, string keyName, TimeSpan expirationTime, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var request = new GetPreSignedUrlRequest
|
var request = new GetPreSignedUrlRequest
|
||||||
{
|
{
|
||||||
@@ -50,7 +50,7 @@ namespace Musify.Infrastructure.Storage
|
|||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Result> RemoveFile(string bucketName, string keyName, CancellationToken cancellationToken)
|
public async Task<Result> RemoveFileAsync(string bucketName, string keyName, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var request = new DeleteObjectRequest
|
var request = new DeleteObjectRequest
|
||||||
{
|
{
|
||||||
@@ -64,7 +64,7 @@ namespace Musify.Infrastructure.Storage
|
|||||||
return Result.NoContent();
|
return Result.NoContent();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Result> TransferFiles(string sourceDirectory, string bucketName, string route, CancellationToken cancellationToken)
|
public async Task<Result> TransferFilesAsync(string sourceDirectory, string bucketName, string route, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var trasnsferUtility = new TransferUtility(amazonS3);
|
var trasnsferUtility = new TransferUtility(amazonS3);
|
||||||
|
|
||||||
@@ -89,7 +89,7 @@ namespace Musify.Infrastructure.Storage
|
|||||||
return Result.Success();
|
return Result.Success();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Result<string>> UploadFile(Stream sourceStream, string contentType, string bucketName, string keyName, CancellationToken cancellationToken)
|
public async Task<Result<string>> UploadFileAsync(Stream sourceStream, string contentType, string bucketName, string keyName, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var request = new PutObjectRequest
|
var request = new PutObjectRequest
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using System.ComponentModel.DataAnnotations.Schema;
|
||||||
|
|
||||||
|
namespace Musify.Domain.Entities
|
||||||
|
{
|
||||||
|
[Table("Upload")]
|
||||||
|
public class Upload
|
||||||
|
{
|
||||||
|
[Key]
|
||||||
|
public Guid Id { get; set; } = Guid.NewGuid();
|
||||||
|
|
||||||
|
[Required]
|
||||||
|
public required string KeyName { get; set; }
|
||||||
|
|
||||||
|
[Required]
|
||||||
|
public required string BucketName { get; set; }
|
||||||
|
|
||||||
|
[Required]
|
||||||
|
public required string ContentType { get; set; }
|
||||||
|
|
||||||
|
[Required]
|
||||||
|
public UploadState State { get; set; } = UploadState.Pending;
|
||||||
|
|
||||||
|
[Required]
|
||||||
|
public DateTime CreatedTime { get; set; } = DateTime.UtcNow;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
namespace Musify.Domain.Entities
|
||||||
|
{
|
||||||
|
public enum UploadState
|
||||||
|
{
|
||||||
|
Pending,
|
||||||
|
Processing,
|
||||||
|
Completed,
|
||||||
|
Failed
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user