Small changes
This commit is contained in:
@@ -28,6 +28,7 @@ namespace Musify.Application.Pictures
|
|||||||
BucketName = storageConfiguration.BucketName,
|
BucketName = storageConfiguration.BucketName,
|
||||||
EntityType = entityType,
|
EntityType = entityType,
|
||||||
EntityId = EntityId,
|
EntityId = EntityId,
|
||||||
|
State = UploadState.Completed
|
||||||
};
|
};
|
||||||
|
|
||||||
await database.Uploads.AddAsync(upload, cancellationToken);
|
await database.Uploads.AddAsync(upload, cancellationToken);
|
||||||
@@ -44,6 +45,11 @@ namespace Musify.Application.Pictures
|
|||||||
if (!uploadResult.IsSuccess)
|
if (!uploadResult.IsSuccess)
|
||||||
{
|
{
|
||||||
logger.LogError("Failed to upload the picture with ID {PictureId} to bucket {BucketName}.", pictureId, storageConfiguration.BucketName);
|
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.");
|
return Result.Error("Failed to upload the picture.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
using MassTransit;
|
using MassTransit;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Musify.Application.Contracts.Infrastructure;
|
using Musify.Application.Contracts.Infrastructure;
|
||||||
|
using Musify.Application.Pictures.Contracts;
|
||||||
using Musify.Application.Pictures.Events;
|
using Musify.Application.Pictures.Events;
|
||||||
using Musify.Domain.Entities;
|
using Musify.Domain.Entities;
|
||||||
|
|
||||||
@@ -24,9 +25,9 @@ namespace Musify.Infrastructure.Messaging.Consumers
|
|||||||
return;
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,8 +52,6 @@ namespace Musify.Infrastructure.Messaging.Consumers
|
|||||||
|
|
||||||
foreach (var resize in consumeContext.Message.ImageResizes)
|
foreach (var resize in consumeContext.Message.ImageResizes)
|
||||||
{
|
{
|
||||||
memoryStream.Position = 0;
|
|
||||||
|
|
||||||
var newUploadId = Guid.NewGuid();
|
var newUploadId = Guid.NewGuid();
|
||||||
|
|
||||||
var newUpload = new Upload
|
var newUpload = new Upload
|
||||||
@@ -70,34 +69,16 @@ namespace Musify.Infrastructure.Messaging.Consumers
|
|||||||
|
|
||||||
await database.SaveChangesAsync(consumeContext.CancellationToken);
|
await database.SaveChangesAsync(consumeContext.CancellationToken);
|
||||||
|
|
||||||
var pictureResult = await pictureHandler.ResizePictureAsync(
|
try
|
||||||
memoryStream,
|
|
||||||
resize.Width,
|
|
||||||
resize.Height,
|
|
||||||
consumeContext.CancellationToken);
|
|
||||||
|
|
||||||
if (!pictureResult.IsSuccess)
|
|
||||||
{
|
{
|
||||||
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);
|
await UpdateState(database, newUpload, UploadState.Failed, consumeContext.CancellationToken);
|
||||||
continue;
|
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);
|
await UpdateState(database, upload, UploadState.Completed, consumeContext.CancellationToken);
|
||||||
@@ -119,5 +100,39 @@ namespace Musify.Infrastructure.Messaging.Consumers
|
|||||||
upload.State = uploadState;
|
upload.State = uploadState;
|
||||||
database.Uploads.Update(upload);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user