Remove dead code: unused storage/transcoder members, Upload table, retry columns, redundant packages
This commit is contained in:
@@ -75,47 +75,6 @@ public partial class AudioService(ILogger<AudioService> logger,
|
||||
return new TimeSpan(hours, minutes, 0) + TimeSpan.FromSeconds(seconds);
|
||||
}
|
||||
|
||||
public async Task<bool> IsValidAudioFileAsync(string filePath, CancellationToken cancellationToken)
|
||||
{
|
||||
if (!File.Exists(filePath))
|
||||
{
|
||||
logger.LogWarning("Audio validation requested for non-existing file {FilePath}", filePath);
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
logger.LogDebug("Validating audio file {FilePath}", filePath);
|
||||
|
||||
var executionResult = await ExecuteFfmpegAsync(
|
||||
BuildValidateAudioArguments(filePath),
|
||||
Path.GetDirectoryName(filePath) ?? Environment.CurrentDirectory,
|
||||
"ValidateAudioFile",
|
||||
cancellationToken);
|
||||
|
||||
var isValid = executionResult.ExitCode == 0;
|
||||
if (!isValid)
|
||||
{
|
||||
logger.LogWarning("Audio validation failed for {FilePath}: {StandardError}", filePath, executionResult.StandardError);
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.LogDebug("Audio validation successful for {FilePath}", filePath);
|
||||
}
|
||||
|
||||
return isValid;
|
||||
}
|
||||
catch (TimeoutException timeoutException)
|
||||
{
|
||||
logger.LogError(timeoutException, "Audio validation timed out for {FilePath}", filePath);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to validate audio file {FilePath}", filePath);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private async Task<FfmpegExecutionResult> ExecuteFfmpegWithInputAsync(
|
||||
string arguments,
|
||||
string workingDirectory,
|
||||
@@ -239,9 +198,6 @@ public partial class AudioService(ILogger<AudioService> logger,
|
||||
$" \"{outputPath}\"";
|
||||
}
|
||||
|
||||
private static string BuildValidateAudioArguments(string filePath)
|
||||
=> $"-v error -i \"{filePath}\" -map 0:a:0 -f null -";
|
||||
|
||||
private static string BuildProbeDurationArguments(string filePath)
|
||||
=> $"-hide_banner -i \"{filePath}\" -f null -";
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ public static class ServicesDependencyInjection
|
||||
return serviceDescriptors;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddPictureService(this IServiceCollection serviceDescriptors, IConfiguration configuration)
|
||||
public static IServiceCollection AddPictureService(this IServiceCollection serviceDescriptors)
|
||||
{
|
||||
serviceDescriptors.AddScoped<IPictureService, PictureService>();
|
||||
return serviceDescriptors;
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
using System.Collections.Concurrent;
|
||||
using ErrorOr;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
|
||||
namespace Musify.Infrastructure.Services;
|
||||
|
||||
public sealed class SingleFlightCache(IMemoryCache cache)
|
||||
{
|
||||
private readonly ConcurrentDictionary<string, object> inFlight = new();
|
||||
|
||||
public async Task<ErrorOr<TValue>> GetOrCreateAsync<TValue>(
|
||||
string key,
|
||||
TimeSpan duration,
|
||||
Func<Task<ErrorOr<TValue>>> factory)
|
||||
{
|
||||
if (duration <= TimeSpan.Zero)
|
||||
return await factory();
|
||||
|
||||
if (cache.TryGetValue(key, out TValue? cached) && cached != null)
|
||||
return cached;
|
||||
|
||||
var pending = (Lazy<Task<ErrorOr<TValue>>>)inFlight.GetOrAdd(
|
||||
key,
|
||||
_ => new Lazy<Task<ErrorOr<TValue>>>(factory));
|
||||
|
||||
try
|
||||
{
|
||||
var result = await pending.Value;
|
||||
|
||||
if (!result.IsError)
|
||||
cache.Set(key, result.Value, duration);
|
||||
|
||||
return result;
|
||||
}
|
||||
finally
|
||||
{
|
||||
inFlight.TryRemove(key, out _);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,14 +4,12 @@ using Amazon.S3.Transfer;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using MimeMapping;
|
||||
using Musify.Application.Contracts;
|
||||
using Musify.Domain.Entities;
|
||||
using Musify.Domain.ValueObjects;
|
||||
using Musify.Infrastructure.Configuration;
|
||||
using ObjectMetaData = Musify.Application.Contracts.ObjectMetaData;
|
||||
|
||||
namespace Musify.Infrastructure.Services;
|
||||
|
||||
public class StorageService(IDatabase database, IAmazonS3 amazonS3, ILogger<StorageService> logger,
|
||||
public class StorageService(IAmazonS3 amazonS3, ILogger<StorageService> logger,
|
||||
InfrastructureStorageConfiguration storageClientConfiguration)
|
||||
: IStorageService
|
||||
{
|
||||
@@ -41,20 +39,6 @@ public class StorageService(IDatabase database, IAmazonS3 amazonS3, ILogger<Stor
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<string> GetUrlAsync(string bucket, string key, TimeSpan expirationTime, CancellationToken cancellationToken)
|
||||
{
|
||||
var request = new GetPreSignedUrlRequest
|
||||
{
|
||||
BucketName = bucket,
|
||||
Key = key,
|
||||
Expires = DateTime.UtcNow + expirationTime,
|
||||
Protocol = storageClientConfiguration.UseHttp ? Protocol.HTTP : Protocol.HTTPS
|
||||
};
|
||||
|
||||
logger.LogDebug("Generating pre-signed URL for {Bucket}/{Key}", bucket, key);
|
||||
return await amazonS3.GetPreSignedURLAsync(request);
|
||||
}
|
||||
|
||||
public async Task<string> GetUploadUrlAsync(
|
||||
string bucket,
|
||||
string key,
|
||||
@@ -129,15 +113,6 @@ public class StorageService(IDatabase database, IAmazonS3 amazonS3, ILogger<Stor
|
||||
.Where(static s => !string.IsNullOrWhiteSpace(s))
|
||||
.Select(static s => s.Trim().Trim('/', '\\')));
|
||||
var contentType = MimeUtility.GetMimeMapping(filePath);
|
||||
var upload = new Upload
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Bucket = bucket,
|
||||
Key = key,
|
||||
ContentType = contentType
|
||||
};
|
||||
await database.Uploads.AddAsync(upload, cancellationToken);
|
||||
|
||||
try
|
||||
{
|
||||
await trasnsferUtility.UploadAsync(
|
||||
@@ -149,7 +124,6 @@ public class StorageService(IDatabase database, IAmazonS3 amazonS3, ILogger<Stor
|
||||
ContentType = contentType
|
||||
},
|
||||
cancellationToken);
|
||||
upload.State = UploadState.Successful;
|
||||
uploadedKeys.Add(key);
|
||||
successCount++;
|
||||
logger.LogDebug("Transferred file to S3 {Bucket}/{Key}", bucket, key);
|
||||
@@ -157,13 +131,11 @@ public class StorageService(IDatabase database, IAmazonS3 amazonS3, ILogger<Stor
|
||||
catch (Exception exception)
|
||||
{
|
||||
failedCount++;
|
||||
upload.State = UploadState.Failed;
|
||||
logger.LogError(exception, "Failed to transfer file to S3 {Bucket}/{Key}", bucket, key);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
await database.SaveChangesAsync(cancellationToken);
|
||||
logger.LogInformation("File transfer completed to {Bucket}/{Route}. Success: {SuccessCount}, Failed: {FailedCount}", bucket, route, successCount, failedCount);
|
||||
|
||||
return uploadedKeys;
|
||||
@@ -194,31 +166,16 @@ public class StorageService(IDatabase database, IAmazonS3 amazonS3, ILogger<Stor
|
||||
Key = key
|
||||
};
|
||||
|
||||
var upload = new Upload
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Bucket = bucket,
|
||||
Key = key,
|
||||
ContentType = contentType
|
||||
};
|
||||
await database.Uploads.AddAsync(upload, cancellationToken);
|
||||
|
||||
try
|
||||
{
|
||||
var response = await amazonS3.PutObjectAsync(request, cancellationToken);
|
||||
await amazonS3.PutObjectAsync(request, cancellationToken);
|
||||
logger.LogDebug("Uploaded file to S3 {Bucket}/{Key}", bucket, key);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to upload file to S3 {Bucket}/{Key}", bucket, key);
|
||||
upload.State = UploadState.Failed;
|
||||
await database.SaveChangesAsync(cancellationToken);
|
||||
throw;
|
||||
}
|
||||
|
||||
upload.State = UploadState.Successful;
|
||||
await database.SaveChangesAsync(cancellationToken);
|
||||
logger.LogDebug("Saved upload record for {Bucket}/{Key}", bucket, key);
|
||||
}
|
||||
|
||||
public async Task CopyFileAsync(string sourceBucket, string sourceKey, string destinationBucket, string destinationKey,
|
||||
@@ -234,7 +191,7 @@ public class StorageService(IDatabase database, IAmazonS3 amazonS3, ILogger<Stor
|
||||
|
||||
try
|
||||
{
|
||||
var response = await amazonS3.CopyObjectAsync(request, cancellationToken);
|
||||
await amazonS3.CopyObjectAsync(request, cancellationToken);
|
||||
logger.LogInformation("Copied file from {SourceBucket}/{SourceKey} to {DestinationBucket}/{DestinationKey}",
|
||||
sourceBucket, sourceKey, destinationBucket, destinationKey);
|
||||
}
|
||||
@@ -266,43 +223,4 @@ public class StorageService(IDatabase database, IAmazonS3 amazonS3, ILogger<Stor
|
||||
}
|
||||
while (listResponse.IsTruncated.HasValue && listResponse.IsTruncated.Value);
|
||||
}
|
||||
|
||||
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 == null || 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);
|
||||
|
||||
var deleteErrors = deleteResponse.DeleteErrors ?? [];
|
||||
if (deleteErrors.Count > 0)
|
||||
{
|
||||
var errors = string.Join(", ", 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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user