This commit is contained in:
2026-05-13 02:20:22 +02:00
parent 57a5358701
commit fa6eaf5a2b
27 changed files with 445 additions and 227 deletions
@@ -16,5 +16,7 @@
Task CopyFileAsync(string sourceBucket, string sourceKey, string destinationBucket, string destinationKey, Task CopyFileAsync(string sourceBucket, string sourceKey, string destinationBucket, string destinationKey,
CancellationToken cancellationToken); CancellationToken cancellationToken);
Task RemoveFolderAsync(string bucket, string folderKey, CancellationToken cancellationToken);
} }
} }
+6
View File
@@ -0,0 +1,6 @@
namespace Musify.Application.Events
{
public record DeleteTrackEvent(
Guid TrackId,
Guid UserId);
}
@@ -2,15 +2,17 @@ using Ardalis.Result;
using Mediator; using Mediator;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Musify.Application.Configuration;
using Musify.Application.Contracts.Infrastructure; using Musify.Application.Contracts.Infrastructure;
using Musify.Application.Tracks.Commands; using Musify.Application.Tracks.Commands;
using Musify.Domain.Entities; using Musify.Domain.Entities;
using Musify.Application.Events;
namespace Musify.Application.Tracks.Handler namespace Musify.Application.Tracks.Handler
{ {
public class DeleteTrackCommandHandler(IDatabase database, IStorageService storageHandler, ILogger<DeleteTrackCommandHandler> logger, public class DeleteTrackCommandHandler(
TrackConfiguration trackConfiguration, ApplicationStorageConfiguration storageConfiguration) IDatabase database,
IEventBus eventBus,
ILogger<DeleteTrackCommandHandler> logger)
: ICommandHandler<DeleteTrackCommand, Result> : ICommandHandler<DeleteTrackCommand, Result>
{ {
public async ValueTask<Result> Handle(DeleteTrackCommand request, CancellationToken cancellationToken) public async ValueTask<Result> Handle(DeleteTrackCommand request, CancellationToken cancellationToken)
@@ -28,48 +30,22 @@ namespace Musify.Application.Tracks.Handler
return Result.Unauthorized(); return Result.Unauthorized();
} }
await RemovePictures(track, cancellationToken); if (track.AudioTranscodeProcessingStatus == ProcessingStatus.Processing
await RemoveAudios(track, cancellationToken); || track.AudioTranscodeProcessingStatus == ProcessingStatus.Pending)
{
logger.LogWarning("Track {TrackId} is currently being processed and cannot be deleted", request.TrackId);
return Result.Conflict("Track is currently being processed and cannot be deleted");
}
track.PicturesProcessingStatus = ProcessingStatus.Processing;
track.AudioTranscodeProcessingStatus = ProcessingStatus.Processing;
await database.SaveChangesAsync(cancellationToken);
await eventBus.PublishAsync(
new DeleteTrackEvent(track.Id, request.UserId),
cancellationToken);
return Result.NoContent(); return Result.NoContent();
} }
async Task RemovePictures(Track track, CancellationToken cancellationToken)
{
await RemoveFile(trackConfiguration.Routes.BuildOriginalPicturePath(track.OriginalPictureName),
cancellationToken);
if (track.SmallPictureName != trackConfiguration.Routes.PresetSmallPicture)
await RemoveFile(trackConfiguration.Routes.BuildSmallPicturePath(track.SmallPictureName),
cancellationToken);
if (track.MediumPictureName != trackConfiguration.Routes.PresetMediumPicture)
await RemoveFile(trackConfiguration.Routes.BuildMediumPicturePath(track.MediumPictureName),
cancellationToken);
if (track.LargePictureName != trackConfiguration.Routes.PresetLargePicture)
await RemoveFile(trackConfiguration.Routes.BuildLargePicturePath(track.LargePictureName),
cancellationToken);
}
async Task RemoveAudios(Track track, CancellationToken cancellationToken)
{
await RemoveFile(trackConfiguration.Routes.BuildOriginalAudioPath(track.OriginalAudioName),
cancellationToken);
await RemoveFile(trackConfiguration.Routes.BuildProcessedAudioPath(track.AudioFolderName),
cancellationToken);
}
async Task RemoveFile(string path, CancellationToken cancellationToken)
{
try
{
await storageHandler.RemoveFileAsync(storageConfiguration.Bucket, path, cancellationToken);
logger.LogDebug("Removed file {Path} from storage", path);
}
catch (Exception exception)
{
logger.LogError(exception, "Failed to remove file {Path} from storage", path);
}
}
} }
} }
+10
View File
@@ -0,0 +1,10 @@
namespace Musify.Domain.Entities
{
public enum ProcessingStatus
{
Pending = 0,
Processing = 1,
Completed = 2,
Failed = 3
}
}
+27 -7
View File
@@ -1,4 +1,6 @@
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Diagnostics.CodeAnalysis;
namespace Musify.Domain.Entities namespace Musify.Domain.Entities
{ {
@@ -20,24 +22,32 @@ namespace Musify.Domain.Entities
[MaxLength(64)] [MaxLength(64)]
public required string OriginalPictureName { get; set; } public required string OriginalPictureName { get; set; }
[Required]
[MaxLength(64)] [MaxLength(64)]
public required string SmallPictureName { get; set; } public string? SmallPictureName { get; set; }
[Required]
[MaxLength(64)] [MaxLength(64)]
public required string MediumPictureName { get; set; } public string? MediumPictureName { get; set; }
[Required]
[MaxLength(64)] [MaxLength(64)]
public required string LargePictureName { get; set; } public string? LargePictureName { get; set; }
[Required] [Required]
[MaxLength(64)] [MaxLength(64)]
public required string OriginalAudioName { get; set; } public required string OriginalAudioName { get; set; }
[MaxLength(64)] [MaxLength(64)]
public string AudioFolderName { get; set; } = string.Empty; public string? AudioFolderName { get; set; }
[Required]
public ProcessingStatus PicturesProcessingStatus { get; set; } = ProcessingStatus.Pending;
[Required]
public ProcessingStatus AudioTranscodeProcessingStatus { get; set; } = ProcessingStatus.Pending;
[Required]
public int RetryCount { get; set; } = 0;
public DateTime? LastRetryAt { get; set; }
[Required] [Required]
public DateTime CreatedDate { get; set; } = DateTime.UtcNow; public DateTime CreatedDate { get; set; } = DateTime.UtcNow;
@@ -48,5 +58,15 @@ namespace Musify.Domain.Entities
public ICollection<UserHasTrack> UserTracks { get; set; } = new List<UserHasTrack>(); public ICollection<UserHasTrack> UserTracks { get; set; } = new List<UserHasTrack>();
public ICollection<PlayListHasTrack> PlayListTracks { get; set; } = new List<PlayListHasTrack>(); public ICollection<PlayListHasTrack> PlayListTracks { get; set; } = new List<PlayListHasTrack>();
[NotMapped]
[MemberNotNullWhen(true, nameof(SmallPictureName)) ]
[MemberNotNullWhen(true, nameof(MediumPictureName)) ]
[MemberNotNullWhen(true, nameof(LargePictureName)) ]
public bool IsPicturesProcessed => PicturesProcessingStatus == ProcessingStatus.Completed;
[NotMapped]
[MemberNotNullWhen(true, nameof(AudioFolderName)) ]
public bool IsAudioProcessed => AudioTranscodeProcessingStatus == ProcessingStatus.Completed;
} }
} }
@@ -1,4 +1,5 @@
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.Domain.Entities; using Musify.Domain.Entities;
@@ -6,42 +7,41 @@ using Musify.Infrastructure.MassTransit.Activities.Arguments;
namespace Musify.Infrastructure.MassTransit.Activities namespace Musify.Infrastructure.MassTransit.Activities
{ {
public class GenerateAudioWorkflowPathsActivity( internal class GenerateAudioWorkflowPathsActivity(
ILogger<GenerateAudioWorkflowPathsActivity> logger, IDatabase database,
IProcessTrackingStore processTrackingStore) ILogger<GenerateAudioWorkflowPathsActivity> logger)
: IExecuteActivity<GenerateAudioWorkflowPathsArguments> : IExecuteActivity<GenerateAudioWorkflowPathsArguments>
{ {
public const string ExecuteEndpointName = "generate-audio-workflow-paths"; public const string ExecuteEndpointName = "generate-audio-workflow-paths";
public async Task<ExecutionResult> Execute(ExecuteContext<GenerateAudioWorkflowPathsArguments> executeContext) public async Task<ExecutionResult> Execute(ExecuteContext<GenerateAudioWorkflowPathsArguments> executeContext)
{ {
var processId = await processTrackingStore.GetOrCreateProcessAsync( Track track;
"RoutingSlip", try
executeContext.CorrelationId ?? executeContext.TrackingNumber, {
executeContext.ConversationId, var getTrack = await database.Tracks.SingleOrDefaultAsync(p => p.Id == executeContext.Arguments.TrackId, executeContext.CancellationToken);
executeContext.MessageId, track = getTrack ?? throw new InvalidOperationException($"Track with id {executeContext.Arguments.TrackId} not found.");
executeContext.CancellationToken); }
catch (Exception exception)
var stepId = await processTrackingStore.StartStepAsync( {
processId, logger.LogError(exception, "Failed to retrieve track for TrackId {TrackId}",
nameof(GenerateAudioWorkflowPathsActivity), executeContext.Arguments.TrackId);
ProcessStepComponentType.Activity, throw;
0, }
executeContext.CancellationToken);
try try
{ {
track.AudioTranscodeProcessingStatus = ProcessingStatus.Processing;
var folderName = Guid.NewGuid().ToString(); var folderName = Guid.NewGuid().ToString();
var sourceFileName = Path.GetFileName(executeContext.Arguments.SourceKey); var sourceFileName = Path.GetFileName(executeContext.Arguments.SourceKey);
var workingDirectory = Path.Combine(executeContext.Arguments.TemporaryRootDirectory, folderName); var workingDirectory = Path.Combine(executeContext.Arguments.TemporaryRootDirectory, folderName);
var sourceFilePath = Path.Combine(workingDirectory, sourceFileName); var sourceFilePath = Path.Combine(workingDirectory, sourceFileName);
Directory.CreateDirectory(workingDirectory); Directory.CreateDirectory(workingDirectory);
await database.SaveChangesAsync(executeContext.CancellationToken);
logger.LogDebug("Generated audio workflow paths in {WorkingDirectory}", logger.LogDebug("Generated audio workflow paths in {WorkingDirectory}", workingDirectory);
workingDirectory);
await processTrackingStore.CompleteStepAsync(processId, stepId, executeContext.CancellationToken);
return executeContext.CompletedWithVariables(new Dictionary<string, object> return executeContext.CompletedWithVariables(new Dictionary<string, object>
{ {
[RoutingSlipVariableNames.Workflow.TemporalDirectory] = workingDirectory, [RoutingSlipVariableNames.Workflow.TemporalDirectory] = workingDirectory,
@@ -54,9 +54,20 @@ namespace Musify.Infrastructure.MassTransit.Activities
{ {
logger.LogError(exception, "Failed to generate audio workflow paths for {SourceKey}", logger.LogError(exception, "Failed to generate audio workflow paths for {SourceKey}",
executeContext.Arguments.SourceKey); executeContext.Arguments.SourceKey);
await processTrackingStore.FailStepAsync(processId, stepId, exception.Message, executeContext.CancellationToken);
try
{
track.AudioTranscodeProcessingStatus = ProcessingStatus.Failed;
await database.SaveChangesAsync(executeContext.CancellationToken);
}
catch (Exception dbException)
{
logger.LogError(dbException, "Failed to update ProcessingStatus to Failed for track {TrackId}",
executeContext.Arguments.TrackId);
}
throw; throw;
} }
} }
} }
} }
@@ -1,4 +1,5 @@
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.Domain.Entities; using Musify.Domain.Entities;
@@ -7,54 +8,59 @@ using Musify.Infrastructure.MassTransit.Activities.Logs;
namespace Musify.Infrastructure.MassTransit.Activities namespace Musify.Infrastructure.MassTransit.Activities
{ {
public class TranscodeDashAudioActivity( internal class TranscodeDashAudioActivity(
IDatabase database,
IAudioTranscoderService audioTranscoder, IAudioTranscoderService audioTranscoder,
ILogger<TranscodeDashAudioActivity> logger, ILogger<TranscodeDashAudioActivity> logger)
IProcessTrackingStore processTrackingStore)
: IActivity<TranscodeDashAudioArguments, TranscodeDashAudioLog> : IActivity<TranscodeDashAudioArguments, TranscodeDashAudioLog>
{ {
public const string ExecuteEndpointName = "transcode-dash-audio"; public const string ExecuteEndpointName = "transcode-dash-audio";
public async Task<ExecutionResult> Execute(ExecuteContext<TranscodeDashAudioArguments> executeContext) public async Task<ExecutionResult> Execute(ExecuteContext<TranscodeDashAudioArguments> executeContext)
{ {
var processId = await processTrackingStore.GetOrCreateProcessAsync(
"RoutingSlip",
executeContext.CorrelationId ?? executeContext.TrackingNumber,
executeContext.ConversationId,
executeContext.MessageId,
executeContext.CancellationToken);
var stepId = await processTrackingStore.StartStepAsync(
processId,
nameof(TranscodeDashAudioActivity),
ProcessStepComponentType.Activity,
0,
executeContext.CancellationToken);
var sourceFilePath = executeContext.GetVariable<string>(executeContext.Arguments.SourceFilePathVariable); var sourceFilePath = executeContext.GetVariable<string>(executeContext.Arguments.SourceFilePathVariable);
ArgumentNullException.ThrowIfNull(sourceFilePath, nameof(sourceFilePath)); ArgumentNullException.ThrowIfNull(sourceFilePath, nameof(sourceFilePath));
var workingDirectory = executeContext.GetVariable<string>(executeContext.Arguments.WorkingDirectoryVariable); var workingDirectory = executeContext.GetVariable<string>(executeContext.Arguments.WorkingDirectoryVariable);
ArgumentNullException.ThrowIfNull(workingDirectory, nameof(workingDirectory)); ArgumentNullException.ThrowIfNull(workingDirectory, nameof(workingDirectory));
Track track;
try try
{ {
await using (var fileStream = File.OpenRead(sourceFilePath)) var getTrack = await database.Tracks.SingleOrDefaultAsync(t => t.Id == executeContext.Arguments.TrackId, executeContext.CancellationToken);
{ track = getTrack ?? throw new InvalidOperationException($"Track with Id {executeContext.Arguments.TrackId} not found");
await audioTranscoder.TranscodeToDashAsync( }
fileStream, catch (Exception exception)
workingDirectory, {
executeContext.CancellationToken); logger.LogError(exception, "Failed to retrieve track with Id {TrackId} from database", executeContext.Arguments.TrackId);
} throw;
}
try
{
await using var fileStream = File.OpenRead(sourceFilePath);
await audioTranscoder.TranscodeToDashAsync(
fileStream,
workingDirectory,
executeContext.CancellationToken);
File.Delete(sourceFilePath); File.Delete(sourceFilePath);
await processTrackingStore.CompleteStepAsync(processId, stepId, executeContext.CancellationToken);
return executeContext.Completed(); return executeContext.Completed();
} }
catch (Exception exception) catch (Exception exception)
{ {
logger.LogError(exception, "Failed to transcode {SourceFilePath}", logger.LogError(exception, "Failed to transcode {SourceFilePath}", sourceFilePath);
sourceFilePath);
await processTrackingStore.FailStepAsync(processId, stepId, exception.Message, executeContext.CancellationToken); try
{
track.AudioTranscodeProcessingStatus = ProcessingStatus.Failed;
await database.SaveChangesAsync(executeContext.CancellationToken);
}
catch (Exception dbException)
{
logger.LogError(dbException, "Failed to update ProcessingStatus to Failed for track {TrackId}",
executeContext.Arguments.TrackId);
}
throw; throw;
} }
} }
@@ -1,4 +1,5 @@
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.Domain.Entities; using Musify.Domain.Entities;
@@ -7,63 +8,76 @@ using Musify.Infrastructure.MassTransit.Logs;
namespace Musify.Infrastructure.MassTransit.Activities.Audio namespace Musify.Infrastructure.MassTransit.Activities.Audio
{ {
public class UpdateTrackAudioActivity( internal class UpdateTrackAudioActivity(
IDatabase database, IDatabase database,
ILogger<TranscodeDashAudioActivity> logger, ILogger<UpdateTrackAudioActivity> logger)
IProcessTrackingStore processTrackingStore)
: IActivity<UpdateTrackAudioArguments, UpdateTrackAudioLog> : IActivity<UpdateTrackAudioArguments, UpdateTrackAudioLog>
{ {
public const string ExecuteEndpointName = "update-track-audio"; public const string ExecuteEndpointName = "update-track-audio";
public async Task<ExecutionResult> Execute(ExecuteContext<UpdateTrackAudioArguments> executeContext) public async Task<ExecutionResult> Execute(ExecuteContext<UpdateTrackAudioArguments> executeContext)
{ {
var processId = await processTrackingStore.GetOrCreateProcessAsync( Track track;
"RoutingSlip", try
executeContext.CorrelationId ?? executeContext.TrackingNumber, {
executeContext.ConversationId, var getTrack = await database.Tracks.SingleOrDefaultAsync(t => t.Id == executeContext.Arguments.TrackId, executeContext.CancellationToken);
executeContext.MessageId, track = getTrack ?? throw new InvalidOperationException($"Track with id {executeContext.Arguments.TrackId} not found");
executeContext.CancellationToken); }
catch (Exception exception)
{
logger.LogError(exception, "Failed to retrieve track {TrackId}",
executeContext.Arguments.TrackId);
throw;
}
var stepId = await processTrackingStore.StartStepAsync( string? audioFolderKey;
processId, try
nameof(UpdateTrackAudioActivity), {
ProcessStepComponentType.Activity, audioFolderKey = executeContext.GetVariable<string>(executeContext.Arguments.AudioFolderKeyVariable);
0, logger.LogDebug("Resolved audio folder key: {AudioFolderKey}", audioFolderKey);
executeContext.CancellationToken); }
catch (Exception exception)
{
logger.LogWarning(exception, "Audio folder key variable could not be resolved for track {TrackId}", track.Id);
return executeContext.Completed();
}
if (string.IsNullOrWhiteSpace(audioFolderKey))
{
logger.LogWarning("Audio folder key is empty for track {TrackId}; skipping update", track.Id);
return executeContext.Completed();
}
try try
{ {
var track = await database.Tracks.FindAsync(
[executeContext.Arguments.TrackId],
cancellationToken: executeContext.CancellationToken);
if (track is null)
{
logger.LogWarning("Track {TrackId} not found",
executeContext.Arguments.TrackId);
throw new InvalidOperationException($"Track with id {executeContext.Arguments.TrackId} not found");
}
var log = new UpdateTrackAudioLog( var log = new UpdateTrackAudioLog(
track.Id, track.Id,
track.AudioFolderName); track.AudioFolderName);
track.AudioFolderName = Path.GetFileName(executeContext.Arguments.AudioFolderKey); track.AudioFolderName = Path.GetFileName(audioFolderKey);
track.AudioTranscodeProcessingStatus = ProcessingStatus.Completed;
database.Tracks.Update(track);
await database.SaveChangesAsync(executeContext.CancellationToken); await database.SaveChangesAsync(executeContext.CancellationToken);
logger.LogInformation("Updated track {TrackId} audio", logger.LogInformation("Updated track {TrackId} audio", executeContext.Arguments.TrackId);
executeContext.Arguments.TrackId);
await processTrackingStore.CompleteStepAsync(processId, stepId, executeContext.CancellationToken); return executeContext.Completed(log);
return executeContext.Completed();
} }
catch (Exception exception) catch (Exception exception)
{ {
logger.LogError(exception, "Failed to update track {TrackId} audio", logger.LogError(exception, "Failed to update track {TrackId} audio",
executeContext.Arguments.TrackId); executeContext.Arguments.TrackId);
await processTrackingStore.FailStepAsync(processId, stepId, exception.Message, executeContext.CancellationToken);
try
{
track.AudioTranscodeProcessingStatus = ProcessingStatus.Failed;
await database.SaveChangesAsync(executeContext.CancellationToken);
}
catch (Exception dbException)
{
logger.LogError(dbException, "Failed to update ProcessingStatus to Failed for track {TrackId}",
executeContext.Arguments.TrackId);
}
throw; throw;
} }
} }
@@ -72,25 +86,22 @@ namespace Musify.Infrastructure.MassTransit.Activities.Audio
{ {
try try
{ {
var track = await database.Tracks.FindAsync( var track = await database.Tracks.SingleOrDefaultAsync(t => t.Id == compensateContext.Log.TrackId, compensateContext.CancellationToken);
[compensateContext.Log.TrackId],
cancellationToken: compensateContext.CancellationToken);
if (track is null) if (track is null)
{ throw new InvalidOperationException($"Track with id {compensateContext.Log.TrackId} not found");
return compensateContext.Compensated();
}
track.AudioFolderName = compensateContext.Log.PreviousFolderAudioKey; track.AudioFolderName = compensateContext.Log.PreviousAudioFolderName;
track.AudioTranscodeProcessingStatus = ProcessingStatus.Failed;
database.Tracks.Update(track);
await database.SaveChangesAsync(compensateContext.CancellationToken); await database.SaveChangesAsync(compensateContext.CancellationToken);
logger.LogInformation("Compensated track {TrackId} audio", compensateContext.Log.TrackId);
return compensateContext.Compensated(); return compensateContext.Compensated();
} }
catch (Exception exception) catch (Exception exception)
{ {
logger.LogError(exception, "Failed to compensate track {TrackId} audio", compensateContext.Log.TrackId); logger.LogError(exception, "Failed to compensate track {TrackId} audio",
compensateContext.Log.TrackId);
return compensateContext.Failed(exception); return compensateContext.Failed(exception);
} }
} }
@@ -5,9 +5,9 @@ using Musify.Domain.Entities;
using Musify.Infrastructure.MassTransit.Activities.Arguments; using Musify.Infrastructure.MassTransit.Activities.Arguments;
using Musify.Infrastructure.MassTransit.Activities.Logs; using Musify.Infrastructure.MassTransit.Activities.Logs;
namespace Musify.Infrastructure.MassTransit.Activities namespace Musify.Infrastructure.MassTransit.Activities.Files
{ {
public class DownloadFileFromBucketActivity( internal class DownloadFileFromBucketActivity(
IStorageService storageHandler, IStorageService storageHandler,
ILogger<DownloadFileFromBucketActivity> logger, ILogger<DownloadFileFromBucketActivity> logger,
IProcessTrackingStore processTrackingStore) IProcessTrackingStore processTrackingStore)
@@ -1,43 +1,25 @@
using MassTransit; using MassTransit;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Musify.Application.Contracts.Infrastructure; using Musify.Application.Contracts.Infrastructure;
using Musify.Domain.Entities;
using Musify.Infrastructure.MassTransit.Activities.Arguments; using Musify.Infrastructure.MassTransit.Activities.Arguments;
namespace Musify.Infrastructure.MassTransit.Activities namespace Musify.Infrastructure.MassTransit.Activities.Files
{ {
public class RemoveFileFromBucketActivity( internal class RemoveFileFromBucketActivity(
IStorageService storageHandler, IStorageService storageHandler,
ILogger<RemoveFileFromBucketActivity> logger, ILogger<RemoveFileFromBucketActivity> logger)
IProcessTrackingStore processTrackingStore)
: IExecuteActivity<RemoveFileFromBucketArguments> : IExecuteActivity<RemoveFileFromBucketArguments>
{ {
public const string ExecuteEndpointName = "remove-file-from-bucket"; public const string ExecuteEndpointName = "remove-file-from-bucket";
public async Task<ExecutionResult> Execute(ExecuteContext<RemoveFileFromBucketArguments> executeContext) public async Task<ExecutionResult> Execute(ExecuteContext<RemoveFileFromBucketArguments> executeContext)
{ {
var processId = await processTrackingStore.GetOrCreateProcessAsync(
"RoutingSlip",
executeContext.CorrelationId ?? executeContext.TrackingNumber,
executeContext.ConversationId,
executeContext.MessageId,
executeContext.CancellationToken);
var stepId = await processTrackingStore.StartStepAsync(
processId,
nameof(RemoveFileFromBucketActivity),
ProcessStepComponentType.Activity,
0,
executeContext.CancellationToken);
try try
{ {
await storageHandler.RemoveFileAsync( await storageHandler.RemoveFileAsync(
executeContext.Arguments.Bucket, executeContext.Arguments.Bucket,
executeContext.Arguments.Key, executeContext.Arguments.Key,
executeContext.CancellationToken); executeContext.CancellationToken);
await processTrackingStore.CompleteStepAsync(processId, stepId, executeContext.CancellationToken);
return executeContext.Completed(); return executeContext.Completed();
} }
catch (Exception exception) catch (Exception exception)
@@ -45,7 +27,6 @@ namespace Musify.Infrastructure.MassTransit.Activities
logger.LogError(exception, "Failed to remove {Key} from bucket {Bucket}", logger.LogError(exception, "Failed to remove {Key} from bucket {Bucket}",
executeContext.Arguments.Key, executeContext.Arguments.Key,
executeContext.Arguments.Bucket); executeContext.Arguments.Bucket);
await processTrackingStore.FailStepAsync(processId, stepId, exception.Message, executeContext.CancellationToken);
throw; throw;
} }
} }
@@ -0,0 +1,42 @@
using MassTransit;
using Microsoft.Extensions.Logging;
using Musify.Application.Contracts.Infrastructure;
using Musify.Infrastructure.MassTransit.Arguments;
namespace Musify.Infrastructure.MassTransit.Activities.Files
{
internal class RemoveFolderFromBucketActivity(
IStorageService storageHandler,
ILogger<RemoveFolderFromBucketActivity> logger)
: IExecuteActivity<RemoveFolderFromBucketArguments>
{
public const string ExecuteEndpointName = "remove-folder-from-bucket";
public async Task<ExecutionResult> Execute(ExecuteContext<RemoveFolderFromBucketArguments> executeContext)
{
var folderKey = executeContext.GetVariable<string>(executeContext.Arguments.FolderKeyVariable);
ArgumentException.ThrowIfNullOrEmpty(folderKey, nameof(folderKey));
try
{
await storageHandler.RemoveFolderAsync(
executeContext.Arguments.Bucket,
folderKey,
executeContext.CancellationToken);
logger.LogInformation("Removed folder {FolderKey} from bucket {Bucket}",
folderKey,
executeContext.Arguments.Bucket);
return executeContext.Completed();
}
catch (Exception exception)
{
logger.LogError(exception, "Failed to remove folder {FolderKey} from bucket {Bucket}",
folderKey,
executeContext.Arguments.Bucket);
throw;
}
}
}
}
@@ -9,28 +9,13 @@ namespace Musify.Infrastructure.MassTransit.Activities
{ {
public class UpdateTrackPictureActivity( public class UpdateTrackPictureActivity(
IDatabase database, IDatabase database,
ILogger<UpdateTrackPictureActivity> logger, ILogger<UpdateTrackPictureActivity> logger)
IProcessTrackingStore processTrackingStore)
: IActivity<UpdateTrackPictureArguments, UpdateTrackPictureLog> : IActivity<UpdateTrackPictureArguments, UpdateTrackPictureLog>
{ {
public const string ExecuteEndpointName = "update-track-picture"; public const string ExecuteEndpointName = "update-track-picture";
public async Task<ExecutionResult> Execute(ExecuteContext<UpdateTrackPictureArguments> executeContext) public async Task<ExecutionResult> Execute(ExecuteContext<UpdateTrackPictureArguments> executeContext)
{ {
var processId = await processTrackingStore.GetOrCreateProcessAsync(
"RoutingSlip",
executeContext.CorrelationId ?? executeContext.TrackingNumber,
executeContext.ConversationId,
executeContext.MessageId,
executeContext.CancellationToken);
var stepId = await processTrackingStore.StartStepAsync(
processId,
nameof(UpdateTrackPictureActivity),
ProcessStepComponentType.Activity,
0,
executeContext.CancellationToken);
var smallResizedVariable = executeContext.GetVariable<string>(executeContext.Arguments.SmallPictureVariable); var smallResizedVariable = executeContext.GetVariable<string>(executeContext.Arguments.SmallPictureVariable);
ArgumentNullException.ThrowIfNull(smallResizedVariable, nameof(smallResizedVariable)); ArgumentNullException.ThrowIfNull(smallResizedVariable, nameof(smallResizedVariable));
var mediumResizedVariable = executeContext.GetVariable<string>(executeContext.Arguments.MediumPictureVariable); var mediumResizedVariable = executeContext.GetVariable<string>(executeContext.Arguments.MediumPictureVariable);
@@ -69,14 +54,12 @@ namespace Musify.Infrastructure.MassTransit.Activities
logger.LogInformation("Updated track {TrackId} pictures", logger.LogInformation("Updated track {TrackId} pictures",
executeContext.Arguments.TrackId); executeContext.Arguments.TrackId);
await processTrackingStore.CompleteStepAsync(processId, stepId, executeContext.CancellationToken);
return executeContext.Completed(); return executeContext.Completed();
} }
catch (Exception exception) catch (Exception exception)
{ {
logger.LogError(exception, "Failed to update track {TrackId} pictures", logger.LogError(exception, "Failed to update track {TrackId} pictures",
executeContext.Arguments.TrackId); executeContext.Arguments.TrackId);
await processTrackingStore.FailStepAsync(processId, stepId, exception.Message, executeContext.CancellationToken);
throw; throw;
} }
} }
@@ -0,0 +1,115 @@
using MassTransit;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Musify.Application.Configuration;
using Musify.Application.Contracts.Infrastructure;
using Musify.Domain.Entities;
namespace Musify.Infrastructure.MassTransit.Activities.Tracks
{
internal class DeleteTrackActivity(
IDatabase database,
IStorageService storageService,
TrackConfiguration trackConfiguration,
ApplicationStorageConfiguration storageConfiguration,
ILogger<DeleteTrackActivity> logger)
: IExecuteActivity<DeleteTrackArguments>
{
public const string ExecuteEndpointName = "delete-track";
public async Task<ExecutionResult> Execute(ExecuteContext<DeleteTrackArguments> executeContext)
{
Track track;
try
{
var getTrack = await database.Tracks.SingleOrDefaultAsync(t => t.Id == executeContext.Arguments.TrackId, executeContext.CancellationToken);
track = getTrack ?? throw new InvalidOperationException($"Track with id {executeContext.Arguments.TrackId} not found");
}
catch (Exception exception)
{
logger.LogError(exception, "Failed to retrieve track {TrackId} for deletion", executeContext.Arguments.TrackId);
throw;
}
try
{
await RemovePicturesAsync(track, executeContext.CancellationToken);
track.PicturesProcessingStatus = ProcessingStatus.Completed;
await database.SaveChangesAsync(executeContext.CancellationToken);
await RemoveAudiosAsync(track, executeContext.CancellationToken);
track.AudioTranscodeProcessingStatus = ProcessingStatus.Completed;
await database.SaveChangesAsync(executeContext.CancellationToken);
database.Tracks.Remove(track);
await database.SaveChangesAsync(executeContext.CancellationToken);
logger.LogInformation("Deleted track {TrackId}", track.Id);
return executeContext.Completed();
}
catch (Exception exception)
{
logger.LogError(exception, "Failed to delete track {TrackId}", executeContext.Arguments.TrackId);
try
{
track.PicturesProcessingStatus = ProcessingStatus.Failed;
track.AudioTranscodeProcessingStatus = ProcessingStatus.Failed;
await database.SaveChangesAsync(executeContext.CancellationToken);
}
catch (Exception dbException)
{
logger.LogError(dbException, "Failed to update processing statuses to Failed for track {TrackId}", executeContext.Arguments.TrackId);
}
throw;
}
}
async Task RemovePicturesAsync(Track track, CancellationToken cancellationToken)
{
await RemoveFileAsync(trackConfiguration.Routes.BuildOriginalPicturePath(track.OriginalPictureName), cancellationToken);
if (!track.IsPicturesProcessed)
{
logger.LogInformation(
"Track {TrackId} pictures are not fully processed, skipping removal of small, medium, and large pictures",
track.Id);
return;
}
await RemoveFileAsync(trackConfiguration.Routes.BuildSmallPicturePath(track.SmallPictureName), cancellationToken);
await RemoveFileAsync(trackConfiguration.Routes.BuildMediumPicturePath(track.MediumPictureName), cancellationToken);
await RemoveFileAsync(trackConfiguration.Routes.BuildLargePicturePath(track.LargePictureName), cancellationToken);
}
async Task RemoveAudiosAsync(Track track, CancellationToken cancellationToken)
{
await RemoveFileAsync(trackConfiguration.Routes.BuildOriginalAudioPath(track.OriginalAudioName), cancellationToken);
if (!track.IsAudioProcessed)
{
logger.LogInformation(
"Track {TrackId} audio is not fully processed, skipping removal of processed audio",
track.Id);
return;
}
await RemoveFileAsync(trackConfiguration.Routes.BuildProcessedAudioPath(track.AudioFolderName), cancellationToken);
}
async Task RemoveFileAsync(string key, CancellationToken cancellationToken)
{
try
{
logger.LogDebug("Removing file {Key} from bucket {Bucket}", key, storageConfiguration.Bucket);
await storageService.RemoveFileAsync(storageConfiguration.Bucket, key, cancellationToken);
}
catch (Exception exception)
{
logger.LogError(exception, "Failed to remove file {Key} from bucket {Bucket}", key, storageConfiguration.Bucket);
throw;
}
}
}
}
@@ -0,0 +1,6 @@
namespace Musify.Infrastructure.MassTransit.Activities.Tracks
{
internal record DeleteTrackArguments(
Guid TrackId,
Guid UserId);
}
@@ -1,6 +1,7 @@
namespace Musify.Infrastructure.MassTransit.Activities.Arguments namespace Musify.Infrastructure.MassTransit.Activities.Arguments
{ {
public record GenerateAudioWorkflowPathsArguments( public record GenerateAudioWorkflowPathsArguments(
Guid TrackId,
string TemporaryRootDirectory, string TemporaryRootDirectory,
string SourceKey); string SourceKey);
} }
@@ -0,0 +1,6 @@
namespace Musify.Infrastructure.MassTransit.Arguments
{
public record RemoveFolderFromBucketArguments(
string Bucket,
string FolderKeyVariable);
}
@@ -1,6 +1,7 @@
namespace Musify.Infrastructure.MassTransit.Activities.Arguments namespace Musify.Infrastructure.MassTransit.Activities.Arguments
{ {
public record TranscodeDashAudioArguments( public record TranscodeDashAudioArguments(
Guid TrackId,
string SourceFilePathVariable, string SourceFilePathVariable,
string WorkingDirectoryVariable); string WorkingDirectoryVariable);
} }
@@ -2,5 +2,5 @@
{ {
public record UpdateTrackAudioArguments( public record UpdateTrackAudioArguments(
Guid TrackId, Guid TrackId,
string AudioFolderKey); string AudioFolderKeyVariable);
} }
@@ -0,0 +1,23 @@
using MassTransit;
using Musify.Application.Events;
using Musify.Infrastructure.MassTransit.RoutingSlip.Builders;
namespace Musify.Infrastructure.MassTransit.Consumers
{
public class DeleteTrackConsumer(
IBus bus,
DeleteTrackRoutingSlipBuilder routingSlipBuilder)
: IConsumer<DeleteTrackEvent>
{
public const string QueueName = "delete-track";
public async Task Consume(ConsumeContext<DeleteTrackEvent> consumeContext)
{
var routingSlip = routingSlipBuilder
.Build(consumeContext.Message.TrackId, consumeContext.Message.UserId, consumeContext.CorrelationId)
.Build();
await bus.Execute(routingSlip);
}
}
}
@@ -1,23 +0,0 @@
using MassTransit;
using Musify.Application.Events;
using Musify.Infrastructure.MassTransit.RoutingSlip.Builders;
namespace Musify.Infrastructure.MassTransit.Consumers
{
public class RemoveFileConsumer(
IBus bus,
RemoveFileFromBucketRoutingSlipBuilder routingSlipBuilder)
: IConsumer<RemoveFileEvent>
{
public const string QueueName = "remove-file";
public async Task Consume(ConsumeContext<RemoveFileEvent> consumeContext)
{
var routingSlip = routingSlipBuilder
.Build(consumeContext.Message, consumeContext.CorrelationId)
.Build();
await bus.Execute(routingSlip);
}
}
}
@@ -14,6 +14,7 @@ using Musify.Infrastructure.MassTransit.Consumers;
using Musify.Infrastructure.MassTransit.Filters; using Musify.Infrastructure.MassTransit.Filters;
using Musify.Infrastructure.MassTransit.Logs; using Musify.Infrastructure.MassTransit.Logs;
using Musify.Infrastructure.MassTransit.RoutingSlip.Builders; using Musify.Infrastructure.MassTransit.RoutingSlip.Builders;
using Musify.Infrastructure.MassTransit.Activities.Tracks;
namespace Musify.Infrastructure.MassTransit namespace Musify.Infrastructure.MassTransit
{ {
@@ -61,19 +62,20 @@ namespace Musify.Infrastructure.MassTransit
serviceDescriptors.AddScoped<AudioWorkflowRoutingSlipBuilder>(); serviceDescriptors.AddScoped<AudioWorkflowRoutingSlipBuilder>();
serviceDescriptors.AddScoped<PictureWorkflowRoutingSlipBuilder>(); serviceDescriptors.AddScoped<PictureWorkflowRoutingSlipBuilder>();
serviceDescriptors.AddScoped<RemoveFileFromBucketRoutingSlipBuilder>(); serviceDescriptors.AddScoped<DeleteTrackRoutingSlipBuilder>();
serviceDescriptors.AddMassTransit(options => serviceDescriptors.AddMassTransit(options =>
{ {
options.AddConsumer<UpdatePlayListPictureConsumer>(); options.AddConsumer<UpdatePlayListPictureConsumer>();
options.AddConsumer<RemoveFileConsumer>();
options.AddConsumer<UpdateTrackPictureConsumer>(); options.AddConsumer<UpdateTrackPictureConsumer>();
options.AddConsumer<RoutingSlipCleanUpConsumer>(); options.AddConsumer<RoutingSlipCleanUpConsumer>();
options.AddConsumer<UpdateTrackAudioConsumer>(); options.AddConsumer<UpdateTrackAudioConsumer>();
options.AddConsumer<DeleteTrackConsumer>();
options.AddExecuteActivity<RemoveFileFromBucketActivity, RemoveFileFromBucketArguments>(); options.AddExecuteActivity<RemoveFileFromBucketActivity, RemoveFileFromBucketArguments>();
options.AddExecuteActivity<GenerateAudioWorkflowPathsActivity, GenerateAudioWorkflowPathsArguments>(); options.AddExecuteActivity<GenerateAudioWorkflowPathsActivity, GenerateAudioWorkflowPathsArguments>();
options.AddExecuteActivity<GeneratePictureWorkflowPathsActivity, GeneratePictureWorkflowPathsArguments>(); options.AddExecuteActivity<GeneratePictureWorkflowPathsActivity, GeneratePictureWorkflowPathsArguments>();
options.AddExecuteActivity<DeleteTrackActivity, DeleteTrackArguments>();
options.AddActivity<ResizePictureActivity, ResizePictureLocalArguments, ResizePictureLog>(); options.AddActivity<ResizePictureActivity, ResizePictureLocalArguments, ResizePictureLog>();
options.AddActivity<UpdatePlayListPictureActivity, UpdatePlayListPictureArguments, UpdatePlayListPictureLog>(); options.AddActivity<UpdatePlayListPictureActivity, UpdatePlayListPictureArguments, UpdatePlayListPictureLog>();
@@ -95,15 +97,15 @@ namespace Musify.Infrastructure.MassTransit
busFactoryConfigurator.ReceiveEndpoint(UpdatePlayListPictureConsumer.QueueName, endpointConfigurator => busFactoryConfigurator.ReceiveEndpoint(UpdatePlayListPictureConsumer.QueueName, endpointConfigurator =>
endpointConfigurator.ConfigureConsumer<UpdatePlayListPictureConsumer>(busRegistrationContext)); endpointConfigurator.ConfigureConsumer<UpdatePlayListPictureConsumer>(busRegistrationContext));
busFactoryConfigurator.ReceiveEndpoint(RemoveFileConsumer.QueueName, endpointConfigurator =>
endpointConfigurator.ConfigureConsumer<RemoveFileConsumer>(busRegistrationContext));
busFactoryConfigurator.ReceiveEndpoint(UpdateTrackAudioConsumer.QueueName, endpointConfigurator => busFactoryConfigurator.ReceiveEndpoint(UpdateTrackAudioConsumer.QueueName, endpointConfigurator =>
endpointConfigurator.ConfigureConsumer<UpdateTrackAudioConsumer>(busRegistrationContext)); endpointConfigurator.ConfigureConsumer<UpdateTrackAudioConsumer>(busRegistrationContext));
busFactoryConfigurator.ReceiveEndpoint(UpdateTrackPictureConsumer.QueueName, endpointConfigurator => busFactoryConfigurator.ReceiveEndpoint(UpdateTrackPictureConsumer.QueueName, endpointConfigurator =>
endpointConfigurator.ConfigureConsumer<UpdateTrackPictureConsumer>(busRegistrationContext)); endpointConfigurator.ConfigureConsumer<UpdateTrackPictureConsumer>(busRegistrationContext));
busFactoryConfigurator.ReceiveEndpoint(DeleteTrackConsumer.QueueName, endpointConfigurator =>
endpointConfigurator.ConfigureConsumer<DeleteTrackConsumer>(busRegistrationContext));
busFactoryConfigurator.ReceiveEndpoint(RoutingSlipCleanUpConsumer.QueueName, endpointConfigurator => busFactoryConfigurator.ReceiveEndpoint(RoutingSlipCleanUpConsumer.QueueName, endpointConfigurator =>
endpointConfigurator.ConfigureConsumer<RoutingSlipCleanUpConsumer>(busRegistrationContext)); endpointConfigurator.ConfigureConsumer<RoutingSlipCleanUpConsumer>(busRegistrationContext));
@@ -2,5 +2,5 @@
{ {
public record UpdateTrackAudioLog( public record UpdateTrackAudioLog(
Guid TrackId, Guid TrackId,
string PreviousFolderAudioKey); string? PreviousAudioFolderName);
} }
@@ -2,6 +2,7 @@ namespace Musify.Infrastructure.MassTransit.RoutingSlip
{ {
internal static class ActivityNames internal static class ActivityNames
{ {
internal const string DeleteTrack = "DeleteTrack";
internal const string GenerateAudioWorkflowPaths = "GenerateAudioWorkflowPaths"; internal const string GenerateAudioWorkflowPaths = "GenerateAudioWorkflowPaths";
internal const string GeneratePictureWorkflowPaths = "GeneratePictureWorkflowPaths"; internal const string GeneratePictureWorkflowPaths = "GeneratePictureWorkflowPaths";
internal const string DownloadFile = "DownloadFile"; internal const string DownloadFile = "DownloadFile";
@@ -3,6 +3,7 @@ using MassTransit.Courier.Contracts;
using Musify.Application.Events; using Musify.Application.Events;
using Musify.Infrastructure.Configuration; using Musify.Infrastructure.Configuration;
using Musify.Infrastructure.MassTransit.Activities.Audio; using Musify.Infrastructure.MassTransit.Activities.Audio;
using Musify.Infrastructure.MassTransit.Activities.Files;
using Musify.Infrastructure.MassTransit.Arguments; using Musify.Infrastructure.MassTransit.Arguments;
using Musify.Infrastructure.MassTransit.Consumers; using Musify.Infrastructure.MassTransit.Consumers;
using Musify.Infrastructure.MassTransit.Activities; using Musify.Infrastructure.MassTransit.Activities;
@@ -25,6 +26,7 @@ namespace Musify.Infrastructure.MassTransit.RoutingSlip.Builders
ActivityNames.GenerateAudioWorkflowPaths, ActivityNames.GenerateAudioWorkflowPaths,
EndpointHelper.BuildExecuteActivityUri(GenerateAudioWorkflowPathsActivity.ExecuteEndpointName), EndpointHelper.BuildExecuteActivityUri(GenerateAudioWorkflowPathsActivity.ExecuteEndpointName),
new GenerateAudioWorkflowPathsArguments( new GenerateAudioWorkflowPathsArguments(
message.TrackId,
workerConfiguration.Routes.TemporaryFilesDirectory, workerConfiguration.Routes.TemporaryFilesDirectory,
message.SourceKey)); message.SourceKey));
@@ -40,6 +42,7 @@ namespace Musify.Infrastructure.MassTransit.RoutingSlip.Builders
ActivityNames.TranscodeAudio, ActivityNames.TranscodeAudio,
EndpointHelper.BuildExecuteActivityUri(TranscodeDashAudioActivity.ExecuteEndpointName), EndpointHelper.BuildExecuteActivityUri(TranscodeDashAudioActivity.ExecuteEndpointName),
new TranscodeDashAudioArguments( new TranscodeDashAudioArguments(
message.TrackId,
RoutingSlipVariableNames.Audio.SourceFilePath, RoutingSlipVariableNames.Audio.SourceFilePath,
RoutingSlipVariableNames.Workflow.TemporalDirectory)); RoutingSlipVariableNames.Workflow.TemporalDirectory));
@@ -56,7 +59,7 @@ namespace Musify.Infrastructure.MassTransit.RoutingSlip.Builders
EndpointHelper.BuildExecuteActivityUri(UpdateTrackAudioActivity.ExecuteEndpointName), EndpointHelper.BuildExecuteActivityUri(UpdateTrackAudioActivity.ExecuteEndpointName),
new UpdateTrackAudioArguments( new UpdateTrackAudioArguments(
message.TrackId, message.TrackId,
message.DestinationFolderKey)); RoutingSlipVariableNames.Audio.TranscodedDirectory));
return routingSlipBuilder; return routingSlipBuilder;
} }
@@ -1,15 +1,13 @@
using MassTransit; using MassTransit;
using MassTransit.Courier.Contracts; using MassTransit.Courier.Contracts;
using Musify.Application.Events; using Musify.Infrastructure.MassTransit.Activities.Tracks;
using Musify.Infrastructure.MassTransit.Consumers; using Musify.Infrastructure.MassTransit.Consumers;
using Musify.Infrastructure.MassTransit.Activities;
using Musify.Infrastructure.MassTransit.Activities.Arguments;
namespace Musify.Infrastructure.MassTransit.RoutingSlip.Builders namespace Musify.Infrastructure.MassTransit.RoutingSlip.Builders
{ {
public class RemoveFileFromBucketRoutingSlipBuilder public class DeleteTrackRoutingSlipBuilder
{ {
public RoutingSlipBuilder Build(RemoveFileEvent message, Guid? correlationId) public RoutingSlipBuilder Build(Guid trackId, Guid userId, Guid? correlationId)
{ {
var routingSlipBuilder = new RoutingSlipBuilder(NewId.NextGuid()); var routingSlipBuilder = new RoutingSlipBuilder(NewId.NextGuid());
routingSlipBuilder.AddVariable(RoutingSlipVariableNames.Workflow.CorrelationId, correlationId ?? Guid.Empty); routingSlipBuilder.AddVariable(RoutingSlipVariableNames.Workflow.CorrelationId, correlationId ?? Guid.Empty);
@@ -19,11 +17,9 @@ namespace Musify.Infrastructure.MassTransit.RoutingSlip.Builders
RoutingSlipEvents.Completed | RoutingSlipEvents.Faulted); RoutingSlipEvents.Completed | RoutingSlipEvents.Faulted);
routingSlipBuilder.AddActivity( routingSlipBuilder.AddActivity(
ActivityNames.RemoveFile, ActivityNames.DeleteTrack,
EndpointHelper.BuildExecuteActivityUri(RemoveFileFromBucketActivity.ExecuteEndpointName), EndpointHelper.BuildExecuteActivityUri(DeleteTrackActivity.ExecuteEndpointName),
new RemoveFileFromBucketArguments( new DeleteTrackArguments(trackId, userId));
message.Bucket,
message.Key));
return routingSlipBuilder; return routingSlipBuilder;
} }
@@ -13,6 +13,7 @@ namespace Musify.Infrastructure.MassTransit
internal const string SourceFilePath = "Audio.SourceFilePath"; internal const string SourceFilePath = "Audio.SourceFilePath";
internal const string WorkingDirectory = "Audio.WorkingDirectory"; internal const string WorkingDirectory = "Audio.WorkingDirectory";
internal const string TranscodedDirectory = "Audio.TranscodedDirectory"; internal const string TranscodedDirectory = "Audio.TranscodedDirectory";
internal const string PreviousAudioFolderName = "Audio.PreviousAudioFolderName";
} }
internal static class Picture internal static class Picture
+38
View File
@@ -180,5 +180,43 @@ namespace Musify.Infrastructure.Services
throw; throw;
} }
} }
public async Task RemoveFolderAsync(string bucket, string folderKey, CancellationToken cancellationToken)
{
var prefix = folderKey.EndsWith('/') ? folderKey : $"{folderKey}/";
var listRequest = new ListObjectsV2Request
{
BucketName = bucket,
Prefix = prefix
};
ListObjectsV2Response listResponse;
do
{
listResponse = await amazonS3.ListObjectsV2Async(listRequest, cancellationToken);
if (listResponse.S3Objects.Count == 0)
break;
var deleteRequest = new DeleteObjectsRequest
{
BucketName = bucket,
Objects = listResponse.S3Objects
.Select(o => new KeyVersion { Key = o.Key } )
.ToList()
};
var deleteResponse = await amazonS3.DeleteObjectsAsync(deleteRequest, cancellationToken);
if (deleteResponse.DeleteErrors.Count > 0)
{
var errors = string.Join(", ", deleteResponse.DeleteErrors.Select(e => $"{e.Key}: {e.Message}"));
throw new InvalidOperationException($"Failed to delete some objects in folder {folderKey}: {errors}");
}
listRequest.ContinuationToken = listResponse.NextContinuationToken;
} while (listResponse.IsTruncated.HasValue && listResponse.IsTruncated.Value);
}
} }
} }