Reorganize backend to mirror Musify.slnx folder structure
Moves the six backend projects on disk into Core/ and Hosts/ subfolders, matching the solution folders already defined in Musify.slnx, so the file explorer reflects the same layout as the solution: backend/Core/Musify.Domain backend/Core/Musify.Application backend/Core/Musify.Infrastructure backend/Hosts/Musify.Api backend/Hosts/Musify.Worker backend/Hosts/Musify.StreamingGateway Updates every reference to the old paths: ProjectReference entries in Musify.Api.csproj and Musify.Worker.csproj, the three Dockerfiles, deploy/scripts/migrate.ps1|sh, deploy/zitadel/provision.ps1, deploy/up.ps1, deploy/README.md and docs/development.md. Also fixes a stale Dockerfile comment pointing at a compose file that no longer exists. Verified with `dotnet build Musify.slnx` against the new layout.
This commit is contained in:
@@ -0,0 +1,248 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Musify.Application.Contracts;
|
||||
using Musify.Infrastructure.Configuration;
|
||||
using System.Diagnostics;
|
||||
using System.Globalization;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Musify.Infrastructure.Services
|
||||
{
|
||||
public partial class AudioService(ILogger<AudioService> logger,
|
||||
AudioConfiguration audioConfiguration)
|
||||
: IAudioTranscoderService
|
||||
{
|
||||
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)
|
||||
{
|
||||
try
|
||||
{
|
||||
Directory.CreateDirectory(destinationPath);
|
||||
|
||||
logger.LogInformation("Starting audio transcoding for {DestinationPath}", destinationPath);
|
||||
|
||||
var executionResult = await ExecuteFfmpegWithInputAsync(
|
||||
BuildAudioFileArguments(destinationPath, audioConfiguration.Ffmpeg),
|
||||
destinationPath,
|
||||
audioStream,
|
||||
"TranscodeToAudioFile",
|
||||
cancellationToken);
|
||||
|
||||
if (executionResult.ExitCode != 0)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
logger.LogInformation("Audio transcoding completed for {DestinationPath}", destinationPath);
|
||||
|
||||
var outputPath = Path.Combine(destinationPath, audioConfiguration.Ffmpeg.OutputFileName);
|
||||
var duration = await GetAudioDurationAsync(outputPath, cancellationToken);
|
||||
return new AudioTranscodeResult(executionResult.ExitCode, duration);
|
||||
}
|
||||
catch (TimeoutException timeoutException)
|
||||
{
|
||||
logger.LogError(timeoutException, "Audio transcoding timed out after {Timeout} for {DestinationPath}", audioConfiguration.TranscodingTimeout, destinationPath);
|
||||
throw;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to transcode audio for {DestinationPath}", destinationPath);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
async Task<TimeSpan> GetAudioDurationAsync(string filePath, CancellationToken cancellationToken)
|
||||
{
|
||||
var executionResult = await ExecuteFfmpegAsync(
|
||||
BuildProbeDurationArguments(filePath),
|
||||
Path.GetDirectoryName(filePath) ?? Environment.CurrentDirectory,
|
||||
"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);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
async Task<FfmpegExecutionResult> ExecuteFfmpegWithInputAsync(
|
||||
string arguments,
|
||||
string workingDirectory,
|
||||
Stream inputStream,
|
||||
string operationName,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
using var process = BuildProcess(arguments, workingDirectory, redirectStandardInput: true);
|
||||
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 (inputStream.CanSeek)
|
||||
inputStream.Position = 0;
|
||||
|
||||
await inputStream.CopyToAsync(process.StandardInput.BaseStream, cancellationToken);
|
||||
process.StandardInput.Close();
|
||||
|
||||
return await WaitForProcessResultAsync(process, outputTask, errorTask, operationName, cancellationToken);
|
||||
}
|
||||
|
||||
async Task<FfmpegExecutionResult> ExecuteFfmpegAsync(
|
||||
string arguments,
|
||||
string workingDirectory,
|
||||
string operationName,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
using var process = BuildProcess(arguments, workingDirectory, redirectStandardInput: false);
|
||||
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);
|
||||
|
||||
return await WaitForProcessResultAsync(process, outputTask, errorTask, operationName, cancellationToken);
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
};
|
||||
|
||||
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)
|
||||
{
|
||||
logger.LogError("[{Operation}] ffmpeg timed out after {Timeout} (PID: {ProcessId} )",
|
||||
operationName, audioConfiguration.TranscodingTimeout, process.Id);
|
||||
|
||||
TryKillProcess(process);
|
||||
throw new TimeoutException($"ffmpeg execution exceeded timeout {audioConfiguration.TranscodingTimeout}.");
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
void TryKillProcess(Process process)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!process.HasExited)
|
||||
process.Kill();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to kill ffmpeg process (PID: {ProcessId} )", process.Id);
|
||||
}
|
||||
}
|
||||
|
||||
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}\"";
|
||||
}
|
||||
|
||||
static string BuildValidateAudioArguments(string filePath)
|
||||
=> $"-v error -i \"{filePath}\" -map 0:a:0 -f null -";
|
||||
|
||||
static string BuildProbeDurationArguments(string filePath)
|
||||
=> $"-hide_banner -i \"{filePath}\" -f null -";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Musify.Application.Contracts;
|
||||
using SixLabors.ImageSharp;
|
||||
using SixLabors.ImageSharp.Processing;
|
||||
|
||||
namespace Musify.Infrastructure.Services
|
||||
{
|
||||
public class PictureService(ILogger<PictureService> logger)
|
||||
: IPictureService
|
||||
{
|
||||
public async Task<Stream> ResizePictureAsWebpAsync(Stream pictureStream, int width, int height, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
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
|
||||
{
|
||||
Size = new Size(width, height),
|
||||
Mode = ResizeMode.Max
|
||||
} ));
|
||||
|
||||
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;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to resize picture to {Width}x{Height}", width, height);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
using System.Reflection;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Musify.Application.Configuration;
|
||||
using Musify.Application.Contracts;
|
||||
|
||||
namespace Musify.Infrastructure.Services
|
||||
{
|
||||
public class PlayListPresetSeeder(IServiceScopeFactory scopeFactory, ILogger<PlayListPresetSeeder> logger)
|
||||
: BackgroundService
|
||||
{
|
||||
private const string SourceResourceName = "Musify.Infrastructure.Assets.PlayListDefaultCover.png";
|
||||
private const string WebpContentType = "image/webp";
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
using var scope = scopeFactory.CreateScope();
|
||||
var services = scope.ServiceProvider;
|
||||
|
||||
var playListConfiguration = services.GetRequiredService<PlayListConfiguration>();
|
||||
if (!playListConfiguration.SeedPresetPictures)
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
await SeedAsync(services, playListConfiguration, stoppingToken);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to seed playlist preset pictures");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task SeedAsync(IServiceProvider services, PlayListConfiguration configuration, CancellationToken cancellationToken)
|
||||
{
|
||||
var storage = services.GetRequiredService<IStorageService>();
|
||||
var pictures = services.GetRequiredService<IPictureService>();
|
||||
var bucket = services.GetRequiredService<ApplicationStorageConfiguration>().Bucket;
|
||||
|
||||
var source = LoadSourceBytes();
|
||||
|
||||
var routes = configuration.Routes;
|
||||
var sizes = configuration.PicturesSizes;
|
||||
|
||||
var targets = new (string Key, int Width, int Height)[]
|
||||
{
|
||||
(routes.BuildOriginalPicturePath(routes.PresetOriginalPicture), sizes.LargePictureWidth, sizes.LargePictureHeight),
|
||||
(routes.PresetSmallPicturePath, sizes.SmallPictureWidth, sizes.SmallPictureHeight),
|
||||
(routes.PresetMediumPicturePath, sizes.MediumPictureWidth, sizes.MediumPictureHeight),
|
||||
(routes.PresetLargePicturePath, sizes.LargePictureWidth, sizes.LargePictureHeight),
|
||||
};
|
||||
|
||||
foreach (var target in targets)
|
||||
{
|
||||
if (!configuration.OverwritePresetPictures)
|
||||
{
|
||||
var existing = await storage.HeadObjectAsync(bucket, target.Key, cancellationToken);
|
||||
if (existing is not null)
|
||||
{
|
||||
logger.LogDebug("Playlist preset picture already present at {Bucket}/{Key}", bucket, target.Key);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
using var sourceStream = new MemoryStream(source, writable: false);
|
||||
await using var resized = await pictures.ResizePictureAsWebpAsync(sourceStream, target.Width, target.Height, cancellationToken);
|
||||
await storage.UploadFileAsync(resized, WebpContentType, bucket, target.Key, cancellationToken);
|
||||
|
||||
logger.LogInformation("Seeded playlist preset picture {Bucket}/{Key} ({Width}x{Height})", bucket, target.Key, target.Width, target.Height);
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] LoadSourceBytes()
|
||||
{
|
||||
var assembly = Assembly.GetExecutingAssembly();
|
||||
using var stream = assembly.GetManifestResourceStream(SourceResourceName)
|
||||
?? throw new InvalidOperationException($"Embedded resource '{SourceResourceName}' was not found.");
|
||||
|
||||
using var memory = new MemoryStream();
|
||||
stream.CopyTo(memory);
|
||||
return memory.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
using Amazon.S3;
|
||||
using Hangfire;
|
||||
using Hangfire.PostgreSql;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Musify.Application.Configuration;
|
||||
using Musify.Infrastructure.Configuration;
|
||||
using Musify.Infrastructure.Jobs;
|
||||
using Musify.Application.Contracts;
|
||||
|
||||
namespace Musify.Infrastructure.Services
|
||||
{
|
||||
public static class ServicesDependencyInjection
|
||||
{
|
||||
public static IServiceCollection AddStorageService(this IServiceCollection serviceDescriptors, IConfiguration configuration)
|
||||
{
|
||||
serviceDescriptors
|
||||
.AddOptionsWithValidateOnStart<InfrastructureStorageConfiguration>()
|
||||
.Bind(configuration.GetRequiredSection(InfrastructureStorageConfiguration.SectionName))
|
||||
.ValidateDataAnnotations();
|
||||
|
||||
serviceDescriptors.AddSingleton(serviceProvider =>
|
||||
serviceProvider.GetRequiredService<IOptions<InfrastructureStorageConfiguration>>().Value);
|
||||
|
||||
serviceDescriptors.AddSingleton<IAmazonS3>(serviceProvider =>
|
||||
{
|
||||
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);
|
||||
} );
|
||||
|
||||
serviceDescriptors.AddScoped<IStorageService, StorageService>();
|
||||
return serviceDescriptors;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddPictureService(this IServiceCollection serviceDescriptors, IConfiguration configuration)
|
||||
{
|
||||
serviceDescriptors.AddScoped<IPictureService, PictureService>();
|
||||
return serviceDescriptors;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddPlayListPresetSeeder(this IServiceCollection serviceDescriptors, IConfiguration configuration)
|
||||
{
|
||||
serviceDescriptors.AddHostedService<PlayListPresetSeeder>();
|
||||
return serviceDescriptors;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddUploadIntentConfiguration(this IServiceCollection serviceDescriptors, IConfiguration configuration)
|
||||
{
|
||||
serviceDescriptors.AddValidatedOptions<UploadIntentConfiguration>(configuration, UploadIntentConfiguration.SectionName);
|
||||
|
||||
return serviceDescriptors;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddUploadIntentJobs(this IServiceCollection serviceDescriptors, IConfiguration configuration)
|
||||
{
|
||||
serviceDescriptors.AddUploadIntentConfiguration(configuration);
|
||||
|
||||
serviceDescriptors.AddHostedService<UploadIntentExpirationJob>();
|
||||
serviceDescriptors.AddHostedService<TemporalUploadsCleanUpJob>();
|
||||
|
||||
return serviceDescriptors;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddDailyMixGenerationJob(this IServiceCollection serviceDescriptors, IConfiguration configuration)
|
||||
{
|
||||
serviceDescriptors.AddHangfire((serviceProvider, hangfireConfiguration) =>
|
||||
{
|
||||
var databaseConfiguration = serviceProvider.GetRequiredService<DatabaseConfiguration>();
|
||||
hangfireConfiguration.UsePostgreSqlStorage(options =>
|
||||
options.UseNpgsqlConnection(databaseConfiguration.ConnectionString));
|
||||
});
|
||||
|
||||
serviceDescriptors.AddHangfireServer();
|
||||
serviceDescriptors.AddScoped<DailyMixGenerationJob>();
|
||||
|
||||
return serviceDescriptors;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddStreamTicketService(this IServiceCollection serviceDescriptors, IConfiguration configuration)
|
||||
{
|
||||
serviceDescriptors
|
||||
.AddOptionsWithValidateOnStart<StreamTicketConfiguration>()
|
||||
.Bind(configuration.GetRequiredSection(StreamTicketConfiguration.SectionName))
|
||||
.ValidateDataAnnotations();
|
||||
|
||||
serviceDescriptors.AddSingleton(serviceProvider =>
|
||||
serviceProvider.GetRequiredService<IOptions<StreamTicketConfiguration>>().Value);
|
||||
|
||||
serviceDescriptors.AddSingleton<IStreamTicketService, StreamTicketService>();
|
||||
return serviceDescriptors;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddYouTubeMusicService(this IServiceCollection serviceDescriptors, IConfiguration configuration)
|
||||
{
|
||||
serviceDescriptors.AddValidatedOptions<YouTubeConfiguration>(configuration, YouTubeConfiguration.SectionName);
|
||||
|
||||
serviceDescriptors.AddMemoryCache();
|
||||
serviceDescriptors.AddSingleton<IYouTubeMusicService, YouTubeMusicService>();
|
||||
return serviceDescriptors;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddYouTubeDownloader(this IServiceCollection serviceDescriptors, IConfiguration configuration)
|
||||
{
|
||||
serviceDescriptors.AddValidatedOptions<YtDlpConfiguration>(configuration, YtDlpConfiguration.SectionName);
|
||||
|
||||
serviceDescriptors.AddScoped<IYouTubeDownloaderService, YouTubeDownloaderService>();
|
||||
return serviceDescriptors;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddAudioTranscoder(this IServiceCollection serviceDescriptors, IConfiguration configuration)
|
||||
{
|
||||
serviceDescriptors
|
||||
.AddOptionsWithValidateOnStart<AudioConfiguration>()
|
||||
.Bind(configuration.GetRequiredSection(AudioConfiguration.SectionName))
|
||||
.ValidateDataAnnotations();
|
||||
|
||||
serviceDescriptors.AddSingleton(serviceProvider =>
|
||||
serviceProvider.GetRequiredService<IOptions<AudioConfiguration>>().Value);
|
||||
|
||||
serviceDescriptors.AddScoped<IAudioTranscoderService, AudioService>();
|
||||
return serviceDescriptors;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
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 is not 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 _);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,303 @@
|
||||
using Amazon.S3;
|
||||
using Amazon.S3.Model;
|
||||
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,
|
||||
InfrastructureStorageConfiguration storageClientConfiguration)
|
||||
: IStorageService
|
||||
{
|
||||
public async Task<Stream> GetFileAsync(string bucket, string key, CancellationToken cancellationToken)
|
||||
{
|
||||
var request = new GetObjectRequest
|
||||
{
|
||||
BucketName = bucket,
|
||||
Key = key,
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
var response = await amazonS3.GetObjectAsync(request, cancellationToken);
|
||||
logger.LogDebug("Retrieved file from S3 {Bucket}/{Key}", bucket, key);
|
||||
return response.ResponseStream;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to get file from S3 {Bucket}/{Key}", bucket, key);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
string contentType,
|
||||
TimeSpan expirationTime,
|
||||
CancellationToken cancellationToken,
|
||||
bool preventOverwrite = true)
|
||||
{
|
||||
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<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
|
||||
{
|
||||
BucketName = bucket,
|
||||
Key = key,
|
||||
};
|
||||
|
||||
logger.LogDebug("Removing file from S3 {Bucket}/{Key}", bucket, key);
|
||||
await amazonS3.DeleteObjectAsync(request, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<string>> TransferFilesAsync(string sourceDirectory, string bucket, string route, CancellationToken cancellationToken)
|
||||
{
|
||||
var trasnsferUtility = new TransferUtility(amazonS3);
|
||||
|
||||
var files = Directory.GetFiles(sourceDirectory, "*");
|
||||
|
||||
var uploadedKeys = new List<string>();
|
||||
var successCount = 0;
|
||||
var failedCount = 0;
|
||||
|
||||
foreach (var file in files)
|
||||
{
|
||||
string fileName = Path.GetFileName(file);
|
||||
string filePath = Path.Combine(sourceDirectory, file);
|
||||
|
||||
var key = string.Join('/', new[] { route, fileName }
|
||||
.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(
|
||||
new TransferUtilityUploadRequest
|
||||
{
|
||||
FilePath = filePath,
|
||||
BucketName = bucket,
|
||||
Key = key,
|
||||
ContentType = contentType
|
||||
},
|
||||
cancellationToken);
|
||||
upload.State = UploadState.Successful;
|
||||
uploadedKeys.Add(key);
|
||||
successCount++;
|
||||
logger.LogDebug("Transferred file to S3 {Bucket}/{Key}", bucket, key);
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
public async Task UploadFileAsync(string filePath, string bucket, string key, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var fileStream = File.OpenRead(filePath);
|
||||
var contentType = MimeUtility.GetMimeMapping(filePath);
|
||||
await UploadFileAsync(fileStream, contentType, bucket, key, cancellationToken);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to upload file to S3 {Bucket}/{Key}", bucket, key);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UploadFileAsync(Stream sourceStream, string contentType, string bucket, string key, CancellationToken cancellationToken)
|
||||
{
|
||||
var request = new PutObjectRequest
|
||||
{
|
||||
BucketName = bucket,
|
||||
ContentType = contentType,
|
||||
InputStream = sourceStream,
|
||||
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);
|
||||
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,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var request = new CopyObjectRequest
|
||||
{
|
||||
SourceBucket = sourceBucket,
|
||||
SourceKey = sourceKey,
|
||||
DestinationBucket = destinationBucket,
|
||||
DestinationKey = destinationKey
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
var response = await amazonS3.CopyObjectAsync(request, cancellationToken);
|
||||
logger.LogInformation("Copied file from {SourceBucket}/{SourceKey} to {DestinationBucket}/{DestinationKey}",
|
||||
sourceBucket, sourceKey, destinationBucket, destinationKey);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to copy file from {SourceBucket}/{SourceKey} to {DestinationBucket}/{DestinationKey}",
|
||||
sourceBucket, sourceKey, destinationBucket, destinationKey);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async IAsyncEnumerable<StorageObject> 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 s3Object in listResponse.S3Objects ?? [] )
|
||||
yield return new StorageObject(s3Object.Key, (s3Object.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}/";
|
||||
var listRequest = new ListObjectsV2Request
|
||||
{
|
||||
BucketName = bucket,
|
||||
Prefix = prefix
|
||||
};
|
||||
|
||||
ListObjectsV2Response listResponse;
|
||||
do
|
||||
{
|
||||
listResponse = await amazonS3.ListObjectsV2Async(listRequest, cancellationToken);
|
||||
|
||||
if (listResponse.S3Objects is null or { 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);
|
||||
|
||||
if (deleteResponse.DeleteErrors.Count > 0)
|
||||
{
|
||||
var errors = string.Join(", ", deleteResponse.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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
using System.Security.Cryptography;
|
||||
using Microsoft.IdentityModel.JsonWebTokens;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using Musify.Application.Contracts;
|
||||
using Musify.Infrastructure.Configuration;
|
||||
|
||||
namespace Musify.Infrastructure.Services
|
||||
{
|
||||
public sealed class StreamTicketService : IStreamTicketService, IDisposable
|
||||
{
|
||||
private readonly StreamTicketConfiguration configuration;
|
||||
private readonly RSA rsa;
|
||||
private readonly SigningCredentials signingCredentials;
|
||||
private readonly JsonWebTokenHandler tokenHandler = new();
|
||||
|
||||
public StreamTicketService(StreamTicketConfiguration configuration)
|
||||
{
|
||||
this.configuration = configuration;
|
||||
|
||||
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)
|
||||
{
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
|
||||
var descriptor = new SecurityTokenDescriptor
|
||||
{
|
||||
Issuer = configuration.Issuer,
|
||||
Audience = configuration.Audience,
|
||||
IssuedAt = now.UtcDateTime,
|
||||
NotBefore = now.UtcDateTime,
|
||||
Expires = now.AddSeconds(configuration.TicketTtlSeconds).UtcDateTime,
|
||||
Claims = new Dictionary<string, object>
|
||||
{
|
||||
["sub"] = userId.ToString(),
|
||||
["prefix"] = keyPrefix,
|
||||
},
|
||||
SigningCredentials = signingCredentials,
|
||||
};
|
||||
|
||||
var token = tokenHandler.CreateToken(descriptor);
|
||||
return new StreamTicket(token, configuration.TicketTtlSeconds);
|
||||
}
|
||||
|
||||
public void Dispose() => rsa.Dispose();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Musify.Application.Contracts;
|
||||
using Musify.Infrastructure.Configuration;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Musify.Infrastructure.Services
|
||||
{
|
||||
public class YouTubeDownloaderService(
|
||||
ILogger<YouTubeDownloaderService> logger,
|
||||
YtDlpConfiguration configuration)
|
||||
: IYouTubeDownloaderService
|
||||
{
|
||||
public async Task<YouTubeDownloadResult> DownloadAudioAsync(string videoId, string destinationFilePath, CancellationToken cancellationToken)
|
||||
{
|
||||
var destinationDirectory = Path.GetDirectoryName(destinationFilePath);
|
||||
if (!string.IsNullOrWhiteSpace(destinationDirectory))
|
||||
Directory.CreateDirectory(destinationDirectory);
|
||||
|
||||
var additionalArguments = string.IsNullOrWhiteSpace(configuration.AdditionalArguments)
|
||||
? string.Empty
|
||||
: $" {configuration.AdditionalArguments.Trim()}";
|
||||
|
||||
var arguments =
|
||||
$"-f {configuration.Format} " +
|
||||
"--no-playlist " +
|
||||
$"-o \"{destinationFilePath}\"" +
|
||||
additionalArguments +
|
||||
$" -- {videoId}";
|
||||
|
||||
using var process = new Process
|
||||
{
|
||||
StartInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = configuration.ExecutableName,
|
||||
WorkingDirectory = destinationDirectory ?? Environment.CurrentDirectory,
|
||||
Arguments = arguments,
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true,
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true,
|
||||
}
|
||||
};
|
||||
|
||||
process.Start();
|
||||
|
||||
logger.LogInformation("yt-dlp started (PID: {ProcessId} ) for video {VideoId}", process.Id, videoId);
|
||||
|
||||
var outputTask = process.StandardOutput.ReadToEndAsync(cancellationToken);
|
||||
var errorTask = process.StandardError.ReadToEndAsync(cancellationToken);
|
||||
|
||||
using var linkedCancellationTokens = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
||||
linkedCancellationTokens.CancelAfter(configuration.DownloadTimeout);
|
||||
|
||||
try
|
||||
{
|
||||
await process.WaitForExitAsync(linkedCancellationTokens.Token);
|
||||
}
|
||||
catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
logger.LogError("yt-dlp timed out after {Timeout} for video {VideoId} (PID: {ProcessId} )",
|
||||
configuration.DownloadTimeout, videoId, process.Id);
|
||||
|
||||
TryKillProcess(process);
|
||||
throw new TimeoutException($"yt-dlp execution exceeded timeout {configuration.DownloadTimeout}.");
|
||||
}
|
||||
|
||||
var standardError = await errorTask;
|
||||
var standardOutput = await outputTask;
|
||||
|
||||
logger.LogDebug("yt-dlp finished (PID: {ProcessId} ). Exit code: {ExitCode}, Output: {OutputLength}B, Error: {ErrorLength}B",
|
||||
process.Id, process.ExitCode, standardOutput.Length, standardError.Length);
|
||||
|
||||
return new YouTubeDownloadResult(process.ExitCode, standardError);
|
||||
}
|
||||
|
||||
void TryKillProcess(Process process)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!process.HasExited)
|
||||
process.Kill();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to kill yt-dlp process (PID: {ProcessId} )", process.Id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,311 @@
|
||||
using ErrorOr;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Musify.Application.Contracts;
|
||||
using Musify.Infrastructure.Configuration;
|
||||
using Musify.Infrastructure.Helpers;
|
||||
using YouTubeMusicAPI.Client;
|
||||
using YouTubeMusicAPI.Models.Search;
|
||||
using YouTubeMusicAPI.Models.Streaming;
|
||||
using YouTubeMusicAPI.Pagination;
|
||||
|
||||
namespace Musify.Infrastructure.Services
|
||||
{
|
||||
public class YouTubeMusicService(YouTubeConfiguration configuration, IMemoryCache cache, ILogger<YouTubeMusicService> logger) : IYouTubeMusicService
|
||||
{
|
||||
private readonly YouTubeMusicClient client = new(geographicalLocation: configuration.GeographicalLocation);
|
||||
private readonly SingleFlightCache singleFlight = new(cache);
|
||||
|
||||
private sealed record CachedStreamInfo(string Url, DateTime ExpiresAtUtc);
|
||||
|
||||
public async Task<ErrorOr<YouTubeSearchResult>> SearchSongsAsync(string query, string continuationToken, CancellationToken cancellationToken)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(continuationToken))
|
||||
return await FetchSearchPageAsync(query, continuationToken, cancellationToken);
|
||||
|
||||
return await singleFlight.GetOrCreateAsync(
|
||||
SearchResultsCacheKey(query),
|
||||
TimeSpan.FromSeconds(configuration.SearchResultsCacheSeconds),
|
||||
() => FetchSearchPageAsync(query, continuationToken, cancellationToken));
|
||||
}
|
||||
|
||||
private sealed record SearchPage(IReadOnlyList<SearchResult> Results, string NextToken);
|
||||
|
||||
private async Task<ErrorOr<SearchPage>> FetchPageAsync(
|
||||
string query,
|
||||
SearchCategory category,
|
||||
string continuationToken,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
PaginatedAsyncEnumerable<SearchResult>? paginator;
|
||||
if (string.IsNullOrEmpty(continuationToken))
|
||||
{
|
||||
paginator = client.SearchAsync(query, category);
|
||||
}
|
||||
else if (!cache.TryGetValue(SearchCacheKey(continuationToken), out paginator) || paginator is null)
|
||||
{
|
||||
return Error.NotFound(description: "The search continuation has expired.");
|
||||
}
|
||||
|
||||
IReadOnlyList<SearchResult> results;
|
||||
try
|
||||
{
|
||||
results = await paginator.FetchNextPageAsync(cancellationToken);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "YouTube Music {Category} search failed for query '{Query}'", category, query);
|
||||
return Error.Failure(description: $"YouTube Music search failed: {exception.Message}");
|
||||
}
|
||||
|
||||
var nextToken = string.Empty;
|
||||
if (paginator.HasMore)
|
||||
{
|
||||
nextToken = Guid.NewGuid().ToString("N");
|
||||
cache.Set(
|
||||
SearchCacheKey(nextToken),
|
||||
paginator,
|
||||
TimeSpan.FromSeconds(configuration.SearchContinuationCacheSeconds));
|
||||
}
|
||||
|
||||
return new SearchPage(results, nextToken);
|
||||
}
|
||||
|
||||
private async Task<ErrorOr<YouTubeSearchResult>> FetchSearchPageAsync(string query, string continuationToken, CancellationToken cancellationToken)
|
||||
{
|
||||
var page = await FetchPageAsync(query, SearchCategory.Songs, continuationToken, cancellationToken);
|
||||
if (page.IsError)
|
||||
return page.Errors;
|
||||
|
||||
var items = page.Value.Results
|
||||
.OfType<SongSearchResult>()
|
||||
.Select(song => new YouTubeSongResult(
|
||||
song.Id,
|
||||
song.Name,
|
||||
string.Join(", ", song.Artists.Select(artist => artist.Name)),
|
||||
song.Album?.Name ?? string.Empty,
|
||||
(int)song.Duration.TotalSeconds,
|
||||
PickThumbnail(song.Thumbnails),
|
||||
song.IsExplicit,
|
||||
song.Artists
|
||||
.Select(artist => new YouTubeArtistRef(
|
||||
string.IsNullOrEmpty(artist.Id) ? null : artist.Id,
|
||||
artist.Name))
|
||||
.ToList()))
|
||||
.ToList();
|
||||
|
||||
return new YouTubeSearchResult(items, page.Value.NextToken);
|
||||
}
|
||||
|
||||
public async Task<ErrorOr<YouTubeAlbumSearchResult>> SearchAlbumsAsync(string query, string continuationToken, CancellationToken cancellationToken)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(continuationToken))
|
||||
return await FetchAlbumSearchPageAsync(query, continuationToken, cancellationToken);
|
||||
|
||||
return await singleFlight.GetOrCreateAsync(
|
||||
AlbumSearchResultsCacheKey(query),
|
||||
TimeSpan.FromSeconds(configuration.SearchResultsCacheSeconds),
|
||||
() => FetchAlbumSearchPageAsync(query, continuationToken, cancellationToken));
|
||||
}
|
||||
|
||||
private async Task<ErrorOr<YouTubeAlbumSearchResult>> FetchAlbumSearchPageAsync(string query, string continuationToken, CancellationToken cancellationToken)
|
||||
{
|
||||
var page = await FetchPageAsync(query, SearchCategory.Albums, continuationToken, cancellationToken);
|
||||
if (page.IsError)
|
||||
return page.Errors;
|
||||
|
||||
var items = page.Value.Results
|
||||
.OfType<AlbumSearchResult>()
|
||||
.Select(ToAlbumResult)
|
||||
.ToList();
|
||||
|
||||
return new YouTubeAlbumSearchResult(items, page.Value.NextToken);
|
||||
}
|
||||
|
||||
public async Task<ErrorOr<YouTubeAlbumDetail>> GetAlbumAsync(string albumId, CancellationToken cancellationToken)
|
||||
{
|
||||
return await singleFlight.GetOrCreateAsync(
|
||||
AlbumCacheKey(albumId),
|
||||
TimeSpan.FromSeconds(configuration.SongInfoCacheSeconds),
|
||||
() => FetchAlbumAsync(albumId, cancellationToken));
|
||||
}
|
||||
|
||||
private async Task<ErrorOr<YouTubeAlbumDetail>> FetchAlbumAsync(string albumId, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
var browseId = await client.GetAlbumBrowseIdAsync(albumId, cancellationToken);
|
||||
var info = await client.GetAlbumInfoAsync(browseId, cancellationToken);
|
||||
|
||||
var album = new YouTubeAlbumResult(
|
||||
albumId,
|
||||
info.Name,
|
||||
string.Join(", ", info.Artists.Select(artist => artist.Name)),
|
||||
PickThumbnail(info.Thumbnails),
|
||||
info.ReleaseYear,
|
||||
info.IsSingle,
|
||||
info.IsEp,
|
||||
info.Artists
|
||||
.Select(artist => new YouTubeArtistRef(
|
||||
string.IsNullOrEmpty(artist.Id) ? null : artist.Id,
|
||||
artist.Name))
|
||||
.ToList());
|
||||
|
||||
var tracks = info.Songs
|
||||
.Select((song, index) => new YouTubeAlbumTrack(
|
||||
song.Id ?? string.Empty,
|
||||
song.Name,
|
||||
(int)song.Duration.TotalSeconds,
|
||||
song.SongNumber ?? index + 1,
|
||||
song.IsExplicit))
|
||||
.Where(track => !string.IsNullOrEmpty(track.VideoId))
|
||||
.ToList();
|
||||
|
||||
return new YouTubeAlbumDetail(
|
||||
album,
|
||||
StripWikipediaAttribution(info.Description),
|
||||
(int)info.Duration.TotalSeconds,
|
||||
tracks);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to load YouTube album '{AlbumId}'", albumId);
|
||||
return Error.Failure(description: $"Failed to load YouTube album '{albumId}': {exception.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private static string StripWikipediaAttribution(string? description)
|
||||
{
|
||||
if (string.IsNullOrEmpty(description))
|
||||
return string.Empty;
|
||||
|
||||
var markerIndex = description.IndexOf("From Wikipedia", StringComparison.OrdinalIgnoreCase);
|
||||
var cleaned = markerIndex >= 0 ? description[..markerIndex] : description;
|
||||
return cleaned.TrimEnd();
|
||||
}
|
||||
|
||||
private YouTubeAlbumResult ToAlbumResult(AlbumSearchResult album) =>
|
||||
new(
|
||||
album.Id,
|
||||
album.Name,
|
||||
string.Join(", ", album.Artists.Select(artist => artist.Name)),
|
||||
PickThumbnail(album.Thumbnails),
|
||||
album.ReleaseYear,
|
||||
album.IsSingle,
|
||||
album.IsEp,
|
||||
album.Artists
|
||||
.Select(artist => new YouTubeArtistRef(
|
||||
string.IsNullOrEmpty(artist.Id) ? null : artist.Id,
|
||||
artist.Name))
|
||||
.ToList());
|
||||
|
||||
public async Task<ErrorOr<YouTubeStreamInfo>> GetAudioStreamAsync(string videoId, CancellationToken cancellationToken)
|
||||
{
|
||||
var now = DateTime.UtcNow;
|
||||
if (cache.TryGetValue(StreamCacheKey(videoId), out CachedStreamInfo? cached)
|
||||
&& cached is not null
|
||||
&& cached.ExpiresAtUtc > now)
|
||||
{
|
||||
return new YouTubeStreamInfo(cached.Url, (int)(cached.ExpiresAtUtc - now).TotalSeconds);
|
||||
}
|
||||
|
||||
StreamingData streamingData;
|
||||
try
|
||||
{
|
||||
streamingData = await client.GetStreamingDataAsync(videoId, cancellationToken);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to resolve YouTube stream for '{VideoId}'", videoId);
|
||||
return Error.Failure(description: $"Failed to resolve YouTube stream for '{videoId}': {exception.Message}");
|
||||
}
|
||||
|
||||
var audioStream = streamingData.StreamInfo
|
||||
.OfType<AudioStreamInfo>()
|
||||
.OrderByDescending(stream => stream.Bitrate)
|
||||
.FirstOrDefault();
|
||||
|
||||
if (audioStream is null || string.IsNullOrEmpty(audioStream.Url))
|
||||
return Error.NotFound(description: $"No audio stream available for '{videoId}'.");
|
||||
|
||||
var expiresInSeconds = Math.Min(
|
||||
configuration.StreamUrlCacheSeconds,
|
||||
(int)streamingData.ExpiresIn.TotalSeconds);
|
||||
|
||||
if (expiresInSeconds > 0)
|
||||
{
|
||||
cache.Set(
|
||||
StreamCacheKey(videoId),
|
||||
new CachedStreamInfo(audioStream.Url, now.AddSeconds(expiresInSeconds)),
|
||||
TimeSpan.FromSeconds(expiresInSeconds));
|
||||
}
|
||||
|
||||
return new YouTubeStreamInfo(audioStream.Url, expiresInSeconds);
|
||||
}
|
||||
|
||||
public async Task<ErrorOr<YouTubeSongResult>> GetSongAsync(string videoId, CancellationToken cancellationToken)
|
||||
{
|
||||
return await singleFlight.GetOrCreateAsync(
|
||||
SongInfoCacheKey(videoId),
|
||||
TimeSpan.FromSeconds(configuration.SongInfoCacheSeconds),
|
||||
() => FetchSongAsync(videoId, cancellationToken));
|
||||
}
|
||||
|
||||
private async Task<ErrorOr<YouTubeSongResult>> FetchSongAsync(string videoId, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
var info = await client.GetSongVideoInfoAsync(videoId, cancellationToken);
|
||||
|
||||
return new YouTubeSongResult(
|
||||
videoId,
|
||||
info.Name,
|
||||
string.Join(", ", info.Artists.Select(artist => artist.Name)),
|
||||
info.Album?.Name ?? string.Empty,
|
||||
(int)info.Duration.TotalSeconds,
|
||||
PickThumbnail(info.Thumbnails),
|
||||
info.IsExplicit,
|
||||
info.Artists
|
||||
.Select(artist => new YouTubeArtistRef(
|
||||
string.IsNullOrEmpty(artist.Id) ? null : artist.Id,
|
||||
artist.Name))
|
||||
.ToList());
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to load YouTube song info for '{VideoId}'", videoId);
|
||||
return Error.Failure(description: $"Failed to load YouTube song info for '{videoId}': {exception.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public string ResolveArtworkUrl(string thumbnailUrl) =>
|
||||
YouTubeThumbNailHelper.WithSize(thumbnailUrl, configuration.ArtworkSize);
|
||||
|
||||
private string PickThumbnail(IEnumerable<YouTubeMusicAPI.Models.Thumbnail> thumbnails)
|
||||
{
|
||||
var ordered = thumbnails.OrderBy(thumbnail => thumbnail.Width).ToList();
|
||||
|
||||
var square = ordered.Where(thumbnail => YouTubeThumbNailHelper.IsSquare(thumbnail.Url)).ToList();
|
||||
var candidates = square.Count > 0 ? square : ordered;
|
||||
|
||||
var chosen = candidates.FirstOrDefault(thumbnail => thumbnail.Width >= configuration.ThumbnailSize)
|
||||
?? candidates.LastOrDefault();
|
||||
|
||||
return chosen is null
|
||||
? string.Empty
|
||||
: YouTubeThumbNailHelper.WithSize(chosen.Url, configuration.ThumbnailSize);
|
||||
}
|
||||
|
||||
private static string SearchCacheKey(string token) => $"yt-search:{token}";
|
||||
|
||||
private static string SearchResultsCacheKey(string query) => $"yt-search-results:{query.Trim().ToLowerInvariant()}";
|
||||
|
||||
private static string AlbumSearchResultsCacheKey(string query) => $"yt-album-search:{query.Trim().ToLowerInvariant()}";
|
||||
|
||||
private static string AlbumCacheKey(string albumId) => $"yt-album:{albumId}";
|
||||
|
||||
private static string SongInfoCacheKey(string videoId) => $"yt-song:{videoId}";
|
||||
|
||||
private static string StreamCacheKey(string videoId) => $"yt-stream:{videoId}";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user