Changes and fixes
This commit is contained in:
@@ -1,11 +1,12 @@
|
|||||||
using Ardalis.Result;
|
using Ardalis.Result;
|
||||||
using Musify.Application.Pictures.Contracts;
|
using Musify.Application.Pictures.Contracts;
|
||||||
|
using Musify.Domain.Entities;
|
||||||
|
|
||||||
namespace Musify.Application.Contracts.Application
|
namespace Musify.Application.Contracts.Application
|
||||||
{
|
{
|
||||||
public interface IPictureService
|
public interface IPictureService
|
||||||
{
|
{
|
||||||
Task<Result<Guid>> ResizePictureAsync(string keyName, string contentType, Stream pictureStream, PictureResize[] pictureResizes,
|
Task<Result<Guid>> ResizePictureAsync(EntityType entityType, Guid EntityId, string keyName, string contentType, Stream pictureStream,
|
||||||
CancellationToken cancellationToken);
|
PictureResize[] pictureResizes, CancellationToken cancellationToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,10 +1,13 @@
|
|||||||
using Ardalis.Result;
|
using Ardalis.Result;
|
||||||
using DispatchR.Abstractions.Send;
|
using DispatchR.Abstractions.Send;
|
||||||
using Musify.Application.Pictures.Contracts;
|
using Musify.Application.Pictures.Contracts;
|
||||||
|
using Musify.Domain.Entities;
|
||||||
|
|
||||||
namespace Musify.Application.Pictures.Commands.ResizePicture
|
namespace Musify.Application.Pictures.Commands.ResizePicture
|
||||||
{
|
{
|
||||||
public record ResizeCommand(
|
public record ResizeCommand(
|
||||||
|
EntityType EntityType,
|
||||||
|
Guid EntityId,
|
||||||
string KeyName,
|
string KeyName,
|
||||||
string ContentType,
|
string ContentType,
|
||||||
Stream PictureStream,
|
Stream PictureStream,
|
||||||
|
|||||||
@@ -9,7 +9,8 @@ namespace Musify.Application.Pictures.Commands.ResizePicture
|
|||||||
{
|
{
|
||||||
public async Task<Result<Guid>> Handle(ResizeCommand request, CancellationToken cancellationToken)
|
public async Task<Result<Guid>> Handle(ResizeCommand request, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var result = await pictureService.ResizePictureAsync(request.KeyName, request.ContentType, request.PictureStream, request.PictureResizes, cancellationToken);
|
var result = await pictureService.ResizePictureAsync(request.EntityType, request.EntityId, request.KeyName, request.ContentType,
|
||||||
|
request.PictureStream, request.PictureResizes, cancellationToken);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,5 +2,6 @@
|
|||||||
{
|
{
|
||||||
public record PictureResize(
|
public record PictureResize(
|
||||||
int Width,
|
int Width,
|
||||||
int Height);
|
int Height,
|
||||||
|
string SaveOnRoute);
|
||||||
}
|
}
|
||||||
@@ -15,8 +15,8 @@ namespace Musify.Application.Pictures
|
|||||||
ILogger<ResizeCommandHandler> logger, StorageConfiguration storageConfiguration)
|
ILogger<ResizeCommandHandler> logger, StorageConfiguration storageConfiguration)
|
||||||
: IPictureService
|
: IPictureService
|
||||||
{
|
{
|
||||||
public async Task<Result<Guid>> ResizePictureAsync(string keyName, string contentType, Stream pictureStream, PictureResize[] pictureResizes,
|
public async Task<Result<Guid>> ResizePictureAsync(EntityType entityType, Guid EntityId, string keyName, string contentType, Stream pictureStream,
|
||||||
CancellationToken cancellationToken)
|
PictureResize[] pictureResizes, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var pictureId = Guid.NewGuid();
|
var pictureId = Guid.NewGuid();
|
||||||
|
|
||||||
@@ -26,6 +26,8 @@ namespace Musify.Application.Pictures
|
|||||||
KeyName = $"{storageConfiguration.Routes.Uploads}/{pictureId}",
|
KeyName = $"{storageConfiguration.Routes.Uploads}/{pictureId}",
|
||||||
ContentType = contentType,
|
ContentType = contentType,
|
||||||
BucketName = storageConfiguration.BucketName,
|
BucketName = storageConfiguration.BucketName,
|
||||||
|
EntityType = entityType,
|
||||||
|
EntityId = EntityId,
|
||||||
};
|
};
|
||||||
|
|
||||||
await database.Uploads.AddAsync(upload, cancellationToken);
|
await database.Uploads.AddAsync(upload, cancellationToken);
|
||||||
|
|||||||
@@ -43,19 +43,24 @@ namespace Musify.Application.PlayLists.Commands.CreatePlayList
|
|||||||
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 pictureService.ResizePictureAsync(
|
||||||
|
EntityType.PlayList,
|
||||||
|
playList.Id,
|
||||||
playList.Id.ToString(),
|
playList.Id.ToString(),
|
||||||
request.PictureContentType,
|
request.PictureContentType,
|
||||||
request.PictureStream,
|
request.PictureStream,
|
||||||
[
|
[
|
||||||
new PictureResize(playListConfiguration.SmallPictureWidth, playListConfiguration.SmallPictureHeight),
|
new PictureResize(playListConfiguration.SmallPictureWidth,
|
||||||
new PictureResize(playListConfiguration.MediumPictureWidth, playListConfiguration.MediumPictureHeight),
|
playListConfiguration.SmallPictureHeight, playListConfiguration.Routes.SmallPictures),
|
||||||
new PictureResize(playListConfiguration.LargePictureWidth, playListConfiguration.LargePictureHeight)
|
new PictureResize(playListConfiguration.MediumPictureWidth,
|
||||||
|
playListConfiguration.MediumPictureHeight, playListConfiguration.Routes.MediumPictures),
|
||||||
|
new PictureResize(playListConfiguration.LargePictureWidth,
|
||||||
|
playListConfiguration.LargePictureHeight, 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);
|
||||||
|
|
||||||
return Result.Created(PlayListResponse.Map(playList));
|
return Result.Created(PlayListResponse.FromEntity(playList));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -12,7 +12,7 @@ namespace Musify.Application.PlayLists.Contracts
|
|||||||
DateTime CreatedAt,
|
DateTime CreatedAt,
|
||||||
DateTime UpdatedAt)
|
DateTime UpdatedAt)
|
||||||
{
|
{
|
||||||
public static PlayListResponse Map(PlayList playList)
|
public static PlayListResponse FromEntity(PlayList playList)
|
||||||
{
|
{
|
||||||
return new PlayListResponse(
|
return new PlayListResponse(
|
||||||
playList.Id,
|
playList.Id,
|
||||||
|
|||||||
@@ -16,13 +16,7 @@ namespace Musify.Application.PlayLists.Queries.GetPlayListById
|
|||||||
.SingleOrDefaultAsync(p => p.Id == request.Id, cancellationToken);
|
.SingleOrDefaultAsync(p => p.Id == request.Id, cancellationToken);
|
||||||
|
|
||||||
return playList is not null
|
return playList is not null
|
||||||
? Result.Success(new PlayListResponse(
|
? Result.Success(PlayListResponse.FromEntity(playList))
|
||||||
playList.Id,
|
|
||||||
playList.Name,
|
|
||||||
playList.Description,
|
|
||||||
playList.ImageName,
|
|
||||||
playList.CreatedDate,
|
|
||||||
playList.UpdatedDate))
|
|
||||||
: Result.NotFound("Playlist not found.");
|
: Result.NotFound("Playlist not found.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ using DispatchR.Abstractions.Send;
|
|||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Musify.Application.Contracts.Infrastructure;
|
using Musify.Application.Contracts.Infrastructure;
|
||||||
using Musify.Application.PlayLists.Contracts;
|
using Musify.Application.PlayLists.Contracts;
|
||||||
|
using Musify.Domain.Entities;
|
||||||
using X.PagedList.EF;
|
using X.PagedList.EF;
|
||||||
|
|
||||||
namespace Musify.Application.PlayLists.Queries.GetPlayLists
|
namespace Musify.Application.PlayLists.Queries.GetPlayLists
|
||||||
@@ -20,13 +21,7 @@ namespace Musify.Application.PlayLists.Queries.GetPlayLists
|
|||||||
var pagedPlayLists = await database.PlayLists
|
var pagedPlayLists = await database.PlayLists
|
||||||
.AsNoTracking()
|
.AsNoTracking()
|
||||||
.OrderBy(p => p.Id)
|
.OrderBy(p => p.Id)
|
||||||
.Select(p => new PlayListResponse(
|
.Select(p => PlayListResponse.FromEntity(p))
|
||||||
p.Id,
|
|
||||||
p.Name,
|
|
||||||
p.Description,
|
|
||||||
p.ImageName,
|
|
||||||
p.CreatedDate,
|
|
||||||
p.UpdatedDate))
|
|
||||||
.ToPagedListAsync(pageNumber, pageSize, totalCount, cancellationToken);
|
.ToPagedListAsync(pageNumber, pageSize, totalCount, cancellationToken);
|
||||||
|
|
||||||
var response = new PaginatedPlayListResponse(
|
var response = new PaginatedPlayListResponse(
|
||||||
|
|||||||
+2
-7
@@ -3,6 +3,7 @@ using DispatchR.Abstractions.Send;
|
|||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Musify.Application.Contracts.Infrastructure;
|
using Musify.Application.Contracts.Infrastructure;
|
||||||
using Musify.Application.PlayLists.Contracts;
|
using Musify.Application.PlayLists.Contracts;
|
||||||
|
using Musify.Domain.Entities;
|
||||||
using X.PagedList.EF;
|
using X.PagedList.EF;
|
||||||
|
|
||||||
namespace Musify.Application.PlayLists.Queries.GetPlayListsByUserId
|
namespace Musify.Application.PlayLists.Queries.GetPlayListsByUserId
|
||||||
@@ -27,13 +28,7 @@ namespace Musify.Application.PlayLists.Queries.GetPlayListsByUserId
|
|||||||
|
|
||||||
var pagedPlayLists = await playListsQuery
|
var pagedPlayLists = await playListsQuery
|
||||||
.OrderBy(p => p.CreatedDate)
|
.OrderBy(p => p.CreatedDate)
|
||||||
.Select(p => new PlayListResponse(
|
.Select(p => PlayListResponse.FromEntity(p))
|
||||||
p.Id,
|
|
||||||
p.Name,
|
|
||||||
p.Description,
|
|
||||||
p.ImageName,
|
|
||||||
p.CreatedDate,
|
|
||||||
p.UpdatedDate))
|
|
||||||
.ToPagedListAsync(pageNumber, pageSize, totalCount, cancellationToken);
|
.ToPagedListAsync(pageNumber, pageSize, totalCount, cancellationToken);
|
||||||
|
|
||||||
var response = new PaginatedPlayListResponse(
|
var response = new PaginatedPlayListResponse(
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
using Ardalis.Result;
|
using Ardalis.Result;
|
||||||
using MassTransit;
|
using MassTransit;
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Musify.Application.Contracts.Infrastructure;
|
using Musify.Application.Contracts.Infrastructure;
|
||||||
using Musify.Application.Pictures.Events;
|
using Musify.Application.Pictures.Events;
|
||||||
@@ -15,7 +14,7 @@ namespace Musify.Infrastructure.Messaging.Consumers
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var upload = await database.Uploads.SingleOrDefaultAsync(u => u.Id == consumeContext.Message.UploadId);
|
var upload = await database.Uploads.FindAsync(consumeContext.Message.UploadId);
|
||||||
|
|
||||||
if (upload is null)
|
if (upload is null)
|
||||||
{
|
{
|
||||||
@@ -31,53 +30,64 @@ namespace Musify.Infrastructure.Messaging.Consumers
|
|||||||
|
|
||||||
await UpdateState(database, upload, UploadState.Processing, consumeContext.CancellationToken);
|
await UpdateState(database, upload, UploadState.Processing, consumeContext.CancellationToken);
|
||||||
|
|
||||||
var file = await storageHandler.GetFileAsync(
|
var fileResult = await storageHandler.GetFileAsync(
|
||||||
upload.BucketName,
|
upload.BucketName,
|
||||||
upload.KeyName,
|
upload.KeyName,
|
||||||
consumeContext.CancellationToken);
|
consumeContext.CancellationToken);
|
||||||
|
|
||||||
if (file.IsNotFound())
|
if (fileResult.IsNotFound())
|
||||||
{
|
{
|
||||||
logger.LogWarning("File not found in storage: BucketName={BucketName}, KeyName={KeyName}", upload.BucketName, upload.KeyName);
|
logger.LogWarning("File not found in storage: BucketName={BucketName}, KeyName={KeyName}", upload.BucketName, upload.KeyName);
|
||||||
|
|
||||||
await UpdateState(database, upload, UploadState.Failed, consumeContext.CancellationToken);
|
await UpdateState(database, upload, UploadState.Failed, consumeContext.CancellationToken);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
using var fileStream = file.Value;
|
using var fileStream = fileResult.Value;
|
||||||
|
|
||||||
var pictureResult = await pictureHandler.ResizePictureAsync(
|
foreach (var resize in consumeContext.Message.ImageResizes)
|
||||||
fileStream,
|
|
||||||
consumeContext.Message.Width,
|
|
||||||
consumeContext.Message.Height,
|
|
||||||
consumeContext.CancellationToken);
|
|
||||||
|
|
||||||
if (!pictureResult.IsSuccess)
|
|
||||||
{
|
{
|
||||||
string errors = string.Join("; ", pictureResult.Errors);
|
var newUpload = new Upload
|
||||||
logger.LogError("Failed to resize picture: {ErrorMessage}", errors);
|
{
|
||||||
|
BucketName = upload.BucketName,
|
||||||
|
KeyName = $"{resize.SaveOnRoute}/{upload.Id}.webp",
|
||||||
|
ContentType = "image/webp",
|
||||||
|
State = UploadState.Processing,
|
||||||
|
EntityId = upload.EntityId,
|
||||||
|
EntityType = upload.EntityType
|
||||||
|
};
|
||||||
|
|
||||||
await UpdateState(database, upload, UploadState.Failed, consumeContext.CancellationToken);
|
await database.Uploads.AddAsync(newUpload, consumeContext.CancellationToken);
|
||||||
|
|
||||||
return;
|
await database.SaveChangesAsync(consumeContext.CancellationToken);
|
||||||
}
|
|
||||||
|
|
||||||
var saveResult = await storageHandler.UploadFileAsync(
|
var pictureResult = await pictureHandler.ResizePictureAsync(
|
||||||
pictureResult.Value,
|
fileStream,
|
||||||
"image/webp",
|
resize.Width,
|
||||||
upload.BucketName,
|
resize.Height,
|
||||||
$"{consumeContext.Message.SaveRoute}/{upload.Id}.webp",
|
consumeContext.CancellationToken);
|
||||||
consumeContext.CancellationToken);
|
|
||||||
|
|
||||||
if (!saveResult.IsSuccess)
|
if (!pictureResult.IsSuccess)
|
||||||
{
|
{
|
||||||
string errors = string.Join("; ", saveResult.Errors);
|
logger.LogError("Failed to resize picture: {ErrorMessage}", string.Join("; ", pictureResult.Errors));
|
||||||
logger.LogError("Failed to upload resized picture: {ErrorMessage}", errors);
|
await UpdateState(database, newUpload, UploadState.Failed, consumeContext.CancellationToken);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
await UpdateState(database, upload, UploadState.Failed, consumeContext.CancellationToken);
|
var saveResult = await storageHandler.UploadFileAsync(
|
||||||
|
pictureResult.Value,
|
||||||
|
"image/webp",
|
||||||
|
upload.BucketName,
|
||||||
|
$"{resize.SaveOnRoute}/{upload.Id}.webp",
|
||||||
|
consumeContext.CancellationToken);
|
||||||
|
|
||||||
return;
|
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);
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
namespace Musify.Domain.Entities
|
||||||
|
{
|
||||||
|
public enum EntityType
|
||||||
|
{
|
||||||
|
User,
|
||||||
|
Track,
|
||||||
|
PlayList
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -18,6 +18,12 @@ namespace Musify.Domain.Entities
|
|||||||
[Required]
|
[Required]
|
||||||
public required string ContentType { get; set; }
|
public required string ContentType { get; set; }
|
||||||
|
|
||||||
|
[Required]
|
||||||
|
public required EntityType EntityType { get; set; }
|
||||||
|
|
||||||
|
[Required]
|
||||||
|
public required Guid EntityId { get; set; }
|
||||||
|
|
||||||
[Required]
|
[Required]
|
||||||
public UploadState State { get; set; } = UploadState.Pending;
|
public UploadState State { get; set; } = UploadState.Pending;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user