Changes
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
namespace Musify.Infrastructure.Messaging.Activities.Arguments
|
||||
{
|
||||
public record CopyFileArguments(
|
||||
string SourceBucketName,
|
||||
string SourceKeyName,
|
||||
string DestinationBucketName,
|
||||
string DestinationKeyName);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Musify.Infrastructure.Messaging.Activities.Arguments
|
||||
{
|
||||
public record RemoveFileArguments(
|
||||
string BucketName,
|
||||
string KeyName);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace Musify.Infrastructure.Messaging.Activities.Arguments
|
||||
{
|
||||
public record ResizePictureArgument(
|
||||
string OriginalBucketName,
|
||||
string OriginalKeyName,
|
||||
string DestinationBucketName,
|
||||
string DestinationKeyName,
|
||||
int Width,
|
||||
int Height);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Musify.Infrastructure.Messaging.Activities.Arguments
|
||||
{
|
||||
public record UpdatePlayListPictureArguments(
|
||||
Guid PlayListId,
|
||||
string SmallPictureKeyName,
|
||||
string MediumPictureKeyName,
|
||||
string LargePictureKeyName);
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
using MassTransit;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Musify.Application.Contracts.Infrastructure;
|
||||
using Musify.Infrastructure.Messaging.Activities.Arguments;
|
||||
|
||||
namespace Musify.Infrastructure.Messaging.Activities
|
||||
{
|
||||
public class CopyFilesActivity(IStorageHandler storageHandler, ILogger<CopyFilesActivity> logger)
|
||||
: IExecuteActivity<CopyFileArguments[]>
|
||||
{
|
||||
public async Task<ExecutionResult> Execute(ExecuteContext<CopyFileArguments[]> executeContext)
|
||||
{
|
||||
foreach (var file in executeContext.Arguments)
|
||||
{
|
||||
var copyFileResult = await storageHandler.CopyFileAsync(
|
||||
file.SourceBucketName,
|
||||
file.SourceKeyName,
|
||||
file.DestinationBucketName,
|
||||
file.SourceKeyName,
|
||||
executeContext.CancellationToken);
|
||||
|
||||
if (!copyFileResult.IsSuccess)
|
||||
{
|
||||
string errorMessage = string.Join("; ", copyFileResult.Errors);
|
||||
logger.LogError(
|
||||
"Failed to copy file from {SourceBucket}/{SourceKey} to {DestinationBucket}/{DestinationKey}. Error: {ErrorMessage}",
|
||||
file.SourceBucketName,
|
||||
file.SourceKeyName,
|
||||
file.DestinationBucketName,
|
||||
file.DestinationKeyName,
|
||||
errorMessage);
|
||||
throw new Exception(errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
return executeContext.Completed();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using MassTransit;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Musify.Application.Contracts.Infrastructure;
|
||||
using Musify.Infrastructure.Messaging.Activities.Arguments;
|
||||
|
||||
namespace Musify.Infrastructure.Messaging.Activities
|
||||
{
|
||||
public class RemoveFilesActivity(IStorageHandler storageHandler, ILogger<RemoveFilesActivity> logger)
|
||||
: IExecuteActivity<RemoveFileArguments[]>
|
||||
{
|
||||
public async Task<ExecutionResult> Execute(ExecuteContext<RemoveFileArguments[]> executeContext)
|
||||
{
|
||||
foreach (var file in executeContext.Arguments)
|
||||
{
|
||||
var removeFile = await storageHandler.RemoveFileAsync(
|
||||
file.BucketName,
|
||||
file.KeyName,
|
||||
executeContext.CancellationToken);
|
||||
|
||||
if (!removeFile.IsSuccess)
|
||||
{
|
||||
string errorMessage = string.Join(";", removeFile.Errors);
|
||||
logger.LogWarning("Failed to remove file {File} from bucket {BucketName}: {ErrorMessage}",
|
||||
file, file.BucketName, errorMessage);
|
||||
}
|
||||
}
|
||||
return executeContext.Completed();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
using MassTransit;
|
||||
using Musify.Application.Contracts.Infrastructure;
|
||||
using Musify.Infrastructure.Messaging.Activities.Arguments;
|
||||
|
||||
namespace Musify.Infrastructure.Messaging.Activities
|
||||
{
|
||||
public class ResizePictureActivity(IStorageHandler storageHandler, IPictureHandler pictureHandler)
|
||||
: IExecuteActivity<ResizePictureArgument>
|
||||
{
|
||||
public async Task<ExecutionResult> Execute(ExecuteContext<ResizePictureArgument> executeContext)
|
||||
{
|
||||
var file = await storageHandler.GetFileAsync(
|
||||
executeContext.Arguments.OriginalBucketName,
|
||||
executeContext.Arguments.OriginalKeyName,
|
||||
executeContext.CancellationToken);
|
||||
|
||||
if (!file.IsSuccess)
|
||||
{
|
||||
string errorMessages = string.Join(", ", file.Errors);
|
||||
throw new Exception(errorMessages);
|
||||
}
|
||||
|
||||
var resizedPicture = await pictureHandler.ResizePictureAsync(
|
||||
file.Value,
|
||||
executeContext.Arguments.Width,
|
||||
executeContext.Arguments.Height,
|
||||
executeContext.CancellationToken);
|
||||
|
||||
if (!resizedPicture.IsSuccess)
|
||||
{
|
||||
string errorMessage = string.Join(", ", resizedPicture.Errors);
|
||||
throw new Exception(errorMessage);
|
||||
}
|
||||
|
||||
var uploadFile = await storageHandler.UploadFileAsync(
|
||||
resizedPicture.Value,
|
||||
pictureHandler.ContentType,
|
||||
executeContext.Arguments.DestinationBucketName,
|
||||
executeContext.Arguments.DestinationKeyName,
|
||||
executeContext.CancellationToken);
|
||||
|
||||
if (!uploadFile.IsSuccess)
|
||||
{
|
||||
var errorMessage = string.Join(", ", uploadFile.Errors);
|
||||
throw new Exception(errorMessage);
|
||||
}
|
||||
|
||||
return executeContext.Completed();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using MassTransit;
|
||||
using Musify.Application.Contracts.Infrastructure;
|
||||
using Musify.Infrastructure.Messaging.Activities.Arguments;
|
||||
|
||||
namespace Musify.Infrastructure.Messaging.Activities
|
||||
{
|
||||
public class UpdatePlayListPictureActivity(IDatabase database)
|
||||
: IExecuteActivity<UpdatePlayListPictureArguments>
|
||||
{
|
||||
public async Task<ExecutionResult> Execute(ExecuteContext<UpdatePlayListPictureArguments> executeContext)
|
||||
{
|
||||
var PlayList = await database.PlayLists.FindAsync(executeContext.Arguments.PlayListId);
|
||||
|
||||
if (PlayList is null)
|
||||
{
|
||||
throw new Exception($"PlayList with id {executeContext.Arguments.PlayListId} not found.");
|
||||
}
|
||||
|
||||
PlayList.SmallPictureKeyName = executeContext.Arguments.SmallPictureKeyName;
|
||||
PlayList.MediumPictureKeyName = executeContext.Arguments.MediumPictureKeyName;
|
||||
PlayList.LargePictureKeyName = executeContext.Arguments.LargePictureKeyName;
|
||||
|
||||
database.PlayLists.Update(PlayList);
|
||||
await database.SaveChangesAsync(executeContext.CancellationToken);
|
||||
|
||||
return executeContext.Completed();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user