Generated response
This commit is contained in:
@@ -3,7 +3,9 @@ using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Musify.Application.Abstractions.Infrastructure;
|
||||
using Musify.Application.Configuration;
|
||||
using Musify.Infrastructure.Configuration;
|
||||
using Musify.Infrastructure.Jobs;
|
||||
|
||||
namespace Musify.Infrastructure.Services
|
||||
{
|
||||
@@ -41,6 +43,31 @@ namespace Musify.Infrastructure.Services
|
||||
return serviceDescriptors;
|
||||
}
|
||||
|
||||
// Called by both the web API host and the worker
|
||||
public static IServiceCollection AddUploadIntentConfiguration(this IServiceCollection serviceDescriptors, IConfiguration configuration)
|
||||
{
|
||||
serviceDescriptors
|
||||
.AddOptionsWithValidateOnStart<UploadIntentConfiguration>()
|
||||
.Bind(configuration.GetRequiredSection(UploadIntentConfiguration.SectionName))
|
||||
.ValidateDataAnnotations();
|
||||
|
||||
serviceDescriptors.AddSingleton(serviceProvider =>
|
||||
serviceProvider.GetRequiredService<IOptions<UploadIntentConfiguration>>().Value);
|
||||
|
||||
return serviceDescriptors;
|
||||
}
|
||||
|
||||
// Called only by the worker host (registers background jobs)
|
||||
public static IServiceCollection AddUploadIntentJobs(this IServiceCollection serviceDescriptors, IConfiguration configuration)
|
||||
{
|
||||
serviceDescriptors.AddUploadIntentConfiguration(configuration);
|
||||
|
||||
serviceDescriptors.AddHostedService<UploadIntentExpirationJob>();
|
||||
serviceDescriptors.AddHostedService<TempUploadsCleanupJob>();
|
||||
|
||||
return serviceDescriptors;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddAudioTranscoder(this IServiceCollection serviceDescriptors, IConfiguration configuration)
|
||||
{
|
||||
serviceDescriptors
|
||||
|
||||
@@ -6,6 +6,7 @@ using MimeMapping;
|
||||
using Musify.Application.Abstractions.Infrastructure;
|
||||
using Musify.Domain.Entities;
|
||||
using Musify.Infrastructure.Configuration;
|
||||
using ObjectMetadata = Musify.Application.Abstractions.Infrastructure.ObjectMetadata;
|
||||
|
||||
namespace Musify.Infrastructure.Services
|
||||
{
|
||||
@@ -69,6 +70,24 @@ namespace Musify.Infrastructure.Services
|
||||
return await amazonS3.GetPreSignedURLAsync(request);
|
||||
}
|
||||
|
||||
public async Task<ObjectMetadata?> HeadObjectAsync(string bucket, string key, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = await amazonS3.GetObjectMetadataAsync(bucket, key, cancellationToken);
|
||||
return new ObjectMetadata(response.Headers.ContentType, response.ContentLength);
|
||||
}
|
||||
catch (AmazonS3Exception ex) when (ex.StatusCode == System.Net.HttpStatusCode.NotFound)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "HEAD failed for {Bucket}/{Key}", bucket, key);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task RemoveFileAsync(string bucket, string key, CancellationToken cancellationToken)
|
||||
{
|
||||
var request = new DeleteObjectRequest
|
||||
@@ -204,6 +223,27 @@ namespace Musify.Infrastructure.Services
|
||||
}
|
||||
}
|
||||
|
||||
public async IAsyncEnumerable<(string Key, DateTime LastModifiedUtc)> ListObjectsAsync(
|
||||
string bucket, string prefix, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken)
|
||||
{
|
||||
var listRequest = new ListObjectsV2Request
|
||||
{
|
||||
BucketName = bucket,
|
||||
Prefix = prefix.EndsWith('/') ? prefix : $"{prefix}/"
|
||||
};
|
||||
|
||||
ListObjectsV2Response listResponse;
|
||||
do
|
||||
{
|
||||
listResponse = await amazonS3.ListObjectsV2Async(listRequest, cancellationToken);
|
||||
foreach (var obj in listResponse.S3Objects)
|
||||
yield return (obj.Key, (obj.LastModified ?? DateTime.UtcNow).ToUniversalTime());
|
||||
|
||||
listRequest.ContinuationToken = listResponse.NextContinuationToken;
|
||||
}
|
||||
while (listResponse.IsTruncated.HasValue && listResponse.IsTruncated.Value);
|
||||
}
|
||||
|
||||
public async Task RemoveFolderAsync(string bucket, string folderKey, CancellationToken cancellationToken)
|
||||
{
|
||||
var prefix = folderKey.EndsWith('/') ? folderKey : $"{folderKey}/";
|
||||
|
||||
Reference in New Issue
Block a user