jobs and others
This commit is contained in:
@@ -23,7 +23,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Folder Include="PlayLists\Commands\DeletePlayList\" />
|
<Folder Include="Contracts\Application\" />
|
||||||
<Folder Include="PlayLists\Commands\UpdatePlayList\" />
|
<Folder Include="PlayLists\Commands\UpdatePlayList\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +0,0 @@
|
|||||||
using Ardalis.Result;
|
|
||||||
using Musify.Application.Pictures.Contracts;
|
|
||||||
using Musify.Domain.Entities;
|
|
||||||
|
|
||||||
namespace Musify.Application.Contracts.Application
|
|
||||||
{
|
|
||||||
public interface IPictureService
|
|
||||||
{
|
|
||||||
Task<Result<Guid>> ResizePictureAsync(EntityType entityType, Guid EntityId, string keyName, string contentType, Stream pictureStream,
|
|
||||||
PictureResize[] pictureResizes, CancellationToken cancellationToken);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -15,6 +15,8 @@ namespace Musify.Application.Contracts.Infrastructure
|
|||||||
|
|
||||||
DbSet<Upload> Uploads { get; }
|
DbSet<Upload> Uploads { get; }
|
||||||
|
|
||||||
|
DbSet<Job> Jobs { get; }
|
||||||
|
|
||||||
Task<int> SaveChangesAsync(CancellationToken cancellationToken);
|
Task<int> SaveChangesAsync(CancellationToken cancellationToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
namespace Musify.Application.Events
|
||||||
|
{
|
||||||
|
public record RemoveFileEvent(
|
||||||
|
string BucketName,
|
||||||
|
string KeyName);
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
namespace Musify.Application.Events
|
||||||
|
{
|
||||||
|
public record ResizePictureEvent(
|
||||||
|
string BucketName,
|
||||||
|
string KeyName,
|
||||||
|
ResizePictureItems[] Items);
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
namespace Musify.Application.Events
|
||||||
|
{
|
||||||
|
public record ResizePictureItems(
|
||||||
|
int Width,
|
||||||
|
int Height,
|
||||||
|
string SaveOnRoute);
|
||||||
|
}
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
using Ardalis.Result;
|
|
||||||
using DispatchR.Abstractions.Send;
|
|
||||||
using Musify.Application.Pictures.Contracts;
|
|
||||||
using Musify.Domain.Entities;
|
|
||||||
|
|
||||||
namespace Musify.Application.Pictures.Commands.ResizePicture
|
|
||||||
{
|
|
||||||
public record ResizeCommand(
|
|
||||||
EntityType EntityType,
|
|
||||||
Guid EntityId,
|
|
||||||
string KeyName,
|
|
||||||
string ContentType,
|
|
||||||
Stream PictureStream,
|
|
||||||
PictureResize[] PictureResizes)
|
|
||||||
: IRequest<ResizeCommand, Result<Guid>>;
|
|
||||||
}
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
using Ardalis.Result;
|
|
||||||
using DispatchR.Abstractions.Send;
|
|
||||||
using Musify.Application.Contracts.Application;
|
|
||||||
|
|
||||||
namespace Musify.Application.Pictures.Commands.ResizePicture
|
|
||||||
{
|
|
||||||
public class ResizeCommandHandler(IPictureService pictureService)
|
|
||||||
: IRequestHandler<ResizeCommand, Task<Result<Guid> >>
|
|
||||||
{
|
|
||||||
public async Task<Result<Guid>> Handle(ResizeCommand request, CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
var result = await pictureService.ResizePictureAsync(request.EntityType, request.EntityId, request.KeyName, request.ContentType,
|
|
||||||
request.PictureStream, request.PictureResizes, cancellationToken);
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
namespace Musify.Application.Pictures.Contracts
|
|
||||||
{
|
|
||||||
public record PictureResize(
|
|
||||||
int Width,
|
|
||||||
int Height,
|
|
||||||
string SaveOnRoute);
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
using Musify.Application.Pictures.Contracts;
|
|
||||||
|
|
||||||
namespace Musify.Application.Pictures.Events
|
|
||||||
{
|
|
||||||
public record PictureResizeEvent(
|
|
||||||
Guid UploadId,
|
|
||||||
PictureResize[] ImageResizes);
|
|
||||||
}
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
using Musify.Application.Contracts.Application;
|
|
||||||
|
|
||||||
namespace Musify.Application.Pictures
|
|
||||||
{
|
|
||||||
public static class PictureDependencyInjection
|
|
||||||
{
|
|
||||||
public static void AddPictureService(this IServiceCollection serviceDescriptors)
|
|
||||||
{
|
|
||||||
serviceDescriptors.AddScoped<IPictureService, PictureService>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,61 +0,0 @@
|
|||||||
using Ardalis.Result;
|
|
||||||
using MassTransit;
|
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using Musify.Application.Configuration;
|
|
||||||
using Musify.Application.Contracts.Application;
|
|
||||||
using Musify.Application.Contracts.Infrastructure;
|
|
||||||
using Musify.Application.Pictures.Commands.ResizePicture;
|
|
||||||
using Musify.Application.Pictures.Contracts;
|
|
||||||
using Musify.Application.Pictures.Events;
|
|
||||||
using Musify.Domain.Entities;
|
|
||||||
|
|
||||||
namespace Musify.Application.Pictures
|
|
||||||
{
|
|
||||||
public class PictureService(IStorageHandler storageHandler, IDatabase database, IPublishEndpoint publishEndpoint,
|
|
||||||
ILogger<ResizeCommandHandler> logger, StorageConfiguration storageConfiguration)
|
|
||||||
: IPictureService
|
|
||||||
{
|
|
||||||
public async Task<Result<Guid>> ResizePictureAsync(EntityType entityType, Guid EntityId, string keyName, string contentType, Stream pictureStream,
|
|
||||||
PictureResize[] pictureResizes, CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
var pictureId = Guid.NewGuid();
|
|
||||||
|
|
||||||
var upload = new Upload
|
|
||||||
{
|
|
||||||
Id = pictureId,
|
|
||||||
KeyName = keyName,
|
|
||||||
ContentType = contentType,
|
|
||||||
BucketName = storageConfiguration.BucketName,
|
|
||||||
EntityType = entityType,
|
|
||||||
EntityId = EntityId,
|
|
||||||
State = UploadState.Completed
|
|
||||||
};
|
|
||||||
|
|
||||||
await database.Uploads.AddAsync(upload, cancellationToken);
|
|
||||||
|
|
||||||
await database.SaveChangesAsync(cancellationToken);
|
|
||||||
|
|
||||||
var uploadResult = await storageHandler.UploadFileAsync(
|
|
||||||
pictureStream,
|
|
||||||
contentType,
|
|
||||||
storageConfiguration.BucketName,
|
|
||||||
keyName,
|
|
||||||
cancellationToken);
|
|
||||||
|
|
||||||
if (!uploadResult.IsSuccess)
|
|
||||||
{
|
|
||||||
logger.LogError("Failed to upload the picture with ID {PictureId} to bucket {BucketName}.", pictureId, storageConfiguration.BucketName);
|
|
||||||
|
|
||||||
upload.State = UploadState.Failed;
|
|
||||||
database.Uploads.Update(upload);
|
|
||||||
await database.SaveChangesAsync(cancellationToken);
|
|
||||||
|
|
||||||
return Result.Error("Failed to upload the picture.");
|
|
||||||
}
|
|
||||||
|
|
||||||
await publishEndpoint.Publish(new PictureResizeEvent(pictureId, pictureResizes));
|
|
||||||
|
|
||||||
return Result.Success();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -9,6 +9,7 @@ namespace Musify.Application.PlayLists.Commands.CreatePlayList
|
|||||||
string Name,
|
string Name,
|
||||||
string Description,
|
string Description,
|
||||||
Stream PictureStream,
|
Stream PictureStream,
|
||||||
|
string PictureFileType,
|
||||||
string PictureContentType)
|
string PictureContentType)
|
||||||
: IRequest<CreatePlayListCommand, Result<PlayListResponse>>;
|
: IRequest<CreatePlayListCommand, Result<PlayListResponse>>;
|
||||||
}
|
}
|
||||||
@@ -1,18 +1,18 @@
|
|||||||
using Ardalis.Result;
|
using Ardalis.Result;
|
||||||
using DispatchR.Abstractions.Send;
|
using DispatchR.Abstractions.Send;
|
||||||
|
using MassTransit;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Musify.Application.Configuration;
|
using Musify.Application.Configuration;
|
||||||
using Musify.Application.Contracts.Application;
|
|
||||||
using Musify.Application.Contracts.Infrastructure;
|
using Musify.Application.Contracts.Infrastructure;
|
||||||
using Musify.Application.Pictures.Contracts;
|
using Musify.Application.Events;
|
||||||
using Musify.Application.PlayLists.Contracts;
|
using Musify.Application.PlayLists.Contracts;
|
||||||
using Musify.Domain.Entities;
|
using Musify.Domain.Entities;
|
||||||
|
|
||||||
namespace Musify.Application.PlayLists.Commands.CreatePlayList
|
namespace Musify.Application.PlayLists.Commands.CreatePlayList
|
||||||
{
|
{
|
||||||
public class CreatePlayListCommandHandler(IPictureService pictureService, IDatabase database, ILogger<CreatePlayListCommandHandler> logger,
|
public class CreatePlayListCommandHandler(IPublishEndpoint publishEndpoint, IDatabase database, IStorageHandler storageHandler,
|
||||||
PlayListConfiguration playListConfiguration)
|
ILogger<CreatePlayListCommandHandler> logger, StorageConfiguration storageConfiguration, PlayListConfiguration playListConfiguration)
|
||||||
: IRequestHandler<CreatePlayListCommand, Task<Result<PlayListResponse>> >
|
: IRequestHandler<CreatePlayListCommand, Task<Result<PlayListResponse>> >
|
||||||
{
|
{
|
||||||
public async Task<Result<PlayListResponse>> Handle(CreatePlayListCommand request, CancellationToken cancellationToken)
|
public async Task<Result<PlayListResponse>> Handle(CreatePlayListCommand request, CancellationToken cancellationToken)
|
||||||
@@ -35,34 +35,45 @@ namespace Musify.Application.PlayLists.Commands.CreatePlayList
|
|||||||
MediumPictureKeyName = playListConfiguration.Routes.MediumPictures,
|
MediumPictureKeyName = playListConfiguration.Routes.MediumPictures,
|
||||||
LargePictureKeyName = playListConfiguration.Routes.LargePictures,
|
LargePictureKeyName = playListConfiguration.Routes.LargePictures,
|
||||||
};
|
};
|
||||||
|
|
||||||
await database.PlayLists.AddAsync(playList, cancellationToken);
|
await database.PlayLists.AddAsync(playList, cancellationToken);
|
||||||
|
|
||||||
await database.SaveChangesAsync(cancellationToken);
|
var imageId = Guid.NewGuid();
|
||||||
|
|
||||||
|
var imageKeyName = $"{playListConfiguration.Routes.OriginalPictures}/{imageId}.{request.PictureFileType}";
|
||||||
|
|
||||||
|
var uploadResult = await storageHandler.UploadFileAsync(
|
||||||
|
request.PictureStream,
|
||||||
|
request.PictureContentType,
|
||||||
|
storageConfiguration.BucketName,
|
||||||
|
imageKeyName,
|
||||||
|
cancellationToken);
|
||||||
|
|
||||||
|
if (!uploadResult.IsSuccess)
|
||||||
|
{
|
||||||
|
logger.LogError("Failed to upload picture for PlayList with Id={PlayListId} to storage. Storage handler returned error: {ErrorMessage}",
|
||||||
|
playList.Id, string.Join("; ", uploadResult.Errors));
|
||||||
|
return Result.Error($"Failed to upload picture for PlayList with Id {playList.Id} to storage.");
|
||||||
|
}
|
||||||
|
|
||||||
logger.LogInformation("PlayList with Id={PlayListId} created successfully for UserId={UserId}.", playList.Id, request.UserId);
|
logger.LogInformation("PlayList with Id={PlayListId} created successfully for UserId={UserId}.", playList.Id, request.UserId);
|
||||||
|
|
||||||
await pictureService.ResizePictureAsync(
|
await publishEndpoint.Publish(new ResizePictureEvent(
|
||||||
EntityType.PlayList,
|
storageConfiguration.BucketName,
|
||||||
playList.Id,
|
imageKeyName,
|
||||||
playList.Id.ToString(),
|
[
|
||||||
request.PictureContentType,
|
new ResizePictureItems(
|
||||||
request.PictureStream,
|
|
||||||
[
|
|
||||||
new PictureResize(
|
|
||||||
playListConfiguration.PicturesSizes.SmallPictureWidth,
|
playListConfiguration.PicturesSizes.SmallPictureWidth,
|
||||||
playListConfiguration.PicturesSizes.SmallPictureHeight,
|
playListConfiguration.PicturesSizes.SmallPictureHeight,
|
||||||
playListConfiguration.Routes.SmallPictures),
|
playListConfiguration.Routes.SmallPictures),
|
||||||
new PictureResize(
|
new ResizePictureItems(
|
||||||
playListConfiguration.PicturesSizes.MediumPictureWidth,
|
playListConfiguration.PicturesSizes.MediumPictureWidth,
|
||||||
playListConfiguration.PicturesSizes.MediumPictureHeight,
|
playListConfiguration.PicturesSizes.MediumPictureHeight,
|
||||||
playListConfiguration.Routes.MediumPictures),
|
playListConfiguration.Routes.MediumPictures),
|
||||||
new PictureResize(
|
new ResizePictureItems(
|
||||||
playListConfiguration.PicturesSizes.LargePictureWidth,
|
playListConfiguration.PicturesSizes.LargePictureWidth,
|
||||||
playListConfiguration.PicturesSizes.LargePictureHeight,
|
playListConfiguration.PicturesSizes.LargePictureHeight,
|
||||||
playListConfiguration.Routes.LargePictures)
|
playListConfiguration.Routes.LargePictures)
|
||||||
],
|
] ), cancellationToken);
|
||||||
cancellationToken);
|
|
||||||
|
|
||||||
logger.LogInformation("Picture for PlayList with Id={PlayListId} ", playList.Id);
|
logger.LogInformation("Picture for PlayList with Id={PlayListId} ", playList.Id);
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
using Ardalis.Result;
|
||||||
|
using DispatchR.Abstractions.Send;
|
||||||
|
|
||||||
|
namespace Musify.Application.PlayLists.Commands.DeletePlayList
|
||||||
|
{
|
||||||
|
public record DeletePlayListCommand(Guid UserId, Guid PlayListId)
|
||||||
|
: IRequest<DeletePlayListCommand, Result>;
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
using Ardalis.Result;
|
||||||
|
using DispatchR.Abstractions.Send;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Musify.Application.Configuration;
|
||||||
|
using Musify.Application.Contracts.Infrastructure;
|
||||||
|
|
||||||
|
namespace Musify.Application.PlayLists.Commands.DeletePlayList
|
||||||
|
{
|
||||||
|
public class DeletePlayListCommandHandler(IDatabase database, IStorageHandler storageHandler,
|
||||||
|
StorageConfiguration storageConfiguration, ILogger<DeletePlayListCommandHandler> logger)
|
||||||
|
: IRequestHandler<DeletePlayListCommand, Task<Result>>
|
||||||
|
{
|
||||||
|
public async Task<Result> Handle(DeletePlayListCommand request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var playList = await database.PlayLists.FindAsync(request.PlayListId, cancellationToken);
|
||||||
|
|
||||||
|
if (playList is null)
|
||||||
|
{
|
||||||
|
logger.LogDebug("PlayList with id={PlayListId} not found", request.PlayListId);
|
||||||
|
return Result.NotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (playList.UserId != request.UserId)
|
||||||
|
{
|
||||||
|
logger.LogInformation("PlayList with id={PlayListId} does not belong to user with id={UserId}", request.PlayListId, request.UserId);
|
||||||
|
return Result.Unauthorized();
|
||||||
|
}
|
||||||
|
|
||||||
|
database.PlayLists.Remove(playList);
|
||||||
|
|
||||||
|
await database.SaveChangesAsync(cancellationToken);
|
||||||
|
|
||||||
|
return Result.NoContent();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
namespace Musify.Domain.Entities
|
|
||||||
{
|
|
||||||
public enum EntityType
|
|
||||||
{
|
|
||||||
User,
|
|
||||||
Track,
|
|
||||||
PlayList
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using System.ComponentModel.DataAnnotations.Schema;
|
||||||
|
|
||||||
|
namespace Musify.Domain.Entities
|
||||||
|
{
|
||||||
|
[Table("Job")]
|
||||||
|
public class Job
|
||||||
|
{
|
||||||
|
[Key]
|
||||||
|
public Guid Id { get; set; } = Guid.NewGuid();
|
||||||
|
|
||||||
|
[Required]
|
||||||
|
public required JobType JobType { get; set; }
|
||||||
|
|
||||||
|
[Required]
|
||||||
|
public JobState JobState { get; set; } = JobState.Pending;
|
||||||
|
|
||||||
|
[Required]
|
||||||
|
public required string Payload { get; set; }
|
||||||
|
|
||||||
|
[Required]
|
||||||
|
public DateTime CreateAt { get; set; } = DateTime.UtcNow;
|
||||||
|
|
||||||
|
[Required]
|
||||||
|
public int RetryCount { get; set; } = 0;
|
||||||
|
|
||||||
|
public DateTime CompletedAt { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
namespace Musify.Domain.Entities
|
||||||
|
{
|
||||||
|
public enum JobState
|
||||||
|
{
|
||||||
|
Pending,
|
||||||
|
Failed,
|
||||||
|
InProgress,
|
||||||
|
Completed
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
namespace Musify.Domain.Entities
|
||||||
|
{
|
||||||
|
public enum JobType
|
||||||
|
{
|
||||||
|
ResizePicture,
|
||||||
|
RemoveFile,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -19,15 +19,9 @@ namespace Musify.Domain.Entities
|
|||||||
public required string ContentType { get; set; }
|
public required string ContentType { get; set; }
|
||||||
|
|
||||||
[Required]
|
[Required]
|
||||||
public required EntityType EntityType { get; set; }
|
public DateTime CreatedTime { get; set; } = DateTime.UtcNow;
|
||||||
|
|
||||||
[Required]
|
|
||||||
public required Guid EntityId { get; set; }
|
|
||||||
|
|
||||||
[Required]
|
[Required]
|
||||||
public UploadState State { get; set; } = UploadState.Pending;
|
public UploadState State { get; set; } = UploadState.Pending;
|
||||||
|
|
||||||
[Required]
|
|
||||||
public DateTime CreatedTime { get; set; } = DateTime.UtcNow;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,8 +3,7 @@
|
|||||||
public enum UploadState
|
public enum UploadState
|
||||||
{
|
{
|
||||||
Pending,
|
Pending,
|
||||||
Processing,
|
Failed,
|
||||||
Completed,
|
Successful,
|
||||||
Failed
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,138 +0,0 @@
|
|||||||
using Ardalis.Result;
|
|
||||||
using MassTransit;
|
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using Musify.Application.Contracts.Infrastructure;
|
|
||||||
using Musify.Application.Pictures.Contracts;
|
|
||||||
using Musify.Application.Pictures.Events;
|
|
||||||
using Musify.Domain.Entities;
|
|
||||||
|
|
||||||
namespace Musify.Infrastructure.Messaging.Consumers
|
|
||||||
{
|
|
||||||
public class PictureResizerConsumer(IStorageHandler storageHandler, IDatabase database, IPictureHandler pictureHandler, ILogger<PictureResizerConsumer> logger)
|
|
||||||
: IConsumer<PictureResizeEvent>
|
|
||||||
{
|
|
||||||
public const string QueueName = "picture-resize-queue";
|
|
||||||
|
|
||||||
public async Task Consume(ConsumeContext<PictureResizeEvent> consumeContext)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var upload = await database.Uploads.FindAsync(consumeContext.Message.UploadId);
|
|
||||||
|
|
||||||
if (upload is null)
|
|
||||||
{
|
|
||||||
logger.LogWarning("Upload not found in database: UploadId={UploadId}", consumeContext.Message.UploadId);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (upload.State != UploadState.Completed)
|
|
||||||
{
|
|
||||||
logger.LogInformation("Upload is not in a Completed state, skipping processing: UploadId={UploadId}, State={State}", consumeContext.Message.UploadId, upload.State);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
await UpdateState(database, upload, UploadState.Processing, consumeContext.CancellationToken);
|
|
||||||
|
|
||||||
var fileResult = await storageHandler.GetFileAsync(
|
|
||||||
upload.BucketName,
|
|
||||||
upload.KeyName,
|
|
||||||
consumeContext.CancellationToken);
|
|
||||||
|
|
||||||
if (fileResult.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;
|
|
||||||
}
|
|
||||||
|
|
||||||
using var fileStream = fileResult.Value;
|
|
||||||
|
|
||||||
using var memoryStream = new MemoryStream();
|
|
||||||
await fileStream.CopyToAsync(memoryStream, consumeContext.CancellationToken);
|
|
||||||
|
|
||||||
foreach (var resize in consumeContext.Message.ImageResizes)
|
|
||||||
{
|
|
||||||
var newUploadId = Guid.NewGuid();
|
|
||||||
|
|
||||||
var newUpload = new Upload
|
|
||||||
{
|
|
||||||
Id = newUploadId,
|
|
||||||
BucketName = upload.BucketName,
|
|
||||||
KeyName = $"{resize.SaveOnRoute}/{newUploadId}.webp",
|
|
||||||
ContentType = "image/webp",
|
|
||||||
State = UploadState.Processing,
|
|
||||||
EntityId = upload.EntityId,
|
|
||||||
EntityType = upload.EntityType
|
|
||||||
};
|
|
||||||
|
|
||||||
await database.Uploads.AddAsync(newUpload, consumeContext.CancellationToken);
|
|
||||||
|
|
||||||
await database.SaveChangesAsync(consumeContext.CancellationToken);
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
await ResizePicture(upload, newUpload, memoryStream, resize, consumeContext.CancellationToken);
|
|
||||||
}
|
|
||||||
catch (Exception exception)
|
|
||||||
{
|
|
||||||
logger.LogError(exception, "An error occurred while resizing picture for UploadId={UploadId} and Resize={Resize}", upload.Id, resize);
|
|
||||||
await UpdateState(database, newUpload, UploadState.Failed, consumeContext.CancellationToken);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
await UpdateState(database, upload, UploadState.Completed, consumeContext.CancellationToken);
|
|
||||||
}
|
|
||||||
catch (Exception exception)
|
|
||||||
{
|
|
||||||
logger.LogError(exception, "An error occurred while consuming the ImageResizedConsumer message.");
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
logger.LogInformation("Saving changes to database after processing ImageResizedConsumer message.");
|
|
||||||
await database.SaveChangesAsync(consumeContext.CancellationToken);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async Task UpdateState(IDatabase database, Upload upload, UploadState uploadState, CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
upload.State = uploadState;
|
|
||||||
database.Uploads.Update(upload);
|
|
||||||
}
|
|
||||||
|
|
||||||
async Task ResizePicture(Upload originalUpload, Upload newUpload, MemoryStream memoryStream, PictureResize pictureResize, CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
memoryStream.Position = 0;
|
|
||||||
|
|
||||||
var pictureResult = await pictureHandler.ResizePictureAsync(
|
|
||||||
memoryStream,
|
|
||||||
pictureResize.Width,
|
|
||||||
pictureResize.Height,
|
|
||||||
cancellationToken);
|
|
||||||
|
|
||||||
if (!pictureResult.IsSuccess)
|
|
||||||
{
|
|
||||||
logger.LogError("Failed to resize picture: {ErrorMessage}", string.Join("; ", pictureResult.Errors));
|
|
||||||
await UpdateState(database, newUpload, UploadState.Failed, cancellationToken);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var saveResult = await storageHandler.UploadFileAsync(
|
|
||||||
pictureResult.Value,
|
|
||||||
"image/webp",
|
|
||||||
originalUpload.BucketName,
|
|
||||||
$"{pictureResize.SaveOnRoute}/{newUpload.Id}.webp",
|
|
||||||
cancellationToken);
|
|
||||||
|
|
||||||
if (!saveResult.IsSuccess)
|
|
||||||
{
|
|
||||||
logger.LogError("Failed to upload resized picture: {ErrorMessage}", string.Join("; ", saveResult.Errors));
|
|
||||||
await UpdateState(database, newUpload, UploadState.Failed, cancellationToken);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
await UpdateState(database, newUpload, UploadState.Completed, cancellationToken);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
using MassTransit;
|
||||||
|
using Musify.Application.Events;
|
||||||
|
|
||||||
|
namespace Musify.Infrastructure.Messaging.Consumers
|
||||||
|
{
|
||||||
|
public class RemoveFileConsumer
|
||||||
|
: IConsumer<RemoveFileEvent>
|
||||||
|
{
|
||||||
|
public Task Consume(ConsumeContext<RemoveFileEvent> context)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,138 @@
|
|||||||
|
using Ardalis.Result;
|
||||||
|
using MassTransit;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Musify.Application.Contracts.Infrastructure;
|
||||||
|
using Musify.Application.Events;
|
||||||
|
using Musify.Domain.Entities;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
|
namespace Musify.Infrastructure.Messaging.Consumers
|
||||||
|
{
|
||||||
|
public class ResizePictureConsumer(IStorageHandler storageHandler, IPictureHandler pictureHandler, IDatabase database, ILogger<ResizePictureConsumer> logger)
|
||||||
|
: IConsumer<ResizePictureEvent>
|
||||||
|
{
|
||||||
|
public const string QueueName = "picture-resize-queue";
|
||||||
|
|
||||||
|
public async Task Consume(ConsumeContext<ResizePictureEvent> consumeContext)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Dictionary<ResizePictureItems, Job> jobs = new Dictionary<ResizePictureItems, Job>();
|
||||||
|
|
||||||
|
foreach (var item in consumeContext.Message.Items)
|
||||||
|
{
|
||||||
|
var payloadJson = JsonSerializer.Serialize(item);
|
||||||
|
|
||||||
|
var job = await database.Jobs
|
||||||
|
.SingleOrDefaultAsync(job => job.JobType == JobType.ResizePicture && job.Payload == payloadJson, consumeContext.CancellationToken);
|
||||||
|
|
||||||
|
if (job is null)
|
||||||
|
{
|
||||||
|
job = new Job
|
||||||
|
{
|
||||||
|
JobType = JobType.ResizePicture,
|
||||||
|
Payload = payloadJson,
|
||||||
|
};
|
||||||
|
await database.Jobs.AddAsync(job, consumeContext.CancellationToken);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
job.RetryCount++;
|
||||||
|
job.JobState = JobState.InProgress;
|
||||||
|
database.Jobs.Update(job);
|
||||||
|
}
|
||||||
|
|
||||||
|
jobs.Add(item, job);
|
||||||
|
}
|
||||||
|
|
||||||
|
var fileResult = await storageHandler.GetFileAsync(
|
||||||
|
consumeContext.Message.BucketName,
|
||||||
|
consumeContext.Message.KeyName,
|
||||||
|
consumeContext.CancellationToken);
|
||||||
|
|
||||||
|
if (fileResult.IsNotFound())
|
||||||
|
{
|
||||||
|
logger.LogWarning("File not found in storage: BucketName={BucketName}, KeyName={KeyName}",
|
||||||
|
consumeContext.Message.BucketName, consumeContext.Message.KeyName);
|
||||||
|
|
||||||
|
foreach (var job in jobs)
|
||||||
|
{
|
||||||
|
job.Value.JobState = JobState.Failed;
|
||||||
|
}
|
||||||
|
await database.SaveChangesAsync(consumeContext.CancellationToken);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
using var fileStream = fileResult.Value;
|
||||||
|
using var memoryStream = new MemoryStream();
|
||||||
|
await fileStream.CopyToAsync(memoryStream, consumeContext.CancellationToken);
|
||||||
|
|
||||||
|
foreach (var item in consumeContext.Message.Items)
|
||||||
|
{
|
||||||
|
var job = jobs[item];
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await ResizePicture(
|
||||||
|
job,
|
||||||
|
consumeContext.Message.BucketName,
|
||||||
|
memoryStream,
|
||||||
|
item,
|
||||||
|
consumeContext.CancellationToken);
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
logger.LogError(exception, "An error occurred while resizing picture for UploadId={UploadId} and Resize={Resize}", job.Id, item);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
logger.LogError(exception, "An error occurred while consuming the ImageResizedConsumer message.");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
logger.LogInformation("Saving changes to database after processing ImageResizedConsumer message.");
|
||||||
|
await database.SaveChangesAsync(consumeContext.CancellationToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task ResizePicture(Job job, string bucketName, MemoryStream memoryStream, ResizePictureItems items, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
memoryStream.Position = 0;
|
||||||
|
job.JobState = JobState.InProgress;
|
||||||
|
|
||||||
|
var pictureResult = await pictureHandler.ResizePictureAsync(
|
||||||
|
memoryStream,
|
||||||
|
items.Width,
|
||||||
|
items.Height,
|
||||||
|
cancellationToken);
|
||||||
|
|
||||||
|
if (!pictureResult.IsSuccess)
|
||||||
|
{
|
||||||
|
logger.LogError("Failed to resize picture: {ErrorMessage}", string.Join("; ", pictureResult.Errors));
|
||||||
|
job.JobState = JobState.Failed;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var saveResult = await storageHandler.UploadFileAsync(
|
||||||
|
pictureResult.Value,
|
||||||
|
"image/webp",
|
||||||
|
bucketName,
|
||||||
|
$"{items.SaveOnRoute}/{Guid.NewGuid() }.webp",
|
||||||
|
cancellationToken);
|
||||||
|
|
||||||
|
if (!saveResult.IsSuccess)
|
||||||
|
{
|
||||||
|
logger.LogError("Failed to upload resized picture: {ErrorMessage}", string.Join("; ", saveResult.Errors));
|
||||||
|
job.JobState = JobState.Failed;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
job.JobState = JobState.Completed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -37,7 +37,7 @@ namespace Musify.Infrastructure.Messaging
|
|||||||
{
|
{
|
||||||
serviceDescriptors.AddMassTransit(options =>
|
serviceDescriptors.AddMassTransit(options =>
|
||||||
{
|
{
|
||||||
options.AddConsumer<PictureResizerConsumer>();
|
options.AddConsumer<ResizePictureConsumer>();
|
||||||
|
|
||||||
options.UsingRabbitMq((busRegistrationContext, busFactoryConfigurator) =>
|
options.UsingRabbitMq((busRegistrationContext, busFactoryConfigurator) =>
|
||||||
{
|
{
|
||||||
@@ -58,9 +58,9 @@ namespace Musify.Infrastructure.Messaging
|
|||||||
options.Password(password);
|
options.Password(password);
|
||||||
} );
|
} );
|
||||||
|
|
||||||
busFactoryConfigurator.ReceiveEndpoint(PictureResizerConsumer.QueueName, endpointConfigurator =>
|
busFactoryConfigurator.ReceiveEndpoint(ResizePictureConsumer.QueueName, endpointConfigurator =>
|
||||||
{
|
{
|
||||||
endpointConfigurator.ConfigureConsumer<PictureResizerConsumer>(busRegistrationContext);
|
endpointConfigurator.ConfigureConsumer<ResizePictureConsumer>(busRegistrationContext);
|
||||||
} );
|
} );
|
||||||
} );
|
} );
|
||||||
} );
|
} );
|
||||||
|
|||||||
@@ -22,5 +22,7 @@ namespace Musify.Infrastructure.Persistence
|
|||||||
public DbSet<PlayListHasTrack> PlayListHasTracks => Set<PlayListHasTrack>();
|
public DbSet<PlayListHasTrack> PlayListHasTracks => Set<PlayListHasTrack>();
|
||||||
|
|
||||||
public DbSet<Upload> Uploads => Set<Upload>();
|
public DbSet<Upload> Uploads => Set<Upload>();
|
||||||
|
|
||||||
|
public DbSet<Job> Jobs => Set<Job>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4,10 +4,11 @@ using Amazon.S3.Transfer;
|
|||||||
using Ardalis.Result;
|
using Ardalis.Result;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Musify.Application.Contracts.Infrastructure;
|
using Musify.Application.Contracts.Infrastructure;
|
||||||
|
using Musify.Domain.Entities;
|
||||||
|
|
||||||
namespace Musify.Infrastructure.Storage
|
namespace Musify.Infrastructure.Storage
|
||||||
{
|
{
|
||||||
public class StorageHandler(IAmazonS3 amazonS3, ILogger<StorageHandler> logger)
|
public class StorageHandler(IDatabase database, IAmazonS3 amazonS3, ILogger<StorageHandler> logger)
|
||||||
: IStorageHandler
|
: IStorageHandler
|
||||||
{
|
{
|
||||||
public async Task<Result<Stream>> GetFileAsync(string bucketName, string keyName, CancellationToken cancellationToken)
|
public async Task<Result<Stream>> GetFileAsync(string bucketName, string keyName, CancellationToken cancellationToken)
|
||||||
@@ -75,18 +76,31 @@ namespace Musify.Infrastructure.Storage
|
|||||||
string fileName = Path.GetFileName(file);
|
string fileName = Path.GetFileName(file);
|
||||||
string filePath = Path.Combine(sourceDirectory, file);
|
string filePath = Path.Combine(sourceDirectory, file);
|
||||||
|
|
||||||
|
var upload = new Upload
|
||||||
|
{
|
||||||
|
Id = Guid.NewGuid(),
|
||||||
|
BucketName = bucketName,
|
||||||
|
KeyName = $"{route}/{fileName}",
|
||||||
|
ContentType = "Application/Octet-Stream"
|
||||||
|
};
|
||||||
|
await database.Uploads.AddAsync(upload, cancellationToken);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await trasnsferUtility.UploadAsync(filePath, bucketName, fileName, cancellationToken);
|
await trasnsferUtility.UploadAsync(filePath, bucketName, fileName, cancellationToken);
|
||||||
|
upload.State = UploadState.Successful;
|
||||||
logger.LogInformation("Successfully transferred file {FileName} to S3 with bucket name {BucketName} and route {Route}", fileName, bucketName, route);
|
logger.LogInformation("Successfully transferred file {FileName} to S3 with bucket name {BucketName} and route {Route}", fileName, bucketName, route);
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
logger.LogError("Failed to transfer file {FileName} to S3 with bucket name {BucketName} and route {Route}", fileName, bucketName, route);
|
logger.LogError("Failed to transfer file {FileName} to S3 with bucket name {BucketName} and route {Route}", fileName, bucketName, route);
|
||||||
return Result.Error($"Failed to transfer file {fileName} to S3 with bucket name {bucketName} and route {route}");
|
upload.State = UploadState.Failed;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await database.SaveChangesAsync(cancellationToken);
|
||||||
|
logger.LogInformation("Successfully saved upload records to database for all files transferred to S3 with bucket name {BucketName} and route {Route}", bucketName, route);
|
||||||
|
|
||||||
return Result.Success();
|
return Result.Success();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,6 +114,15 @@ namespace Musify.Infrastructure.Storage
|
|||||||
Key = keyName
|
Key = keyName
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var upload = new Upload
|
||||||
|
{
|
||||||
|
Id = Guid.NewGuid(),
|
||||||
|
BucketName = bucketName,
|
||||||
|
KeyName = keyName,
|
||||||
|
ContentType = contentType
|
||||||
|
};
|
||||||
|
await database.Uploads.AddAsync(upload, cancellationToken);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var response = await amazonS3.PutObjectAsync(request, cancellationToken);
|
var response = await amazonS3.PutObjectAsync(request, cancellationToken);
|
||||||
@@ -108,9 +131,14 @@ namespace Musify.Infrastructure.Storage
|
|||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
logger.LogError("Failed to upload file to S3 with bucket name {BucketName} and key name {KeyName}", bucketName, keyName);
|
logger.LogError("Failed to upload file to S3 with bucket name {BucketName} and key name {KeyName}", bucketName, keyName);
|
||||||
|
upload.State = UploadState.Failed;
|
||||||
return Result.Error($"Failed to upload file to S3 with bucket name {bucketName} and key name {keyName}");
|
return Result.Error($"Failed to upload file to S3 with bucket name {bucketName} and key name {keyName}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
upload.State = UploadState.Successful;
|
||||||
|
await database.SaveChangesAsync(cancellationToken);
|
||||||
|
logger.LogInformation("Successfully saved upload record to database with bucket name {BucketName} and key name {KeyName}", bucketName, keyName);
|
||||||
|
|
||||||
return Result.Success(keyName);
|
return Result.Success(keyName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user