From 41b8cbfc9c5818493df3894b16c360ec4473be68 Mon Sep 17 00:00:00 2001 From: XShine555 Date: Wed, 18 Mar 2026 19:07:01 +0100 Subject: [PATCH] Small changes --- Application/Pictures/PictureService.cs | 6 ++ .../Consumers/PictureResizerConsumer.cs | 71 +++++++++++-------- 2 files changed, 49 insertions(+), 28 deletions(-) diff --git a/Application/Pictures/PictureService.cs b/Application/Pictures/PictureService.cs index c061995..7ee8348 100644 --- a/Application/Pictures/PictureService.cs +++ b/Application/Pictures/PictureService.cs @@ -28,6 +28,7 @@ namespace Musify.Application.Pictures BucketName = storageConfiguration.BucketName, EntityType = entityType, EntityId = EntityId, + State = UploadState.Completed }; await database.Uploads.AddAsync(upload, cancellationToken); @@ -44,6 +45,11 @@ namespace Musify.Application.Pictures 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."); } diff --git a/Infrastructure/Messaging/Consumers/PictureResizerConsumer.cs b/Infrastructure/Messaging/Consumers/PictureResizerConsumer.cs index 04b1dd5..28b6bdb 100644 --- a/Infrastructure/Messaging/Consumers/PictureResizerConsumer.cs +++ b/Infrastructure/Messaging/Consumers/PictureResizerConsumer.cs @@ -2,6 +2,7 @@ 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; @@ -24,9 +25,9 @@ namespace Musify.Infrastructure.Messaging.Consumers return; } - if (upload.State != UploadState.Pending) + if (upload.State != UploadState.Completed) { - logger.LogInformation("Upload is not in a pending state, skipping processing: UploadId={UploadId}, State={State}", consumeContext.Message.UploadId, upload.State); + logger.LogInformation("Upload is not in a Completed state, skipping processing: UploadId={UploadId}, State={State}", consumeContext.Message.UploadId, upload.State); return; } @@ -51,8 +52,6 @@ namespace Musify.Infrastructure.Messaging.Consumers foreach (var resize in consumeContext.Message.ImageResizes) { - memoryStream.Position = 0; - var newUploadId = Guid.NewGuid(); var newUpload = new Upload @@ -70,34 +69,16 @@ namespace Musify.Infrastructure.Messaging.Consumers await database.SaveChangesAsync(consumeContext.CancellationToken); - var pictureResult = await pictureHandler.ResizePictureAsync( - memoryStream, - resize.Width, - resize.Height, - consumeContext.CancellationToken); - - if (!pictureResult.IsSuccess) + try { - logger.LogError("Failed to resize picture: {ErrorMessage}", string.Join("; ", pictureResult.Errors)); + 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; } - - var saveResult = await storageHandler.UploadFileAsync( - pictureResult.Value, - "image/webp", - upload.BucketName, - $"{resize.SaveOnRoute}/{newUploadId}.webp", - consumeContext.CancellationToken); - - if (!saveResult.IsSuccess) - { - logger.LogError("Failed to upload resized picture: {ErrorMessage}", string.Join("; ", saveResult.Errors)); - await UpdateState(database, newUpload, UploadState.Failed, consumeContext.CancellationToken); - continue; - } - - await UpdateState(database, newUpload, UploadState.Completed, consumeContext.CancellationToken); } await UpdateState(database, upload, UploadState.Completed, consumeContext.CancellationToken); @@ -119,5 +100,39 @@ namespace Musify.Infrastructure.Messaging.Consumers 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); + } } } \ No newline at end of file