Uploads
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Ardalis.Result" Version="10.1.0" />
|
||||
<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="X.PagedList" Version="10.5.9" />
|
||||
<PackageReference Include="X.PagedList.EF" Version="10.5.9" />
|
||||
@@ -20,7 +21,6 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Configuration\" />
|
||||
<Folder Include="Contracts\Application\" />
|
||||
<Folder Include="PlayLists\Commands\CreatePlayList\" />
|
||||
<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<Upload> Uploads { get; }
|
||||
|
||||
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
|
||||
{
|
||||
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);
|
||||
}
|
||||
Reference in New Issue
Block a user