Change editor rule

This commit is contained in:
2026-09-24 13:24:43 +02:00
parent 16478678b5
commit 73e37dbecd
401 changed files with 14735 additions and 12995 deletions
@@ -5,171 +5,172 @@ using Microsoft.Extensions.Logging;
using Musify.Application.Contracts;
using Musify.Infrastructure.Configuration;
namespace Musify.Infrastructure.Services;
public sealed partial class AudioTranscoderService(
ILogger<AudioTranscoderService> logger,
AudioConfiguration audioConfiguration)
: IAudioTranscoderService
namespace Musify.Infrastructure.Services
{
private sealed record FfmpegExecutionResult(int ExitCode, string StandardOutput, string StandardError);
[GeneratedRegex(@"Duration:\s*(\d+):(\d{2}):(\d{2}(?:\.\d+)?)", RegexOptions.IgnoreCase)]
private static partial Regex DurationRegex();
public async Task<AudioTranscodeResult> TranscodeToAudioFileAsync(Stream audioStream, string destinationPath, CancellationToken cancellationToken)
public sealed partial class AudioTranscoderService(
ILogger<AudioTranscoderService> logger,
AudioConfiguration audioConfiguration)
: IAudioTranscoderService
{
Directory.CreateDirectory(destinationPath);
private sealed record FfmpegExecutionResult(int ExitCode, string StandardOutput, string StandardError);
logger.LogInformation("Starting audio transcoding for {DestinationPath}", destinationPath);
[GeneratedRegex(@"Duration:\s*(\d+):(\d{2}):(\d{2}(?:\.\d+)?)", RegexOptions.IgnoreCase)]
private static partial Regex DurationRegex();
var executionResult = await ExecuteFfmpegAsync(
BuildAudioFileArguments(destinationPath, audioConfiguration.Ffmpeg),
destinationPath,
audioStream,
"TranscodeToAudioFile",
cancellationToken);
if (executionResult.ExitCode != 0)
public async Task<AudioTranscodeResult> TranscodeToAudioFileAsync(Stream audioStream, string destinationPath, CancellationToken cancellationToken)
{
logger.LogError("Audio transcoding failed with exit code {ExitCode}. Output: {StandardOutput}, Error: {StandardError}",
executionResult.ExitCode, executionResult.StandardOutput, executionResult.StandardError);
return new AudioTranscodeResult(executionResult.ExitCode, TimeSpan.Zero);
}
Directory.CreateDirectory(destinationPath);
logger.LogInformation("Audio transcoding completed for {DestinationPath}", destinationPath);
logger.LogInformation("Starting audio transcoding for {DestinationPath}", destinationPath);
var outputPath = Path.Combine(destinationPath, audioConfiguration.Ffmpeg.OutputFileName);
var duration = await GetAudioDurationAsync(outputPath, cancellationToken);
return new AudioTranscodeResult(executionResult.ExitCode, duration);
}
var executionResult = await ExecuteFfmpegAsync(
BuildAudioFileArguments(destinationPath, audioConfiguration.Ffmpeg),
destinationPath,
audioStream,
"TranscodeToAudioFile",
cancellationToken);
private async Task<TimeSpan> GetAudioDurationAsync(string filePath, CancellationToken cancellationToken)
{
var executionResult = await ExecuteFfmpegAsync(
BuildProbeDurationArguments(filePath),
Path.GetDirectoryName(filePath) ?? Environment.CurrentDirectory,
input: null,
"ProbeAudioDuration",
cancellationToken);
var match = DurationRegex().Match(executionResult.StandardError);
if (!match.Success)
throw new InvalidOperationException($"Could not determine audio duration for {filePath}.");
var hours = int.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture);
var minutes = int.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture);
var seconds = double.Parse(match.Groups[3].Value, CultureInfo.InvariantCulture);
return new TimeSpan(hours, minutes, 0) + TimeSpan.FromSeconds(seconds);
}
private async Task<FfmpegExecutionResult> ExecuteFfmpegAsync(
string arguments,
string workingDirectory,
Stream? input,
string operationName,
CancellationToken cancellationToken)
{
using var process = BuildProcess(arguments, workingDirectory, redirectStandardInput: input != null);
process.Start();
logger.LogDebug("[{Operation}] ffmpeg started (PID: {ProcessId}) in {WorkingDirectory}", operationName, process.Id, workingDirectory);
var outputTask = process.StandardOutput.ReadToEndAsync(cancellationToken);
var errorTask = process.StandardError.ReadToEndAsync(cancellationToken);
if (input != null)
{
if (input.CanSeek)
input.Position = 0;
await input.CopyToAsync(process.StandardInput.BaseStream, cancellationToken);
process.StandardInput.Close();
}
return await WaitForProcessResultAsync(process, outputTask, errorTask, operationName, cancellationToken);
}
private Process BuildProcess(string arguments, string workingDirectory, bool redirectStandardInput)
=> new()
{
StartInfo = new ProcessStartInfo
if (executionResult.ExitCode != 0)
{
FileName = audioConfiguration.Ffmpeg.ExecutableName,
WorkingDirectory = workingDirectory,
Arguments = arguments,
RedirectStandardInput = redirectStandardInput,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
logger.LogError("Audio transcoding failed with exit code {ExitCode}. Output: {StandardOutput}, Error: {StandardError}",
executionResult.ExitCode, executionResult.StandardOutput, executionResult.StandardError);
return new AudioTranscodeResult(executionResult.ExitCode, TimeSpan.Zero);
}
};
private async Task<FfmpegExecutionResult> WaitForProcessResultAsync(
Process process,
Task<string> outputTask,
Task<string> errorTask,
string operationName,
CancellationToken cancellationToken)
{
using var linkedCancellationTokens = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
linkedCancellationTokens.CancelAfter(audioConfiguration.TranscodingTimeout);
logger.LogInformation("Audio transcoding completed for {DestinationPath}", destinationPath);
try
{
await process.WaitForExitAsync(linkedCancellationTokens.Token);
}
catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested)
{
TryKillProcess(process);
throw new TimeoutException($"ffmpeg execution exceeded timeout {audioConfiguration.TranscodingTimeout} (PID: {process.Id}).");
var outputPath = Path.Combine(destinationPath, audioConfiguration.Ffmpeg.OutputFileName);
var duration = await GetAudioDurationAsync(outputPath, cancellationToken);
return new AudioTranscodeResult(executionResult.ExitCode, duration);
}
var standardError = await errorTask;
var standardOutput = await outputTask;
private async Task<TimeSpan> GetAudioDurationAsync(string filePath, CancellationToken cancellationToken)
{
var executionResult = await ExecuteFfmpegAsync(
BuildProbeDurationArguments(filePath),
Path.GetDirectoryName(filePath) ?? Environment.CurrentDirectory,
input: null,
"ProbeAudioDuration",
cancellationToken);
logger.LogDebug("[{Operation}] ffmpeg finished (PID: {ProcessId}). Exit code: {ExitCode}, Output: {OutputLength}B, Error: {ErrorLength}B",
operationName, process.Id, process.ExitCode, standardOutput.Length, standardError.Length);
var match = DurationRegex().Match(executionResult.StandardError);
if (!match.Success)
throw new InvalidOperationException($"Could not determine audio duration for {filePath}.");
return new FfmpegExecutionResult(process.ExitCode, standardOutput, standardError);
var hours = int.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture);
var minutes = int.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture);
var seconds = double.Parse(match.Groups[3].Value, CultureInfo.InvariantCulture);
return new TimeSpan(hours, minutes, 0) + TimeSpan.FromSeconds(seconds);
}
private async Task<FfmpegExecutionResult> ExecuteFfmpegAsync(
string arguments,
string workingDirectory,
Stream? input,
string operationName,
CancellationToken cancellationToken)
{
using var process = BuildProcess(arguments, workingDirectory, redirectStandardInput: input != null);
process.Start();
logger.LogDebug("[{Operation}] ffmpeg started (PID: {ProcessId}) in {WorkingDirectory}", operationName, process.Id, workingDirectory);
var outputTask = process.StandardOutput.ReadToEndAsync(cancellationToken);
var errorTask = process.StandardError.ReadToEndAsync(cancellationToken);
if (input != null)
{
if (input.CanSeek)
input.Position = 0;
await input.CopyToAsync(process.StandardInput.BaseStream, cancellationToken);
process.StandardInput.Close();
}
return await WaitForProcessResultAsync(process, outputTask, errorTask, operationName, cancellationToken);
}
private Process BuildProcess(string arguments, string workingDirectory, bool redirectStandardInput)
=> new()
{
StartInfo = new ProcessStartInfo
{
FileName = audioConfiguration.Ffmpeg.ExecutableName,
WorkingDirectory = workingDirectory,
Arguments = arguments,
RedirectStandardInput = redirectStandardInput,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
}
};
private async Task<FfmpegExecutionResult> WaitForProcessResultAsync(
Process process,
Task<string> outputTask,
Task<string> errorTask,
string operationName,
CancellationToken cancellationToken)
{
using var linkedCancellationTokens = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
linkedCancellationTokens.CancelAfter(audioConfiguration.TranscodingTimeout);
try
{
await process.WaitForExitAsync(linkedCancellationTokens.Token);
}
catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested)
{
TryKillProcess(process);
throw new TimeoutException($"ffmpeg execution exceeded timeout {audioConfiguration.TranscodingTimeout} (PID: {process.Id}).");
}
var standardError = await errorTask;
var standardOutput = await outputTask;
logger.LogDebug("[{Operation}] ffmpeg finished (PID: {ProcessId}). Exit code: {ExitCode}, Output: {OutputLength}B, Error: {ErrorLength}B",
operationName, process.Id, process.ExitCode, standardOutput.Length, standardError.Length);
return new FfmpegExecutionResult(process.ExitCode, standardOutput, standardError);
}
private void TryKillProcess(Process process)
{
try
{
if (!process.HasExited)
process.Kill();
}
catch (InvalidOperationException exception)
{
logger.LogWarning(exception, "Could not kill ffmpeg process (PID: {ProcessId}); it had already exited", process.Id);
}
}
private static string BuildAudioFileArguments(string outputDirectory, FfmpegConfiguration ffmpegConfiguration)
{
var outputPath = Path.Combine(outputDirectory, ffmpegConfiguration.OutputFileName);
var additionalArguments = string.IsNullOrWhiteSpace(ffmpegConfiguration.AdditionalArguments)
? string.Empty
: $" {ffmpegConfiguration.AdditionalArguments.Trim()}";
return "-y -hide_banner -loglevel error " +
"-i pipe:0 " +
"-vn " +
"-map 0:a " +
$"-c:a {ffmpegConfiguration.AudioCodec} " +
$"-b:a {ffmpegConfiguration.AudioBitrate} " +
$"-ac {ffmpegConfiguration.AudioChannels} " +
$"-ar {ffmpegConfiguration.AudioSampleRate} " +
$"-profile:a {ffmpegConfiguration.AudioProfile} " +
"-movflags +faststart" +
additionalArguments +
$" \"{outputPath}\"";
}
private static string BuildProbeDurationArguments(string filePath)
=> $"-hide_banner -i \"{filePath}\" -f null -";
}
private void TryKillProcess(Process process)
{
try
{
if (!process.HasExited)
process.Kill();
}
catch (InvalidOperationException exception)
{
logger.LogWarning(exception, "Could not kill ffmpeg process (PID: {ProcessId}); it had already exited", process.Id);
}
}
private static string BuildAudioFileArguments(string outputDirectory, FfmpegConfiguration ffmpegConfiguration)
{
var outputPath = Path.Combine(outputDirectory, ffmpegConfiguration.OutputFileName);
var additionalArguments = string.IsNullOrWhiteSpace(ffmpegConfiguration.AdditionalArguments)
? string.Empty
: $" {ffmpegConfiguration.AdditionalArguments.Trim()}";
return "-y -hide_banner -loglevel error " +
"-i pipe:0 " +
"-vn " +
"-map 0:a " +
$"-c:a {ffmpegConfiguration.AudioCodec} " +
$"-b:a {ffmpegConfiguration.AudioBitrate} " +
$"-ac {ffmpegConfiguration.AudioChannels} " +
$"-ar {ffmpegConfiguration.AudioSampleRate} " +
$"-profile:a {ffmpegConfiguration.AudioProfile} " +
"-movflags +faststart" +
additionalArguments +
$" \"{outputPath}\"";
}
private static string BuildProbeDurationArguments(string filePath)
=> $"-hide_banner -i \"{filePath}\" -f null -";
}
@@ -3,29 +3,30 @@ using Musify.Application.Contracts;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
namespace Musify.Infrastructure.Services;
public class PictureService(ILogger<PictureService> logger)
: IPictureService
namespace Musify.Infrastructure.Services
{
public async Task<Stream> ResizePictureAsWebpAsync(Stream pictureStream, int width, int height, CancellationToken cancellationToken)
public class PictureService(ILogger<PictureService> logger)
: IPictureService
{
logger.LogDebug("Resizing picture to {Width}x{Height}", width, height);
if (pictureStream.CanSeek && pictureStream.Position > 0)
pictureStream.Position = 0;
using var picture = await Image.LoadAsync(pictureStream, cancellationToken);
picture.Mutate(options => options.Resize(new ResizeOptions
public async Task<Stream> ResizePictureAsWebpAsync(Stream pictureStream, int width, int height, CancellationToken cancellationToken)
{
Size = new Size(width, height),
Mode = ResizeMode.Max
}));
logger.LogDebug("Resizing picture to {Width}x{Height}", width, height);
if (pictureStream.CanSeek && pictureStream.Position > 0)
pictureStream.Position = 0;
var memoryStream = new MemoryStream();
await picture.SaveAsWebpAsync(memoryStream, cancellationToken);
memoryStream.Position = 0;
using var picture = await Image.LoadAsync(pictureStream, cancellationToken);
picture.Mutate(options => options.Resize(new ResizeOptions
{
Size = new Size(width, height),
Mode = ResizeMode.Max
}));
logger.LogDebug("Picture resized to {Width}x{Height}, length: {Length}", width, height, memoryStream.Length);
return memoryStream;
var memoryStream = new MemoryStream();
await picture.SaveAsWebpAsync(memoryStream, cancellationToken);
memoryStream.Position = 0;
logger.LogDebug("Picture resized to {Width}x{Height}, length: {Length}", width, height, memoryStream.Length);
return memoryStream;
}
}
}
@@ -8,68 +8,69 @@ using Musify.Application.Contracts;
using Musify.Infrastructure.Configuration;
using Musify.Infrastructure.Jobs;
namespace Musify.Infrastructure.Services;
public static class ServicesDependencyInjection
namespace Musify.Infrastructure.Services
{
public static IServiceCollection AddStorageService(this IServiceCollection services, IConfiguration configuration)
public static class ServicesDependencyInjection
{
services.AddValidatedOptions<InfrastructureStorageConfiguration>(configuration);
services.AddSingleton<IAmazonS3>(serviceProvider =>
public static IServiceCollection AddStorageService(this IServiceCollection services, IConfiguration configuration)
{
var configuration = serviceProvider.GetRequiredService<InfrastructureStorageConfiguration>();
var s3Configuration = new AmazonS3Config
services.AddValidatedOptions<InfrastructureStorageConfiguration>(configuration);
services.AddSingleton<IAmazonS3>(serviceProvider =>
{
ServiceURL = configuration.Address,
ForcePathStyle = configuration.ForcePathStyle,
UseHttp = configuration.UseHttp,
};
return new AmazonS3Client(configuration.AccessKey, configuration.SecretAccessKey, s3Configuration);
});
var configuration = serviceProvider.GetRequiredService<InfrastructureStorageConfiguration>();
var s3Configuration = new AmazonS3Config
{
ServiceURL = configuration.Address,
ForcePathStyle = configuration.ForcePathStyle,
UseHttp = configuration.UseHttp,
};
return new AmazonS3Client(configuration.AccessKey, configuration.SecretAccessKey, s3Configuration);
});
services.AddSingleton<IStorageService, StorageService>();
return services;
}
services.AddSingleton<IStorageService, StorageService>();
return services;
}
public static IServiceCollection AddPictureService(this IServiceCollection services)
{
services.AddScoped<IPictureService, PictureService>();
return services;
}
public static IServiceCollection AddJobs(this IServiceCollection services)
{
services.AddHangfire((serviceProvider, hangfireConfiguration) =>
public static IServiceCollection AddPictureService(this IServiceCollection services)
{
var databaseConfiguration = serviceProvider.GetRequiredService<DatabaseConfiguration>();
hangfireConfiguration.UsePostgreSqlStorage(options =>
options.UseNpgsqlConnection(databaseConfiguration.ConnectionString));
});
services.AddScoped<IPictureService, PictureService>();
return services;
}
services.AddHangfireServer();
services.AddScoped<DailyMixGenerationJob>();
services.AddScoped<ListeningHistoryCleanupJob>();
services.AddScoped<UploadIntentExpirationJob>();
services.AddScoped<TempUploadsCleanupJob>();
services.AddHostedService<RecurringJobsRegistrar>();
public static IServiceCollection AddJobs(this IServiceCollection services)
{
services.AddHangfire((serviceProvider, hangfireConfiguration) =>
{
var databaseConfiguration = serviceProvider.GetRequiredService<DatabaseConfiguration>();
hangfireConfiguration.UsePostgreSqlStorage(options =>
options.UseNpgsqlConnection(databaseConfiguration.ConnectionString));
});
return services;
}
services.AddHangfireServer();
services.AddScoped<DailyMixGenerationJob>();
services.AddScoped<ListeningHistoryCleanupJob>();
services.AddScoped<UploadIntentExpirationJob>();
services.AddScoped<TempUploadsCleanupJob>();
services.AddHostedService<RecurringJobsRegistrar>();
public static IServiceCollection AddStreamTicketService(this IServiceCollection services, IConfiguration configuration)
{
services.AddValidatedOptions<StreamTicketConfiguration>(configuration);
return services;
}
services.AddSingleton<IStreamTicketService, StreamTicketService>();
return services;
}
public static IServiceCollection AddStreamTicketService(this IServiceCollection services, IConfiguration configuration)
{
services.AddValidatedOptions<StreamTicketConfiguration>(configuration);
public static IServiceCollection AddAudioTranscoder(this IServiceCollection services, IConfiguration configuration)
{
services.AddValidatedOptions<AudioConfiguration>(configuration);
services.AddSingleton<IStreamTicketService, StreamTicketService>();
return services;
}
services.AddScoped<IAudioTranscoderService, AudioTranscoderService>();
return services;
public static IServiceCollection AddAudioTranscoder(this IServiceCollection services, IConfiguration configuration)
{
services.AddValidatedOptions<AudioConfiguration>(configuration);
services.AddScoped<IAudioTranscoderService, AudioTranscoderService>();
return services;
}
}
}
@@ -8,153 +8,154 @@ using Musify.Application.Contracts;
using Musify.Application.Shared;
using Musify.Infrastructure.Configuration;
namespace Musify.Infrastructure.Services;
public sealed class StorageService(
IAmazonS3 amazonS3,
ILogger<StorageService> logger,
InfrastructureStorageConfiguration storageClientConfiguration)
: IStorageService
namespace Musify.Infrastructure.Services
{
public async Task<Stream?> GetFileAsync(string bucket, string key, CancellationToken cancellationToken)
public sealed class StorageService(
IAmazonS3 amazonS3,
ILogger<StorageService> logger,
InfrastructureStorageConfiguration storageClientConfiguration)
: IStorageService
{
try
public async Task<Stream?> GetFileAsync(string bucket, string key, CancellationToken cancellationToken)
{
var response = await amazonS3.GetObjectAsync(
new GetObjectRequest { BucketName = bucket, Key = key }, cancellationToken);
logger.LogDebug("Retrieved file from S3 {Bucket}/{Key}", bucket, key);
return response.ResponseStream;
try
{
var response = await amazonS3.GetObjectAsync(
new GetObjectRequest { BucketName = bucket, Key = key }, cancellationToken);
logger.LogDebug("Retrieved file from S3 {Bucket}/{Key}", bucket, key);
return response.ResponseStream;
}
catch (AmazonS3Exception exception) when (exception.ErrorCode == "NoSuchKey")
{
logger.LogDebug("File not found in S3 {Bucket}/{Key}", bucket, key);
return null;
}
}
catch (AmazonS3Exception exception) when (exception.ErrorCode == "NoSuchKey")
public async Task<string> GetUploadUrlAsync(
string bucket,
string key,
string contentType,
TimeSpan expirationTime,
bool preventOverwrite = true,
CancellationToken cancellationToken = default)
{
logger.LogDebug("File not found in S3 {Bucket}/{Key}", bucket, key);
return null;
var request = new GetPreSignedUrlRequest
{
BucketName = bucket,
Key = key,
Expires = DateTime.UtcNow + expirationTime,
Protocol = storageClientConfiguration.UseHttp ? Protocol.HTTP : Protocol.HTTPS,
Verb = HttpVerb.PUT,
ContentType = contentType
};
if (preventOverwrite)
request.Headers["If-None-Match"] = "*";
logger.LogDebug("Generating pre-signed upload URL for {Bucket}/{Key}", bucket, key);
return await amazonS3.GetPreSignedURLAsync(request);
}
}
public async Task<string> GetUploadUrlAsync(
string bucket,
string key,
string contentType,
TimeSpan expirationTime,
bool preventOverwrite = true,
CancellationToken cancellationToken = default)
{
var request = new GetPreSignedUrlRequest
public async Task<ObjectMetaData?> HeadObjectAsync(string bucket, string key, CancellationToken cancellationToken)
{
BucketName = bucket,
Key = key,
Expires = DateTime.UtcNow + expirationTime,
Protocol = storageClientConfiguration.UseHttp ? Protocol.HTTP : Protocol.HTTPS,
Verb = HttpVerb.PUT,
ContentType = contentType
};
if (preventOverwrite)
request.Headers["If-None-Match"] = "*";
logger.LogDebug("Generating pre-signed upload URL for {Bucket}/{Key}", bucket, key);
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);
try
{
var response = await amazonS3.GetObjectMetadataAsync(bucket, key, cancellationToken);
return new ObjectMetaData(response.Headers.ContentType, response.ContentLength);
}
catch (AmazonS3Exception exception) when (exception.StatusCode == System.Net.HttpStatusCode.NotFound)
{
return null;
}
}
catch (AmazonS3Exception exception) when (exception.StatusCode == System.Net.HttpStatusCode.NotFound)
public async Task RemoveFileAsync(string bucket, string key, CancellationToken cancellationToken)
{
return null;
logger.LogDebug("Removing file from S3 {Bucket}/{Key}", bucket, key);
await amazonS3.DeleteObjectAsync(new DeleteObjectRequest { BucketName = bucket, Key = key }, cancellationToken);
}
}
public async Task RemoveFileAsync(string bucket, string key, CancellationToken cancellationToken)
{
logger.LogDebug("Removing file from S3 {Bucket}/{Key}", bucket, key);
await amazonS3.DeleteObjectAsync(new DeleteObjectRequest { BucketName = bucket, Key = key }, cancellationToken);
}
public async Task<IReadOnlyList<string>> TransferFilesAsync(
string sourceDirectory, string bucket, string route, CancellationToken cancellationToken)
{
using var transferUtility = new TransferUtility(amazonS3);
var uploadedKeys = new List<string>();
foreach (var filePath in Directory.GetFiles(sourceDirectory, "*"))
public async Task<IReadOnlyList<string>> TransferFilesAsync(
string sourceDirectory, string bucket, string route, CancellationToken cancellationToken)
{
var key = StorageKey.Combine(route, Path.GetFileName(filePath));
using var transferUtility = new TransferUtility(amazonS3);
await transferUtility.UploadAsync(
new TransferUtilityUploadRequest
var uploadedKeys = new List<string>();
foreach (var filePath in Directory.GetFiles(sourceDirectory, "*"))
{
var key = StorageKey.Combine(route, Path.GetFileName(filePath));
await transferUtility.UploadAsync(
new TransferUtilityUploadRequest
{
FilePath = filePath,
BucketName = bucket,
Key = key,
ContentType = MimeUtility.GetMimeMapping(filePath)
},
cancellationToken);
uploadedKeys.Add(key);
logger.LogDebug("Transferred file to S3 {Bucket}/{Key}", bucket, key);
}
logger.LogInformation("Transferred {Count} files to {Bucket}/{Route}", uploadedKeys.Count, bucket, route);
return uploadedKeys;
}
public async Task UploadFileAsync(string filePath, string bucket, string key, CancellationToken cancellationToken)
{
await using var fileStream = File.OpenRead(filePath);
await UploadFileAsync(fileStream, MimeUtility.GetMimeMapping(filePath), bucket, key, cancellationToken);
}
public async Task UploadFileAsync(Stream sourceStream, string contentType, string bucket, string key, CancellationToken cancellationToken)
{
await amazonS3.PutObjectAsync(
new PutObjectRequest { BucketName = bucket, ContentType = contentType, InputStream = sourceStream, Key = key },
cancellationToken);
logger.LogDebug("Uploaded file to S3 {Bucket}/{Key}", bucket, key);
}
public async Task CopyFileAsync(
string sourceBucket, string sourceKey, string destinationBucket, string destinationKey, CancellationToken cancellationToken)
{
await amazonS3.CopyObjectAsync(
new CopyObjectRequest
{
FilePath = filePath,
BucketName = bucket,
Key = key,
ContentType = MimeUtility.GetMimeMapping(filePath)
SourceBucket = sourceBucket,
SourceKey = sourceKey,
DestinationBucket = destinationBucket,
DestinationKey = destinationKey
},
cancellationToken);
uploadedKeys.Add(key);
logger.LogDebug("Transferred file to S3 {Bucket}/{Key}", bucket, key);
logger.LogDebug("Copied file from {SourceBucket}/{SourceKey} to {DestinationBucket}/{DestinationKey}",
sourceBucket, sourceKey, destinationBucket, destinationKey);
}
logger.LogInformation("Transferred {Count} files to {Bucket}/{Route}", uploadedKeys.Count, bucket, route);
return uploadedKeys;
}
public async Task UploadFileAsync(string filePath, string bucket, string key, CancellationToken cancellationToken)
{
await using var fileStream = File.OpenRead(filePath);
await UploadFileAsync(fileStream, MimeUtility.GetMimeMapping(filePath), bucket, key, cancellationToken);
}
public async Task UploadFileAsync(Stream sourceStream, string contentType, string bucket, string key, CancellationToken cancellationToken)
{
await amazonS3.PutObjectAsync(
new PutObjectRequest { BucketName = bucket, ContentType = contentType, InputStream = sourceStream, Key = key },
cancellationToken);
logger.LogDebug("Uploaded file to S3 {Bucket}/{Key}", bucket, key);
}
public async Task CopyFileAsync(
string sourceBucket, string sourceKey, string destinationBucket, string destinationKey, CancellationToken cancellationToken)
{
await amazonS3.CopyObjectAsync(
new CopyObjectRequest
public async IAsyncEnumerable<StorageObject> ListObjectsAsync(
string bucket, string prefix, [EnumeratorCancellation] CancellationToken cancellationToken)
{
var listRequest = new ListObjectsV2Request
{
SourceBucket = sourceBucket,
SourceKey = sourceKey,
DestinationBucket = destinationBucket,
DestinationKey = destinationKey
},
cancellationToken);
BucketName = bucket,
Prefix = prefix.EndsWith('/') ? prefix : $"{prefix}/"
};
logger.LogDebug("Copied file from {SourceBucket}/{SourceKey} to {DestinationBucket}/{DestinationKey}",
sourceBucket, sourceKey, destinationBucket, destinationKey);
}
ListObjectsV2Response listResponse;
do
{
listResponse = await amazonS3.ListObjectsV2Async(listRequest, cancellationToken);
foreach (var s3Object in listResponse.S3Objects ?? [])
yield return new StorageObject(s3Object.Key, (s3Object.LastModified ?? DateTime.UtcNow).ToUniversalTime());
public async IAsyncEnumerable<StorageObject> ListObjectsAsync(
string bucket, string prefix, [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 s3Object in listResponse.S3Objects ?? [])
yield return new StorageObject(s3Object.Key, (s3Object.LastModified ?? DateTime.UtcNow).ToUniversalTime());
listRequest.ContinuationToken = listResponse.NextContinuationToken;
listRequest.ContinuationToken = listResponse.NextContinuationToken;
}
while (listResponse.IsTruncated == true);
}
while (listResponse.IsTruncated == true);
}
}
@@ -5,50 +5,51 @@ using Microsoft.IdentityModel.Tokens;
using Musify.Application.Contracts;
using Musify.Infrastructure.Configuration;
namespace Musify.Infrastructure.Services;
public sealed class StreamTicketService : IStreamTicketService, IDisposable
namespace Musify.Infrastructure.Services
{
private readonly StreamTicketConfiguration configuration;
private readonly RSA rsa;
private readonly SigningCredentials signingCredentials;
private readonly JsonWebTokenHandler tokenHandler = new();
public StreamTicketService(StreamTicketConfiguration configuration)
public sealed class StreamTicketService : IStreamTicketService, IDisposable
{
this.configuration = configuration;
private readonly StreamTicketConfiguration configuration;
private readonly RSA rsa;
private readonly SigningCredentials signingCredentials;
private readonly JsonWebTokenHandler tokenHandler = new();
rsa = RSA.Create();
rsa.ImportFromPem(File.ReadAllText(configuration.PrivateKeyPath));
var key = new RsaSecurityKey(rsa);
signingCredentials = new SigningCredentials(key, SecurityAlgorithms.RsaSha256);
}
public StreamTicket IssueTicket(long? userId, string keyPrefix, long? maxBytes = null)
{
var now = DateTimeOffset.UtcNow;
var claims = new Dictionary<string, object> { ["prefix"] = keyPrefix };
if (userId != null)
claims["sub"] = userId.Value.ToString(CultureInfo.InvariantCulture);
if (maxBytes != null)
claims["maxBytes"] = maxBytes.Value.ToString(CultureInfo.InvariantCulture);
var descriptor = new SecurityTokenDescriptor
public StreamTicketService(StreamTicketConfiguration configuration)
{
Issuer = configuration.Issuer,
Audience = configuration.Audience,
IssuedAt = now.UtcDateTime,
NotBefore = now.UtcDateTime,
Expires = now.AddSeconds(configuration.TicketTtlSeconds).UtcDateTime,
Claims = claims,
SigningCredentials = signingCredentials,
};
this.configuration = configuration;
var token = tokenHandler.CreateToken(descriptor);
return new StreamTicket(token, configuration.TicketTtlSeconds);
rsa = RSA.Create();
rsa.ImportFromPem(File.ReadAllText(configuration.PrivateKeyPath));
var key = new RsaSecurityKey(rsa);
signingCredentials = new SigningCredentials(key, SecurityAlgorithms.RsaSha256);
}
public StreamTicket IssueTicket(long? userId, string keyPrefix, long? maxBytes = null)
{
var now = DateTimeOffset.UtcNow;
var claims = new Dictionary<string, object> { ["prefix"] = keyPrefix };
if (userId != null)
claims["sub"] = userId.Value.ToString(CultureInfo.InvariantCulture);
if (maxBytes != null)
claims["maxBytes"] = maxBytes.Value.ToString(CultureInfo.InvariantCulture);
var descriptor = new SecurityTokenDescriptor
{
Issuer = configuration.Issuer,
Audience = configuration.Audience,
IssuedAt = now.UtcDateTime,
NotBefore = now.UtcDateTime,
Expires = now.AddSeconds(configuration.TicketTtlSeconds).UtcDateTime,
Claims = claims,
SigningCredentials = signingCredentials,
};
var token = tokenHandler.CreateToken(descriptor);
return new StreamTicket(token, configuration.TicketTtlSeconds);
}
public void Dispose() => rsa.Dispose();
}
public void Dispose() => rsa.Dispose();
}