Add editorconfig and analyzers, format backend code
This commit is contained in:
+45
-46
@@ -4,58 +4,57 @@ using Musify.Application.Contracts;
|
||||
using Musify.Infrastructure.MassTransit.Arguments;
|
||||
using Musify.Infrastructure.MassTransit.Logs;
|
||||
|
||||
namespace Musify.Infrastructure.MassTransit.Activities.Files
|
||||
namespace Musify.Infrastructure.MassTransit.Activities.Files;
|
||||
|
||||
internal class CopyFileInBucketActivity(
|
||||
IStorageService storageHandler,
|
||||
ILogger<CopyFileInBucketActivity> logger)
|
||||
: IActivity<CopyFileInBucketArguments, CopyFileInBucketLog>
|
||||
{
|
||||
internal class CopyFileInBucketActivity(
|
||||
IStorageService storageHandler,
|
||||
ILogger<CopyFileInBucketActivity> logger)
|
||||
: IActivity<CopyFileInBucketArguments, CopyFileInBucketLog>
|
||||
public const string ExecuteEndpointName = "copy-file-in-bucket";
|
||||
|
||||
public async Task<ExecutionResult> Execute(ExecuteContext<CopyFileInBucketArguments> executeContext)
|
||||
{
|
||||
public const string ExecuteEndpointName = "copy-file-in-bucket";
|
||||
|
||||
public async Task<ExecutionResult> Execute(ExecuteContext<CopyFileInBucketArguments> executeContext)
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
await storageHandler.CopyFileAsync(
|
||||
executeContext.Arguments.SourceBucket,
|
||||
executeContext.Arguments.SourceKey,
|
||||
executeContext.Arguments.DestinationBucket,
|
||||
executeContext.Arguments.DestinationKey,
|
||||
executeContext.CancellationToken);
|
||||
await storageHandler.CopyFileAsync(
|
||||
executeContext.Arguments.SourceBucket,
|
||||
executeContext.Arguments.SourceKey,
|
||||
executeContext.Arguments.DestinationBucket,
|
||||
executeContext.Arguments.DestinationKey,
|
||||
executeContext.CancellationToken);
|
||||
|
||||
return executeContext.Completed(new CopyFileInBucketLog(
|
||||
executeContext.Arguments.DestinationBucket,
|
||||
executeContext.Arguments.DestinationKey));
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to copy {SourceBucket}/{SourceKey} to {DestinationBucket}/{DestinationKey}",
|
||||
executeContext.Arguments.SourceBucket,
|
||||
executeContext.Arguments.SourceKey,
|
||||
executeContext.Arguments.DestinationBucket,
|
||||
executeContext.Arguments.DestinationKey);
|
||||
throw;
|
||||
}
|
||||
return executeContext.Completed(new CopyFileInBucketLog(
|
||||
executeContext.Arguments.DestinationBucket,
|
||||
executeContext.Arguments.DestinationKey));
|
||||
}
|
||||
|
||||
public async Task<CompensationResult> Compensate(CompensateContext<CopyFileInBucketLog> compensateContext)
|
||||
catch (Exception exception)
|
||||
{
|
||||
try
|
||||
{
|
||||
await storageHandler.RemoveFileAsync(
|
||||
compensateContext.Log.DestinationBucket,
|
||||
compensateContext.Log.DestinationKey,
|
||||
compensateContext.CancellationToken);
|
||||
return compensateContext.Compensated();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to compensate copied file {Bucket}/{Key}",
|
||||
compensateContext.Log.DestinationBucket,
|
||||
compensateContext.Log.DestinationKey);
|
||||
return compensateContext.Failed(exception);
|
||||
}
|
||||
logger.LogError(exception, "Failed to copy {SourceBucket}/{SourceKey} to {DestinationBucket}/{DestinationKey}",
|
||||
executeContext.Arguments.SourceBucket,
|
||||
executeContext.Arguments.SourceKey,
|
||||
executeContext.Arguments.DestinationBucket,
|
||||
executeContext.Arguments.DestinationKey);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<CompensationResult> Compensate(CompensateContext<CopyFileInBucketLog> compensateContext)
|
||||
{
|
||||
try
|
||||
{
|
||||
await storageHandler.RemoveFileAsync(
|
||||
compensateContext.Log.DestinationBucket,
|
||||
compensateContext.Log.DestinationKey,
|
||||
compensateContext.CancellationToken);
|
||||
return compensateContext.Compensated();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to compensate copied file {Bucket}/{Key}",
|
||||
compensateContext.Log.DestinationBucket,
|
||||
compensateContext.Log.DestinationKey);
|
||||
return compensateContext.Failed(exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+45
-46
@@ -4,60 +4,59 @@ using Musify.Application.Contracts;
|
||||
using Musify.Infrastructure.MassTransit.Arguments;
|
||||
using Musify.Infrastructure.MassTransit.Logs;
|
||||
|
||||
namespace Musify.Infrastructure.MassTransit.Activities.Files
|
||||
namespace Musify.Infrastructure.MassTransit.Activities.Files;
|
||||
|
||||
internal class DownloadFileFromBucketActivity(
|
||||
IStorageService storageHandler,
|
||||
ILogger<DownloadFileFromBucketActivity> logger)
|
||||
: IActivity<DownloadFileFromBucketArguments, DownloadFileFromBucketLog>
|
||||
{
|
||||
internal class DownloadFileFromBucketActivity(
|
||||
IStorageService storageHandler,
|
||||
ILogger<DownloadFileFromBucketActivity> logger)
|
||||
: IActivity<DownloadFileFromBucketArguments, DownloadFileFromBucketLog>
|
||||
public const string ExecuteEndpointName = "download-file-from-bucket";
|
||||
|
||||
public async Task<ExecutionResult> Execute(ExecuteContext<DownloadFileFromBucketArguments> executeContext)
|
||||
{
|
||||
public const string ExecuteEndpointName = "download-file-from-bucket";
|
||||
var destinationPath = executeContext.GetVariable<string>(executeContext.Arguments.DestinationFilePathVariable);
|
||||
ArgumentNullException.ThrowIfNull(destinationPath, nameof(destinationPath));
|
||||
|
||||
public async Task<ExecutionResult> Execute(ExecuteContext<DownloadFileFromBucketArguments> executeContext)
|
||||
try
|
||||
{
|
||||
var destinationPath = executeContext.GetVariable<string>(executeContext.Arguments.DestinationFilePathVariable);
|
||||
ArgumentNullException.ThrowIfNull(destinationPath, nameof(destinationPath));
|
||||
using var fileStream = await storageHandler.GetFileAsync(
|
||||
executeContext.Arguments.Bucket,
|
||||
executeContext.Arguments.Key,
|
||||
executeContext.CancellationToken);
|
||||
|
||||
try
|
||||
{
|
||||
using var fileStream = await storageHandler.GetFileAsync(
|
||||
executeContext.Arguments.Bucket,
|
||||
executeContext.Arguments.Key,
|
||||
executeContext.CancellationToken);
|
||||
var destinationDirectory = Path.GetDirectoryName(destinationPath);
|
||||
if (!string.IsNullOrWhiteSpace(destinationDirectory))
|
||||
Directory.CreateDirectory(destinationDirectory);
|
||||
|
||||
var destinationDirectory = Path.GetDirectoryName(destinationPath);
|
||||
if (!string.IsNullOrWhiteSpace(destinationDirectory))
|
||||
Directory.CreateDirectory(destinationDirectory);
|
||||
if (fileStream.CanSeek)
|
||||
fileStream.Position = 0;
|
||||
|
||||
if (fileStream.CanSeek)
|
||||
fileStream.Position = 0;
|
||||
|
||||
using var destinationStream = File.Create(destinationPath);
|
||||
await fileStream.CopyToAsync(destinationStream, executeContext.CancellationToken);
|
||||
return executeContext.Completed(new DownloadFileFromBucketLog(
|
||||
executeContext.Arguments.Bucket,
|
||||
executeContext.Arguments.Key,
|
||||
destinationPath));
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to download file from bucket");
|
||||
throw;
|
||||
}
|
||||
using var destinationStream = File.Create(destinationPath);
|
||||
await fileStream.CopyToAsync(destinationStream, executeContext.CancellationToken);
|
||||
return executeContext.Completed(new DownloadFileFromBucketLog(
|
||||
executeContext.Arguments.Bucket,
|
||||
executeContext.Arguments.Key,
|
||||
destinationPath));
|
||||
}
|
||||
|
||||
public Task<CompensationResult> Compensate(CompensateContext<DownloadFileFromBucketLog> compensateContext)
|
||||
catch (Exception exception)
|
||||
{
|
||||
try
|
||||
{
|
||||
File.Delete(compensateContext.Log.DestinationFilePath);
|
||||
return Task.FromResult(compensateContext.Compensated());
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to compensate downloaded file {DestinationFilePath}", compensateContext.Log.DestinationFilePath);
|
||||
return Task.FromResult(compensateContext.Failed(exception));
|
||||
}
|
||||
logger.LogError(exception, "Failed to download file from bucket");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Task<CompensationResult> Compensate(CompensateContext<DownloadFileFromBucketLog> compensateContext)
|
||||
{
|
||||
try
|
||||
{
|
||||
File.Delete(compensateContext.Log.DestinationFilePath);
|
||||
return Task.FromResult(compensateContext.Compensated());
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to compensate downloaded file {DestinationFilePath}", compensateContext.Log.DestinationFilePath);
|
||||
return Task.FromResult(compensateContext.Failed(exception));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+51
-52
@@ -3,67 +3,66 @@ using Microsoft.Extensions.Logging;
|
||||
using Musify.Infrastructure.MassTransit.Arguments;
|
||||
using Musify.Infrastructure.MassTransit.Logs;
|
||||
|
||||
namespace Musify.Infrastructure.MassTransit.Activities.Files
|
||||
namespace Musify.Infrastructure.MassTransit.Activities.Files;
|
||||
|
||||
internal class DownloadFileFromUrlActivity(
|
||||
IHttpClientFactory httpClientFactory,
|
||||
ILogger<DownloadFileFromUrlActivity> logger)
|
||||
: IActivity<DownloadFileFromUrlArguments, DownloadFileFromUrlLog>
|
||||
{
|
||||
internal class DownloadFileFromUrlActivity(
|
||||
IHttpClientFactory httpClientFactory,
|
||||
ILogger<DownloadFileFromUrlActivity> logger)
|
||||
: IActivity<DownloadFileFromUrlArguments, DownloadFileFromUrlLog>
|
||||
public const string ExecuteEndpointName = "download-file-from-url";
|
||||
|
||||
public async Task<ExecutionResult> Execute(ExecuteContext<DownloadFileFromUrlArguments> executeContext)
|
||||
{
|
||||
public const string ExecuteEndpointName = "download-file-from-url";
|
||||
var destinationPath = executeContext.GetVariable<string>(executeContext.Arguments.DestinationFilePathVariable);
|
||||
ArgumentNullException.ThrowIfNull(destinationPath, nameof(destinationPath));
|
||||
|
||||
public async Task<ExecutionResult> Execute(ExecuteContext<DownloadFileFromUrlArguments> executeContext)
|
||||
try
|
||||
{
|
||||
var destinationPath = executeContext.GetVariable<string>(executeContext.Arguments.DestinationFilePathVariable);
|
||||
ArgumentNullException.ThrowIfNull(destinationPath, nameof(destinationPath));
|
||||
var httpClient = httpClientFactory.CreateClient();
|
||||
|
||||
try
|
||||
using var response = await httpClient.GetAsync(
|
||||
executeContext.Arguments.SourceUrl,
|
||||
HttpCompletionOption.ResponseHeadersRead,
|
||||
executeContext.CancellationToken);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var destinationDirectory = Path.GetDirectoryName(destinationPath);
|
||||
if (!string.IsNullOrWhiteSpace(destinationDirectory))
|
||||
Directory.CreateDirectory(destinationDirectory);
|
||||
|
||||
await using (var destinationStream = File.Create(destinationPath))
|
||||
{
|
||||
var httpClient = httpClientFactory.CreateClient();
|
||||
|
||||
using var response = await httpClient.GetAsync(
|
||||
executeContext.Arguments.SourceUrl,
|
||||
HttpCompletionOption.ResponseHeadersRead,
|
||||
executeContext.CancellationToken);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var destinationDirectory = Path.GetDirectoryName(destinationPath);
|
||||
if (!string.IsNullOrWhiteSpace(destinationDirectory))
|
||||
Directory.CreateDirectory(destinationDirectory);
|
||||
|
||||
await using (var destinationStream = File.Create(destinationPath))
|
||||
{
|
||||
await response.Content.CopyToAsync(destinationStream, executeContext.CancellationToken);
|
||||
}
|
||||
|
||||
logger.LogInformation("Downloaded {SourceUrl} to {DestinationPath}",
|
||||
executeContext.Arguments.SourceUrl, destinationPath);
|
||||
|
||||
return executeContext.Completed(new DownloadFileFromUrlLog(
|
||||
executeContext.Arguments.SourceUrl,
|
||||
destinationPath));
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to download file from {SourceUrl}", executeContext.Arguments.SourceUrl);
|
||||
throw;
|
||||
await response.Content.CopyToAsync(destinationStream, executeContext.CancellationToken);
|
||||
}
|
||||
|
||||
logger.LogInformation("Downloaded {SourceUrl} to {DestinationPath}",
|
||||
executeContext.Arguments.SourceUrl, destinationPath);
|
||||
|
||||
return executeContext.Completed(new DownloadFileFromUrlLog(
|
||||
executeContext.Arguments.SourceUrl,
|
||||
destinationPath));
|
||||
}
|
||||
|
||||
public Task<CompensationResult> Compensate(CompensateContext<DownloadFileFromUrlLog> compensateContext)
|
||||
catch (Exception exception)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (File.Exists(compensateContext.Log.DestinationFilePath))
|
||||
File.Delete(compensateContext.Log.DestinationFilePath);
|
||||
return Task.FromResult(compensateContext.Compensated());
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to compensate downloaded file {DestinationFilePath}",
|
||||
compensateContext.Log.DestinationFilePath);
|
||||
return Task.FromResult(compensateContext.Failed(exception));
|
||||
}
|
||||
logger.LogError(exception, "Failed to download file from {SourceUrl}", executeContext.Arguments.SourceUrl);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public Task<CompensationResult> Compensate(CompensateContext<DownloadFileFromUrlLog> compensateContext)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (File.Exists(compensateContext.Log.DestinationFilePath))
|
||||
File.Delete(compensateContext.Log.DestinationFilePath);
|
||||
return Task.FromResult(compensateContext.Compensated());
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to compensate downloaded file {DestinationFilePath}",
|
||||
compensateContext.Log.DestinationFilePath);
|
||||
return Task.FromResult(compensateContext.Failed(exception));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+37
-38
@@ -1,46 +1,45 @@
|
||||
using MassTransit;
|
||||
using Amazon.S3;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Musify.Infrastructure.MassTransit.Arguments;
|
||||
using System.Net;
|
||||
using Amazon.S3;
|
||||
using MassTransit;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Musify.Application.Contracts;
|
||||
using Musify.Infrastructure.MassTransit.Arguments;
|
||||
|
||||
namespace Musify.Infrastructure.MassTransit.Activities.Files
|
||||
namespace Musify.Infrastructure.MassTransit.Activities.Files;
|
||||
|
||||
internal class RemoveFileFromBucketActivity(
|
||||
IStorageService storageHandler,
|
||||
ILogger<RemoveFileFromBucketActivity> logger)
|
||||
: IExecuteActivity<RemoveFileFromBucketArguments>
|
||||
{
|
||||
internal class RemoveFileFromBucketActivity(
|
||||
IStorageService storageHandler,
|
||||
ILogger<RemoveFileFromBucketActivity> logger)
|
||||
: 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)
|
||||
{
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
await storageHandler.RemoveFileAsync(
|
||||
executeContext.Arguments.Bucket,
|
||||
executeContext.Arguments.Key,
|
||||
executeContext.CancellationToken);
|
||||
return executeContext.Completed();
|
||||
}
|
||||
catch (AmazonS3Exception exception) when (
|
||||
exception.StatusCode == HttpStatusCode.NotFound
|
||||
|| string.Equals(exception.ErrorCode, "NoSuchKey", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
logger.LogInformation(
|
||||
"Key {Key} not found in bucket {Bucket}, skipping",
|
||||
executeContext.Arguments.Key,
|
||||
executeContext.Arguments.Bucket);
|
||||
return executeContext.Completed();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to remove {Key} from bucket {Bucket}",
|
||||
executeContext.Arguments.Key,
|
||||
executeContext.Arguments.Bucket);
|
||||
throw;
|
||||
}
|
||||
await storageHandler.RemoveFileAsync(
|
||||
executeContext.Arguments.Bucket,
|
||||
executeContext.Arguments.Key,
|
||||
executeContext.CancellationToken);
|
||||
return executeContext.Completed();
|
||||
}
|
||||
catch (AmazonS3Exception exception) when (
|
||||
exception.StatusCode == HttpStatusCode.NotFound
|
||||
|| string.Equals(exception.ErrorCode, "NoSuchKey", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
logger.LogInformation(
|
||||
"Key {Key} not found in bucket {Bucket}, skipping",
|
||||
executeContext.Arguments.Key,
|
||||
executeContext.Arguments.Bucket);
|
||||
return executeContext.Completed();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to remove {Key} from bucket {Bucket}",
|
||||
executeContext.Arguments.Key,
|
||||
executeContext.Arguments.Bucket);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+45
-46
@@ -4,62 +4,61 @@ using Musify.Application.Contracts;
|
||||
using Musify.Infrastructure.MassTransit.Arguments;
|
||||
using Musify.Infrastructure.MassTransit.Logs;
|
||||
|
||||
namespace Musify.Infrastructure.MassTransit.Activities.Files
|
||||
namespace Musify.Infrastructure.MassTransit.Activities.Files;
|
||||
|
||||
internal class TransferFilesToBucketActivity(
|
||||
IStorageService storageHandler,
|
||||
ILogger<TransferFilesToBucketActivity> logger)
|
||||
: IActivity<TransferFilesToBucketArguments, TransferFilesToBucketLog>
|
||||
{
|
||||
internal class TransferFilesToBucketActivity(
|
||||
IStorageService storageHandler,
|
||||
ILogger<TransferFilesToBucketActivity> logger)
|
||||
: IActivity<TransferFilesToBucketArguments, TransferFilesToBucketLog>
|
||||
public const string ExecuteEndpointName = "transfer-files-to-bucket";
|
||||
|
||||
public async Task<ExecutionResult> Execute(ExecuteContext<TransferFilesToBucketArguments> executeContext)
|
||||
{
|
||||
public const string ExecuteEndpointName = "transfer-files-to-bucket";
|
||||
var folderPath = executeContext.GetVariable<string>(executeContext.Arguments.SourceDirectoryVariable);
|
||||
ArgumentNullException.ThrowIfNull(folderPath, nameof(folderPath));
|
||||
var destinationKey = executeContext.Arguments.DestinationKey;
|
||||
ArgumentNullException.ThrowIfNull(destinationKey, nameof(destinationKey));
|
||||
|
||||
public async Task<ExecutionResult> Execute(ExecuteContext<TransferFilesToBucketArguments> executeContext)
|
||||
try
|
||||
{
|
||||
var folderPath = executeContext.GetVariable<string>(executeContext.Arguments.SourceDirectoryVariable);
|
||||
ArgumentNullException.ThrowIfNull(folderPath, nameof(folderPath));
|
||||
var destinationKey = executeContext.Arguments.DestinationKey;
|
||||
ArgumentNullException.ThrowIfNull(destinationKey, nameof(destinationKey));
|
||||
var uploadedKeys = await storageHandler.TransferFilesAsync(
|
||||
folderPath,
|
||||
executeContext.Arguments.DestinationBucket,
|
||||
destinationKey,
|
||||
executeContext.CancellationToken);
|
||||
|
||||
try
|
||||
{
|
||||
var uploadedKeys = await storageHandler.TransferFilesAsync(
|
||||
folderPath,
|
||||
executeContext.Arguments.DestinationBucket,
|
||||
destinationKey,
|
||||
executeContext.CancellationToken);
|
||||
|
||||
return executeContext.Completed(new TransferFilesToBucketLog(
|
||||
executeContext.Arguments.DestinationBucket,
|
||||
uploadedKeys.ToArray()));
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to transfer files from {FolderPath} to {DestinationBucket}",
|
||||
folderPath,
|
||||
executeContext.Arguments.DestinationBucket);
|
||||
throw;
|
||||
}
|
||||
return executeContext.Completed(new TransferFilesToBucketLog(
|
||||
executeContext.Arguments.DestinationBucket,
|
||||
uploadedKeys.ToArray()));
|
||||
}
|
||||
|
||||
public async Task<CompensationResult> Compensate(CompensateContext<TransferFilesToBucketLog> compensateContext)
|
||||
catch (Exception exception)
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (var uploadedKey in compensateContext.Log.UploadedKeys)
|
||||
{
|
||||
await storageHandler.RemoveFileAsync(
|
||||
compensateContext.Log.DestinationBucket,
|
||||
uploadedKey,
|
||||
compensateContext.CancellationToken);
|
||||
}
|
||||
logger.LogError(exception, "Failed to transfer files from {FolderPath} to {DestinationBucket}",
|
||||
folderPath,
|
||||
executeContext.Arguments.DestinationBucket);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
return compensateContext.Compensated();
|
||||
}
|
||||
catch (Exception exception)
|
||||
public async Task<CompensationResult> Compensate(CompensateContext<TransferFilesToBucketLog> compensateContext)
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (var uploadedKey in compensateContext.Log.UploadedKeys)
|
||||
{
|
||||
logger.LogError(exception, "Failed to compensate file transfers to {DestinationBucket}", compensateContext.Log.DestinationBucket);
|
||||
return compensateContext.Failed(exception);
|
||||
await storageHandler.RemoveFileAsync(
|
||||
compensateContext.Log.DestinationBucket,
|
||||
uploadedKey,
|
||||
compensateContext.CancellationToken);
|
||||
}
|
||||
|
||||
return compensateContext.Compensated();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to compensate file transfers to {DestinationBucket}", compensateContext.Log.DestinationBucket);
|
||||
return compensateContext.Failed(exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+46
-47
@@ -4,58 +4,57 @@ using Musify.Application.Contracts;
|
||||
using Musify.Infrastructure.MassTransit.Arguments;
|
||||
using Musify.Infrastructure.MassTransit.Logs;
|
||||
|
||||
namespace Musify.Infrastructure.MassTransit.Activities.Files
|
||||
namespace Musify.Infrastructure.MassTransit.Activities.Files;
|
||||
|
||||
internal class UploadFileToBucketActivity(IStorageService storageHandler,
|
||||
ILogger<UploadFileToBucketActivity> logger)
|
||||
: IActivity<UploadFileToBucketArguments, UploadFileToBucketLog>
|
||||
{
|
||||
internal class UploadFileToBucketActivity(IStorageService storageHandler,
|
||||
ILogger<UploadFileToBucketActivity> logger)
|
||||
: IActivity<UploadFileToBucketArguments, UploadFileToBucketLog>
|
||||
public const string ExecuteEndpointName = "upload-file-to-bucket";
|
||||
|
||||
public async Task<ExecutionResult> Execute(ExecuteContext<UploadFileToBucketArguments> executeContext)
|
||||
{
|
||||
public const string ExecuteEndpointName = "upload-file-to-bucket";
|
||||
var sourceFilePath = executeContext.GetVariable<string>(executeContext.Arguments.FilePathVariable);
|
||||
ArgumentNullException.ThrowIfNull(sourceFilePath, nameof(sourceFilePath));
|
||||
var fileName = Path.GetFileName(sourceFilePath);
|
||||
var destinationKey = string.Join('/', new[] { executeContext.Arguments.DestinationRoute, fileName }
|
||||
.Where(static s => !string.IsNullOrWhiteSpace(s))
|
||||
.Select(static s => s.Trim().Trim('/', '\\')));
|
||||
|
||||
public async Task<ExecutionResult> Execute(ExecuteContext<UploadFileToBucketArguments> executeContext)
|
||||
try
|
||||
{
|
||||
var sourceFilePath = executeContext.GetVariable<string>(executeContext.Arguments.FilePathVariable);
|
||||
ArgumentNullException.ThrowIfNull(sourceFilePath, nameof(sourceFilePath));
|
||||
var fileName = Path.GetFileName(sourceFilePath);
|
||||
var destinationKey = string.Join('/', new[] { executeContext.Arguments.DestinationRoute, fileName }
|
||||
.Where(static s => !string.IsNullOrWhiteSpace(s))
|
||||
.Select(static s => s.Trim().Trim('/', '\\')));
|
||||
|
||||
try
|
||||
{
|
||||
await storageHandler.UploadFileAsync(
|
||||
sourceFilePath,
|
||||
executeContext.Arguments.DestinationBucket,
|
||||
destinationKey,
|
||||
executeContext.CancellationToken);
|
||||
return executeContext.CompletedWithVariables(new UploadFileToBucketLog(
|
||||
executeContext.Arguments.DestinationBucket,
|
||||
destinationKey));
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to upload file to bucket");
|
||||
throw;
|
||||
}
|
||||
await storageHandler.UploadFileAsync(
|
||||
sourceFilePath,
|
||||
executeContext.Arguments.DestinationBucket,
|
||||
destinationKey,
|
||||
executeContext.CancellationToken);
|
||||
return executeContext.CompletedWithVariables(new UploadFileToBucketLog(
|
||||
executeContext.Arguments.DestinationBucket,
|
||||
destinationKey));
|
||||
}
|
||||
|
||||
public async Task<CompensationResult> Compensate(CompensateContext<UploadFileToBucketLog> compensateContext)
|
||||
catch (Exception exception)
|
||||
{
|
||||
try
|
||||
{
|
||||
await storageHandler.RemoveFileAsync(
|
||||
compensateContext.Log.DestinationBucket,
|
||||
compensateContext.Log.DestinationKey,
|
||||
compensateContext.CancellationToken);
|
||||
return compensateContext.Compensated();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Error compensating file upload for {Bucket}/{Key}",
|
||||
compensateContext.Log.DestinationBucket,
|
||||
compensateContext.Log.DestinationKey);
|
||||
return compensateContext.Failed(exception);
|
||||
}
|
||||
logger.LogError(exception, "Failed to upload file to bucket");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<CompensationResult> Compensate(CompensateContext<UploadFileToBucketLog> compensateContext)
|
||||
{
|
||||
try
|
||||
{
|
||||
await storageHandler.RemoveFileAsync(
|
||||
compensateContext.Log.DestinationBucket,
|
||||
compensateContext.Log.DestinationKey,
|
||||
compensateContext.CancellationToken);
|
||||
return compensateContext.Compensated();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Error compensating file upload for {Bucket}/{Key}",
|
||||
compensateContext.Log.DestinationBucket,
|
||||
compensateContext.Log.DestinationKey);
|
||||
return compensateContext.Failed(exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user