From 0b471db6aaddad87f76aff07c947208a106eb62c Mon Sep 17 00:00:00 2001 From: NotS1mp Date: Mon, 23 Mar 2026 16:28:02 +0100 Subject: [PATCH] Use variables --- .../Arguments/TranscodeDashAudioArguments.cs | 2 +- .../TransferFilesToBucketArguments.cs | 1 - .../DownloadFileFromBucketActivity.cs | 16 ++++++++++----- .../Activities/TranscodeDashAudioActivity.cs | 20 +++++++++++-------- .../Activities/TransferFilesToBucket.cs | 12 ++++++++--- .../TranscodeAudioFromTrackConsumer.cs | 10 ++-------- .../UpdatePlayListPictureConsumer.cs | 1 - .../Consumers/UpdateTrackPictureConsumer.cs | 1 - 8 files changed, 35 insertions(+), 28 deletions(-) diff --git a/Infrastructure/Messaging/Activities/Arguments/TranscodeDashAudioArguments.cs b/Infrastructure/Messaging/Activities/Arguments/TranscodeDashAudioArguments.cs index ddbd8c4..a983378 100644 --- a/Infrastructure/Messaging/Activities/Arguments/TranscodeDashAudioArguments.cs +++ b/Infrastructure/Messaging/Activities/Arguments/TranscodeDashAudioArguments.cs @@ -1,4 +1,4 @@ namespace Musify.Infrastructure.Messaging.Activities.Arguments { - public record TranscodeDashAudioArguments(string SourceFilePath, string DestinationFolderName); + public record TranscodeDashAudioArguments(); } \ No newline at end of file diff --git a/Infrastructure/Messaging/Activities/Arguments/TransferFilesToBucketArguments.cs b/Infrastructure/Messaging/Activities/Arguments/TransferFilesToBucketArguments.cs index 8f39ecd..b08f97b 100644 --- a/Infrastructure/Messaging/Activities/Arguments/TransferFilesToBucketArguments.cs +++ b/Infrastructure/Messaging/Activities/Arguments/TransferFilesToBucketArguments.cs @@ -1,7 +1,6 @@ namespace Musify.Infrastructure.Messaging.Activities.Arguments { public record TransferFilesToBucketArguments( - string FolderPath, string DestinationBucketName, string DestinationKeyName); } \ No newline at end of file diff --git a/Infrastructure/Messaging/Activities/DownloadFileFromBucketActivity.cs b/Infrastructure/Messaging/Activities/DownloadFileFromBucketActivity.cs index b0f33b3..139cd9a 100644 --- a/Infrastructure/Messaging/Activities/DownloadFileFromBucketActivity.cs +++ b/Infrastructure/Messaging/Activities/DownloadFileFromBucketActivity.cs @@ -53,17 +53,23 @@ namespace Musify.Infrastructure.Messaging.Activities if (fileStream.CanSeek) fileStream.Position = 0; - var destinationPath = Path.Combine( - audioTranscoderConfiguration.Routes.WorkingDirectory, - Path.GetFileName(executeContext.Arguments.KeyName)); + var fileName = Path.GetFileName(executeContext.Arguments.KeyName); + var folderName = Guid.NewGuid().ToString(); + var workingDirectory = Path.Combine(executeContext.Arguments.DestinationPath, folderName); + var destinationPath = Path.Combine(workingDirectory, fileName); - Directory.CreateDirectory(Path.GetDirectoryName(destinationPath)!); + Directory.CreateDirectory(workingDirectory); using var destinationStream = File.Create(destinationPath); await fileStream.CopyToAsync(destinationStream, executeContext.CancellationToken); await processTrackingStore.CompleteStepAsync(processId, stepId, executeContext.CancellationToken); - return executeContext.Completed(); + return executeContext.CompletedWithVariables(new + { + SourceFilePath = destinationPath, + WorkingDirectory = workingDirectory, + DestinationFolderName = folderName + } ); } catch (Exception exception) { diff --git a/Infrastructure/Messaging/Activities/TranscodeDashAudioActivity.cs b/Infrastructure/Messaging/Activities/TranscodeDashAudioActivity.cs index ae8ba5e..3cd59b4 100644 --- a/Infrastructure/Messaging/Activities/TranscodeDashAudioActivity.cs +++ b/Infrastructure/Messaging/Activities/TranscodeDashAudioActivity.cs @@ -32,17 +32,21 @@ namespace Musify.Infrastructure.Messaging.Activities 0, executeContext.CancellationToken); + var sourceFilePath = executeContext.GetVariable("SourceFilePath"); + var destinationFolderName = executeContext.GetVariable("DestinationFolderName"); + + if (string.IsNullOrWhiteSpace(sourceFilePath) || string.IsNullOrWhiteSpace(destinationFolderName)) + { + throw new InvalidOperationException("Transcode activity requires SourceFilePath and DestinationFolderName variables."); + } + try { - var workingDirectory = Path.Combine( - audioTranscoderConfiguration.Routes.WorkingDirectory, - executeContext.Arguments.DestinationFolderName); - - await using var fileStream = File.OpenRead(executeContext.Arguments.SourceFilePath); + await using var fileStream = File.OpenRead(sourceFilePath); var result = await audioTranscoder.TranscodeToDashAsync( fileStream, - executeContext.Arguments.DestinationFolderName, + destinationFolderName, audioTranscoderConfiguration.TranscodingTimeout, executeContext.CancellationToken); @@ -50,7 +54,7 @@ namespace Musify.Infrastructure.Messaging.Activities { var errorMessage = string.Join("; ", result.Errors); logger.LogError("Transcoding failed for file {SourceFilePath}. Errors: {ErrorMessage}", - executeContext.Arguments.SourceFilePath, errorMessage); + sourceFilePath, errorMessage); throw new Exception(errorMessage); } @@ -60,7 +64,7 @@ namespace Musify.Infrastructure.Messaging.Activities catch (Exception exception) { logger.LogError(exception, "Error during transcoding activity for file {SourceFilePath}", - executeContext.Arguments.SourceFilePath); + sourceFilePath); await processTrackingStore.FailStepAsync(processId, stepId, exception.Message, executeContext.CancellationToken); throw; } diff --git a/Infrastructure/Messaging/Activities/TransferFilesToBucket.cs b/Infrastructure/Messaging/Activities/TransferFilesToBucket.cs index b3f1ea9..4907734 100644 --- a/Infrastructure/Messaging/Activities/TransferFilesToBucket.cs +++ b/Infrastructure/Messaging/Activities/TransferFilesToBucket.cs @@ -30,10 +30,16 @@ namespace Musify.Infrastructure.Messaging.Activities 0, executeContext.CancellationToken); + var folderPath = executeContext.GetVariable("WorkingDirectory"); + if (string.IsNullOrWhiteSpace(folderPath)) + { + throw new InvalidOperationException("Transfer activity requires WorkingDirectory variable."); + } + try { var transferFilesResult = await storageHandler.TransferFilesAsync( - executeContext.Arguments.FolderPath, + folderPath, executeContext.Arguments.DestinationBucketName, executeContext.Arguments.DestinationKeyName, executeContext.CancellationToken); @@ -42,7 +48,7 @@ namespace Musify.Infrastructure.Messaging.Activities { var errorMessage = string.Join("; ", transferFilesResult.Errors); logger.LogError("Failed to transfer files from {FolderPath} to bucket {DestinationBucketName}, key {DestinationKeyName}. Errors: {Errors}", - executeContext.Arguments.FolderPath, + folderPath, executeContext.Arguments.DestinationBucketName, executeContext.Arguments.DestinationKeyName, errorMessage); @@ -55,7 +61,7 @@ namespace Musify.Infrastructure.Messaging.Activities catch (Exception exception) { logger.LogError(exception, "Error transferring files from {FolderPath} to bucket {DestinationBucketName}", - executeContext.Arguments.FolderPath, + folderPath, executeContext.Arguments.DestinationBucketName); await processTrackingStore.FailStepAsync(processId, stepId, exception.Message, executeContext.CancellationToken); throw; diff --git a/Infrastructure/Messaging/Consumers/TranscodeAudioFromTrackConsumer.cs b/Infrastructure/Messaging/Consumers/TranscodeAudioFromTrackConsumer.cs index 41bfc49..e373565 100644 --- a/Infrastructure/Messaging/Consumers/TranscodeAudioFromTrackConsumer.cs +++ b/Infrastructure/Messaging/Consumers/TranscodeAudioFromTrackConsumer.cs @@ -16,9 +16,6 @@ namespace Musify.Infrastructure.Messaging.Consumers public async Task Consume(ConsumeContext consumeContext) { var routingSlipBuilder = new RoutingSlipBuilder(NewId.NextGuid()); - var fileName = Path.GetFileName(consumeContext.Message.SourceKeyName); - var folderName = Guid.NewGuid().ToString(); - var workingDirectory = Path.Combine(audioTranscoderConfiguration.Routes.WorkingDirectory, folderName); routingSlipBuilder.AddActivity( "DownloadFile", @@ -26,20 +23,17 @@ namespace Musify.Infrastructure.Messaging.Consumers new DownloadFileFromBucketArguments( consumeContext.Message.SourceBucketName, consumeContext.Message.SourceKeyName, - workingDirectory)); + audioTranscoderConfiguration.Routes.WorkingDirectory)); routingSlipBuilder.AddActivity( "TranscodeAudio", MessagingHelper.BuildExecuteActivityUri(TranscodeDashAudioActivity.ExecuteEndpointName), - new TranscodeDashAudioArguments( - Path.Combine(workingDirectory, fileName), - folderName)); + new TranscodeDashAudioArguments()); routingSlipBuilder.AddActivity( "UploadFile", MessagingHelper.BuildExecuteActivityUri(TransferFilesToBucket.ExecuteEndpointName), new TransferFilesToBucketArguments( - workingDirectory, consumeContext.Message.DestinationBucketName, consumeContext.Message.DestinationKeyName)); diff --git a/Infrastructure/Messaging/Consumers/UpdatePlayListPictureConsumer.cs b/Infrastructure/Messaging/Consumers/UpdatePlayListPictureConsumer.cs index fc72103..6af894e 100644 --- a/Infrastructure/Messaging/Consumers/UpdatePlayListPictureConsumer.cs +++ b/Infrastructure/Messaging/Consumers/UpdatePlayListPictureConsumer.cs @@ -68,7 +68,6 @@ namespace Musify.Infrastructure.Messaging.Consumers "UploadFiles", MessagingHelper.BuildExecuteActivityUri(TransferFilesToBucket.ExecuteEndpointName), new TransferFilesToBucketArguments( - workingDirectory, consumeContext.Message.SourceBucketName, folderName)); diff --git a/Infrastructure/Messaging/Consumers/UpdateTrackPictureConsumer.cs b/Infrastructure/Messaging/Consumers/UpdateTrackPictureConsumer.cs index 45f5942..22d050c 100644 --- a/Infrastructure/Messaging/Consumers/UpdateTrackPictureConsumer.cs +++ b/Infrastructure/Messaging/Consumers/UpdateTrackPictureConsumer.cs @@ -68,7 +68,6 @@ namespace Musify.Infrastructure.Messaging.Consumers "UploadFiles", MessagingHelper.BuildExecuteActivityUri(TransferFilesToBucket.ExecuteEndpointName), new TransferFilesToBucketArguments( - workingDirectory, consumeContext.Message.SourceBucketName, folderName));