mirror of
https://github.com/SoPat712/allstarr.git
synced 2026-02-09 23:55:10 -05:00
fix: merge conflicts and upstream changes merged
This commit is contained in:
@@ -4,6 +4,7 @@ using octo_fiesta.Models.Download;
|
||||
using octo_fiesta.Models.Search;
|
||||
using octo_fiesta.Models.Subsonic;
|
||||
using octo_fiesta.Services.Local;
|
||||
using octo_fiesta.Services.Subsonic;
|
||||
using TagLib;
|
||||
using IOFile = System.IO.File;
|
||||
|
||||
@@ -21,12 +22,30 @@ public abstract class BaseDownloadService : IDownloadService
|
||||
protected readonly IMusicMetadataService MetadataService;
|
||||
protected readonly SubsonicSettings SubsonicSettings;
|
||||
protected readonly ILogger Logger;
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
|
||||
protected readonly string DownloadPath;
|
||||
protected readonly string CachePath;
|
||||
|
||||
protected readonly Dictionary<string, DownloadInfo> ActiveDownloads = new();
|
||||
protected readonly SemaphoreSlim DownloadLock = new(1, 1);
|
||||
|
||||
/// <summary>
|
||||
/// Lazy-loaded PlaylistSyncService to avoid circular dependency
|
||||
/// </summary>
|
||||
private PlaylistSyncService? _playlistSyncService;
|
||||
protected PlaylistSyncService? PlaylistSyncService
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_playlistSyncService == null)
|
||||
{
|
||||
_playlistSyncService = _serviceProvider.GetService<PlaylistSyncService>();
|
||||
}
|
||||
return _playlistSyncService;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Provider name (e.g., "deezer", "qobuz")
|
||||
/// </summary>
|
||||
@@ -37,20 +56,28 @@ public abstract class BaseDownloadService : IDownloadService
|
||||
ILocalLibraryService localLibraryService,
|
||||
IMusicMetadataService metadataService,
|
||||
SubsonicSettings subsonicSettings,
|
||||
IServiceProvider serviceProvider,
|
||||
ILogger logger)
|
||||
{
|
||||
Configuration = configuration;
|
||||
LocalLibraryService = localLibraryService;
|
||||
MetadataService = metadataService;
|
||||
SubsonicSettings = subsonicSettings;
|
||||
_serviceProvider = serviceProvider;
|
||||
Logger = logger;
|
||||
|
||||
DownloadPath = configuration["Library:DownloadPath"] ?? "./downloads";
|
||||
CachePath = PathHelper.GetCachePath();
|
||||
|
||||
if (!Directory.Exists(DownloadPath))
|
||||
{
|
||||
Directory.CreateDirectory(DownloadPath);
|
||||
}
|
||||
|
||||
if (!Directory.Exists(CachePath))
|
||||
{
|
||||
Directory.CreateDirectory(CachePath);
|
||||
}
|
||||
}
|
||||
|
||||
#region IDownloadService Implementation
|
||||
@@ -62,7 +89,7 @@ public abstract class BaseDownloadService : IDownloadService
|
||||
|
||||
public async Task<Stream> DownloadAndStreamAsync(string externalProvider, string externalId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var localPath = await DownloadSongAsync(externalProvider, externalId, cancellationToken);
|
||||
var localPath = await DownloadSongInternalAsync(externalProvider, externalId, triggerAlbumDownload: true, cancellationToken);
|
||||
return IOFile.OpenRead(localPath);
|
||||
}
|
||||
|
||||
@@ -72,6 +99,30 @@ public abstract class BaseDownloadService : IDownloadService
|
||||
return info;
|
||||
}
|
||||
|
||||
public async Task<string?> GetLocalPathIfExistsAsync(string externalProvider, string externalId)
|
||||
{
|
||||
if (externalProvider != ProviderName)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// Check local library
|
||||
var localPath = await LocalLibraryService.GetLocalPathForExternalSongAsync(externalProvider, externalId);
|
||||
if (localPath != null && IOFile.Exists(localPath))
|
||||
{
|
||||
return localPath;
|
||||
}
|
||||
|
||||
// Check cache directory
|
||||
var cachedPath = GetCachedFilePath(externalProvider, externalId);
|
||||
if (cachedPath != null && IOFile.Exists(cachedPath))
|
||||
{
|
||||
return cachedPath;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public abstract Task<bool> IsAvailableAsync();
|
||||
|
||||
public void DownloadRemainingAlbumTracksInBackground(string externalProvider, string albumExternalId, string excludeTrackExternalId)
|
||||
@@ -130,37 +181,86 @@ public abstract class BaseDownloadService : IDownloadService
|
||||
}
|
||||
|
||||
var songId = $"ext-{externalProvider}-{externalId}";
|
||||
var isCache = SubsonicSettings.StorageMode == StorageMode.Cache;
|
||||
|
||||
// Check if already downloaded
|
||||
var existingPath = await LocalLibraryService.GetLocalPathForExternalSongAsync(externalProvider, externalId);
|
||||
if (existingPath != null && IOFile.Exists(existingPath))
|
||||
{
|
||||
Logger.LogInformation("Song already downloaded: {Path}", existingPath);
|
||||
return existingPath;
|
||||
}
|
||||
|
||||
// Check if download in progress
|
||||
if (ActiveDownloads.TryGetValue(songId, out var activeDownload) && activeDownload.Status == DownloadStatus.InProgress)
|
||||
{
|
||||
Logger.LogInformation("Download already in progress for {SongId}", songId);
|
||||
while (ActiveDownloads.TryGetValue(songId, out activeDownload) && activeDownload.Status == DownloadStatus.InProgress)
|
||||
{
|
||||
await Task.Delay(500, cancellationToken);
|
||||
}
|
||||
|
||||
if (activeDownload?.Status == DownloadStatus.Completed && activeDownload.LocalPath != null)
|
||||
{
|
||||
return activeDownload.LocalPath;
|
||||
}
|
||||
|
||||
throw new Exception(activeDownload?.ErrorMessage ?? "Download failed");
|
||||
}
|
||||
|
||||
// Acquire lock BEFORE checking existence to prevent race conditions with concurrent requests
|
||||
await DownloadLock.WaitAsync(cancellationToken);
|
||||
|
||||
try
|
||||
{
|
||||
// Check if already downloaded (skip for cache mode as we want to check cache folder)
|
||||
if (!isCache)
|
||||
{
|
||||
var existingPath = await LocalLibraryService.GetLocalPathForExternalSongAsync(externalProvider, externalId);
|
||||
if (existingPath != null && IOFile.Exists(existingPath))
|
||||
{
|
||||
Logger.LogInformation("Song already downloaded: {Path}", existingPath);
|
||||
return existingPath;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// For cache mode, check if file exists in cache directory
|
||||
var cachedPath = GetCachedFilePath(externalProvider, externalId);
|
||||
if (cachedPath != null && IOFile.Exists(cachedPath))
|
||||
{
|
||||
Logger.LogInformation("Song found in cache: {Path}", cachedPath);
|
||||
// Update file access time for cache cleanup logic
|
||||
IOFile.SetLastAccessTime(cachedPath, DateTime.UtcNow);
|
||||
return cachedPath;
|
||||
}
|
||||
}
|
||||
|
||||
// Check if download in progress
|
||||
if (ActiveDownloads.TryGetValue(songId, out var activeDownload) && activeDownload.Status == DownloadStatus.InProgress)
|
||||
{
|
||||
Logger.LogInformation("Download already in progress for {SongId}, waiting...", songId);
|
||||
// Release lock while waiting
|
||||
DownloadLock.Release();
|
||||
|
||||
while (ActiveDownloads.TryGetValue(songId, out activeDownload) && activeDownload.Status == DownloadStatus.InProgress)
|
||||
{
|
||||
await Task.Delay(500, cancellationToken);
|
||||
}
|
||||
|
||||
if (activeDownload?.Status == DownloadStatus.Completed && activeDownload.LocalPath != null)
|
||||
{
|
||||
return activeDownload.LocalPath;
|
||||
}
|
||||
|
||||
throw new Exception(activeDownload?.ErrorMessage ?? "Download failed");
|
||||
}
|
||||
|
||||
// Get metadata
|
||||
var song = await MetadataService.GetSongAsync(externalProvider, externalId);
|
||||
// In Album mode, fetch the full album first to ensure AlbumArtist is correctly set
|
||||
Song? song = null;
|
||||
|
||||
if (SubsonicSettings.DownloadMode == DownloadMode.Album)
|
||||
{
|
||||
// First try to get the song to extract album ID
|
||||
var tempSong = await MetadataService.GetSongAsync(externalProvider, externalId);
|
||||
if (tempSong != null && !string.IsNullOrEmpty(tempSong.AlbumId))
|
||||
{
|
||||
var albumExternalId = ExtractExternalIdFromAlbumId(tempSong.AlbumId);
|
||||
if (!string.IsNullOrEmpty(albumExternalId))
|
||||
{
|
||||
// Get full album with correct AlbumArtist
|
||||
var album = await MetadataService.GetAlbumAsync(externalProvider, albumExternalId);
|
||||
if (album != null)
|
||||
{
|
||||
// Find the track in the album
|
||||
song = album.Songs.FirstOrDefault(s => s.ExternalId == externalId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback to individual song fetch if not in Album mode or album fetch failed
|
||||
if (song == null)
|
||||
{
|
||||
song = await MetadataService.GetSongAsync(externalProvider, externalId);
|
||||
}
|
||||
|
||||
if (song == null)
|
||||
{
|
||||
throw new Exception("Song not found");
|
||||
@@ -176,15 +276,35 @@ public abstract class BaseDownloadService : IDownloadService
|
||||
};
|
||||
ActiveDownloads[songId] = downloadInfo;
|
||||
|
||||
try
|
||||
var localPath = await DownloadTrackAsync(externalId, song, cancellationToken);
|
||||
|
||||
downloadInfo.Status = DownloadStatus.Completed;
|
||||
downloadInfo.LocalPath = localPath;
|
||||
downloadInfo.CompletedAt = DateTime.UtcNow;
|
||||
|
||||
song.LocalPath = localPath;
|
||||
|
||||
// Check if this track belongs to a playlist and update M3U
|
||||
if (PlaylistSyncService != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var playlistId = PlaylistSyncService.GetPlaylistIdForTrack(songId);
|
||||
if (playlistId != null)
|
||||
{
|
||||
Logger.LogInformation("Track {SongId} belongs to playlist {PlaylistId}, adding to M3U", songId, playlistId);
|
||||
await PlaylistSyncService.AddTrackToM3UAsync(playlistId, song, localPath, isFullPlaylistDownload: false);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogWarning(ex, "Failed to update playlist M3U for track {SongId}", songId);
|
||||
}
|
||||
}
|
||||
|
||||
// Only register and scan if NOT in cache mode
|
||||
if (!isCache)
|
||||
{
|
||||
var localPath = await DownloadTrackAsync(externalId, song, cancellationToken);
|
||||
|
||||
downloadInfo.Status = DownloadStatus.Completed;
|
||||
downloadInfo.LocalPath = localPath;
|
||||
downloadInfo.CompletedAt = DateTime.UtcNow;
|
||||
|
||||
song.LocalPath = localPath;
|
||||
await LocalLibraryService.RegisterDownloadedSongAsync(song, localPath);
|
||||
|
||||
// Trigger a Subsonic library rescan (with debounce)
|
||||
@@ -210,17 +330,24 @@ public abstract class BaseDownloadService : IDownloadService
|
||||
DownloadRemainingAlbumTracksInBackground(externalProvider, albumExternalId, externalId);
|
||||
}
|
||||
}
|
||||
|
||||
Logger.LogInformation("Download completed: {Path}", localPath);
|
||||
return localPath;
|
||||
}
|
||||
catch (Exception ex)
|
||||
else
|
||||
{
|
||||
Logger.LogInformation("Cache mode: skipping library registration and scan");
|
||||
}
|
||||
|
||||
Logger.LogInformation("Download completed: {Path}", localPath);
|
||||
return localPath;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (ActiveDownloads.TryGetValue(songId, out var downloadInfo))
|
||||
{
|
||||
downloadInfo.Status = DownloadStatus.Failed;
|
||||
downloadInfo.ErrorMessage = ex.Message;
|
||||
Logger.LogError(ex, "Download failed for {SongId}", songId);
|
||||
throw;
|
||||
}
|
||||
Logger.LogError(ex, "Download failed for {SongId}", songId);
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -258,6 +385,23 @@ public abstract class BaseDownloadService : IDownloadService
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check if download is already in progress or recently completed
|
||||
var songId = $"ext-{ProviderName}-{track.ExternalId}";
|
||||
if (ActiveDownloads.TryGetValue(songId, out var activeDownload))
|
||||
{
|
||||
if (activeDownload.Status == DownloadStatus.InProgress)
|
||||
{
|
||||
Logger.LogDebug("Track {TrackId} download already in progress, skipping", track.ExternalId);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (activeDownload.Status == DownloadStatus.Completed)
|
||||
{
|
||||
Logger.LogDebug("Track {TrackId} already downloaded in this session, skipping", track.ExternalId);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
Logger.LogInformation("Downloading track '{Title}' from album '{Album}'", track.Title, album.Title);
|
||||
await DownloadSongInternalAsync(ProviderName, track.ExternalId!, triggerAlbumDownload: false, CancellationToken.None);
|
||||
}
|
||||
@@ -401,5 +545,31 @@ public abstract class BaseDownloadService : IDownloadService
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the cached file path for a given provider and external ID
|
||||
/// Returns null if no cached file exists
|
||||
/// </summary>
|
||||
protected string? GetCachedFilePath(string provider, string externalId)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Search for cached files matching the pattern: {provider}_{externalId}.*
|
||||
var pattern = $"{provider}_{externalId}.*";
|
||||
var files = Directory.GetFiles(CachePath, pattern, SearchOption.AllDirectories);
|
||||
|
||||
if (files.Length > 0)
|
||||
{
|
||||
return files[0]; // Return first match
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogWarning(ex, "Failed to search for cached file: {Provider}_{ExternalId}", provider, externalId);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
163
octo-fiesta/Services/Common/CacheCleanupService.cs
Normal file
163
octo-fiesta/Services/Common/CacheCleanupService.cs
Normal file
@@ -0,0 +1,163 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
using octo_fiesta.Models.Settings;
|
||||
|
||||
namespace octo_fiesta.Services.Common;
|
||||
|
||||
/// <summary>
|
||||
/// Background service that periodically cleans up old cached files
|
||||
/// Only runs when StorageMode is set to Cache
|
||||
/// </summary>
|
||||
public class CacheCleanupService : BackgroundService
|
||||
{
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly SubsonicSettings _subsonicSettings;
|
||||
private readonly ILogger<CacheCleanupService> _logger;
|
||||
private readonly TimeSpan _cleanupInterval = TimeSpan.FromHours(1);
|
||||
|
||||
public CacheCleanupService(
|
||||
IConfiguration configuration,
|
||||
IOptions<SubsonicSettings> subsonicSettings,
|
||||
ILogger<CacheCleanupService> logger)
|
||||
{
|
||||
_configuration = configuration;
|
||||
_subsonicSettings = subsonicSettings.Value;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
// Only run if storage mode is Cache
|
||||
if (_subsonicSettings.StorageMode != StorageMode.Cache)
|
||||
{
|
||||
_logger.LogInformation("CacheCleanupService disabled: StorageMode is not Cache");
|
||||
return;
|
||||
}
|
||||
|
||||
_logger.LogInformation("CacheCleanupService started with cleanup interval of {Interval} and retention of {Hours} hours",
|
||||
_cleanupInterval, _subsonicSettings.CacheDurationHours);
|
||||
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
try
|
||||
{
|
||||
await CleanupOldCachedFilesAsync(stoppingToken);
|
||||
await Task.Delay(_cleanupInterval, stoppingToken);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
// Service is stopping, exit gracefully
|
||||
break;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error during cache cleanup");
|
||||
// Continue running even if cleanup fails
|
||||
await Task.Delay(_cleanupInterval, stoppingToken);
|
||||
}
|
||||
}
|
||||
|
||||
_logger.LogInformation("CacheCleanupService stopped");
|
||||
}
|
||||
|
||||
private async Task CleanupOldCachedFilesAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var cachePath = PathHelper.GetCachePath();
|
||||
|
||||
if (!Directory.Exists(cachePath))
|
||||
{
|
||||
_logger.LogDebug("Cache directory does not exist: {Path}", cachePath);
|
||||
return;
|
||||
}
|
||||
|
||||
var cutoffTime = DateTime.UtcNow.AddHours(-_subsonicSettings.CacheDurationHours);
|
||||
var deletedCount = 0;
|
||||
var totalSize = 0L;
|
||||
|
||||
_logger.LogInformation("Starting cache cleanup: deleting files older than {CutoffTime}", cutoffTime);
|
||||
|
||||
try
|
||||
{
|
||||
// Get all files in cache directory and subdirectories
|
||||
var files = Directory.GetFiles(cachePath, "*.*", SearchOption.AllDirectories);
|
||||
|
||||
foreach (var filePath in files)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
break;
|
||||
|
||||
try
|
||||
{
|
||||
var fileInfo = new FileInfo(filePath);
|
||||
|
||||
// Use last access time to determine if file should be deleted
|
||||
// This gets updated when a cached file is streamed
|
||||
if (fileInfo.LastAccessTimeUtc < cutoffTime)
|
||||
{
|
||||
var size = fileInfo.Length;
|
||||
File.Delete(filePath);
|
||||
deletedCount++;
|
||||
totalSize += size;
|
||||
_logger.LogDebug("Deleted cached file: {Path} (last accessed: {LastAccess})",
|
||||
filePath, fileInfo.LastAccessTimeUtc);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Failed to delete cached file: {Path}", filePath);
|
||||
}
|
||||
}
|
||||
|
||||
// Clean up empty directories
|
||||
await CleanupEmptyDirectoriesAsync(cachePath, cancellationToken);
|
||||
|
||||
if (deletedCount > 0)
|
||||
{
|
||||
var sizeMB = totalSize / (1024.0 * 1024.0);
|
||||
_logger.LogInformation("Cache cleanup completed: deleted {Count} files, freed {Size:F2} MB",
|
||||
deletedCount, sizeMB);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogDebug("Cache cleanup completed: no files to delete");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error during cache cleanup");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task CleanupEmptyDirectoriesAsync(string rootPath, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
var directories = Directory.GetDirectories(rootPath, "*", SearchOption.AllDirectories)
|
||||
.OrderByDescending(d => d.Length); // Process deepest directories first
|
||||
|
||||
foreach (var directory in directories)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
break;
|
||||
|
||||
try
|
||||
{
|
||||
if (!Directory.EnumerateFileSystemEntries(directory).Any())
|
||||
{
|
||||
Directory.Delete(directory);
|
||||
_logger.LogDebug("Deleted empty directory: {Path}", directory);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Failed to delete empty directory: {Path}", directory);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Error cleaning up empty directories");
|
||||
}
|
||||
|
||||
await Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,17 @@ namespace octo_fiesta.Services.Common;
|
||||
/// </summary>
|
||||
public static class PathHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the cache directory path for temporary file storage.
|
||||
/// Uses system temp directory combined with octo-fiesta-cache subfolder.
|
||||
/// Respects TMPDIR environment variable on Linux/macOS.
|
||||
/// </summary>
|
||||
/// <returns>Full path to the cache directory.</returns>
|
||||
public static string GetCachePath()
|
||||
{
|
||||
return Path.Combine(Path.GetTempPath(), "octo-fiesta-cache");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds the output path for a downloaded track following the Artist/Album/Track structure.
|
||||
/// </summary>
|
||||
|
||||
76
octo-fiesta/Services/Common/PlaylistIdHelper.cs
Normal file
76
octo-fiesta/Services/Common/PlaylistIdHelper.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
namespace octo_fiesta.Services.Common;
|
||||
|
||||
/// <summary>
|
||||
/// Helper class for handling external playlist IDs.
|
||||
/// Playlist IDs use the format: "pl-{provider}-{externalId}"
|
||||
/// Example: "pl-deezer-123456", "pl-qobuz-789"
|
||||
/// </summary>
|
||||
public static class PlaylistIdHelper
|
||||
{
|
||||
private const string PlaylistPrefix = "pl-";
|
||||
|
||||
/// <summary>
|
||||
/// Checks if an ID represents an external playlist.
|
||||
/// </summary>
|
||||
/// <param name="id">The ID to check</param>
|
||||
/// <returns>True if the ID starts with "pl-", false otherwise</returns>
|
||||
public static bool IsExternalPlaylist(string? id)
|
||||
{
|
||||
return !string.IsNullOrEmpty(id) && id.StartsWith(PlaylistPrefix, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parses a playlist ID to extract provider and external ID.
|
||||
/// </summary>
|
||||
/// <param name="id">The playlist ID in format "pl-{provider}-{externalId}"</param>
|
||||
/// <returns>A tuple containing (provider, externalId)</returns>
|
||||
/// <exception cref="ArgumentException">Thrown if the ID format is invalid</exception>
|
||||
public static (string provider, string externalId) ParsePlaylistId(string id)
|
||||
{
|
||||
if (!IsExternalPlaylist(id))
|
||||
{
|
||||
throw new ArgumentException($"Invalid playlist ID format. Expected 'pl-{{provider}}-{{externalId}}', got '{id}'", nameof(id));
|
||||
}
|
||||
|
||||
// Remove "pl-" prefix
|
||||
var withoutPrefix = id.Substring(PlaylistPrefix.Length);
|
||||
|
||||
// Split by first dash to get provider and externalId
|
||||
var dashIndex = withoutPrefix.IndexOf('-');
|
||||
if (dashIndex == -1)
|
||||
{
|
||||
throw new ArgumentException($"Invalid playlist ID format. Expected 'pl-{{provider}}-{{externalId}}', got '{id}'", nameof(id));
|
||||
}
|
||||
|
||||
var provider = withoutPrefix.Substring(0, dashIndex);
|
||||
var externalId = withoutPrefix.Substring(dashIndex + 1);
|
||||
|
||||
if (string.IsNullOrEmpty(provider) || string.IsNullOrEmpty(externalId))
|
||||
{
|
||||
throw new ArgumentException($"Invalid playlist ID format. Provider or external ID is empty in '{id}'", nameof(id));
|
||||
}
|
||||
|
||||
return (provider, externalId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a playlist ID from provider and external ID.
|
||||
/// </summary>
|
||||
/// <param name="provider">The provider name (e.g., "deezer", "qobuz")</param>
|
||||
/// <param name="externalId">The external ID from the provider</param>
|
||||
/// <returns>A playlist ID in format "pl-{provider}-{externalId}"</returns>
|
||||
public static string CreatePlaylistId(string provider, string externalId)
|
||||
{
|
||||
if (string.IsNullOrEmpty(provider))
|
||||
{
|
||||
throw new ArgumentException("Provider cannot be null or empty", nameof(provider));
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(externalId))
|
||||
{
|
||||
throw new ArgumentException("External ID cannot be null or empty", nameof(externalId));
|
||||
}
|
||||
|
||||
return $"{PlaylistPrefix}{provider.ToLowerInvariant()}-{externalId}";
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ using octo_fiesta.Models.Search;
|
||||
using octo_fiesta.Models.Subsonic;
|
||||
using octo_fiesta.Services.Local;
|
||||
using octo_fiesta.Services.Common;
|
||||
using octo_fiesta.Services.Subsonic;
|
||||
using Microsoft.Extensions.Options;
|
||||
using IOFile = System.IO.File;
|
||||
|
||||
@@ -50,8 +51,9 @@ public class DeezerDownloadService : BaseDownloadService
|
||||
IMusicMetadataService metadataService,
|
||||
IOptions<SubsonicSettings> subsonicSettings,
|
||||
IOptions<DeezerSettings> deezerSettings,
|
||||
IServiceProvider serviceProvider,
|
||||
ILogger<DeezerDownloadService> logger)
|
||||
: base(configuration, localLibraryService, metadataService, subsonicSettings.Value, logger)
|
||||
: base(configuration, localLibraryService, metadataService, subsonicSettings.Value, serviceProvider, logger)
|
||||
{
|
||||
_httpClient = httpClientFactory.CreateClient();
|
||||
|
||||
@@ -109,7 +111,8 @@ public class DeezerDownloadService : BaseDownloadService
|
||||
|
||||
// Build organized folder structure: Artist/Album/Track using AlbumArtist (fallback to Artist for singles)
|
||||
var artistForPath = song.AlbumArtist ?? song.Artist;
|
||||
var outputPath = PathHelper.BuildTrackPath(DownloadPath, artistForPath, song.Album, song.Title, song.Track, extension);
|
||||
var basePath = SubsonicSettings.StorageMode == StorageMode.Cache ? CachePath : DownloadPath;
|
||||
var outputPath = PathHelper.BuildTrackPath(basePath, artistForPath, song.Album, song.Title, song.Track, extension);
|
||||
|
||||
// Create directories if they don't exist
|
||||
var albumFolder = Path.GetDirectoryName(outputPath)!;
|
||||
|
||||
@@ -229,8 +229,14 @@ public class DeezerMetadataService : IMusicMetadataService
|
||||
int trackIndex = 1;
|
||||
foreach (var track in tracksData.EnumerateArray())
|
||||
{
|
||||
// Pass the index as fallback for track_position (Deezer doesn't include it in album tracks)
|
||||
var song = ParseDeezerTrack(track, trackIndex);
|
||||
// Pass the album artist to ensure proper folder organization
|
||||
var song = ParseDeezerTrack(track, trackIndex, album.Artist);
|
||||
|
||||
// Ensure album metadata is set (tracks in album response may not have full album object)
|
||||
song.Album = album.Title;
|
||||
song.AlbumId = album.Id;
|
||||
song.AlbumArtist = album.Artist;
|
||||
|
||||
if (ShouldIncludeSong(song))
|
||||
{
|
||||
album.Songs.Add(song);
|
||||
@@ -283,7 +289,7 @@ public class DeezerMetadataService : IMusicMetadataService
|
||||
return albums;
|
||||
}
|
||||
|
||||
private Song ParseDeezerTrack(JsonElement track, int? fallbackTrackNumber = null)
|
||||
private Song ParseDeezerTrack(JsonElement track, int? fallbackTrackNumber = null, string? albumArtist = null)
|
||||
{
|
||||
var externalId = track.GetProperty("id").GetInt64().ToString();
|
||||
|
||||
@@ -321,6 +327,7 @@ public class DeezerMetadataService : IMusicMetadataService
|
||||
albumForCover.TryGetProperty("cover_medium", out var cover)
|
||||
? cover.GetString()
|
||||
: null,
|
||||
AlbumArtist = albumArtist,
|
||||
IsLocal = false,
|
||||
ExternalProvider = "deezer",
|
||||
ExternalId = externalId,
|
||||
@@ -510,6 +517,163 @@ public class DeezerMetadataService : IMusicMetadataService
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<List<ExternalPlaylist>> SearchPlaylistsAsync(string query, int limit = 20)
|
||||
{
|
||||
try
|
||||
{
|
||||
var url = $"{BaseUrl}/search/playlist?q={Uri.EscapeDataString(query)}&limit={limit}";
|
||||
var response = await _httpClient.GetAsync(url);
|
||||
|
||||
if (!response.IsSuccessStatusCode) return new List<ExternalPlaylist>();
|
||||
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
var result = JsonDocument.Parse(json);
|
||||
|
||||
var playlists = new List<ExternalPlaylist>();
|
||||
if (result.RootElement.TryGetProperty("data", out var data))
|
||||
{
|
||||
foreach (var playlist in data.EnumerateArray())
|
||||
{
|
||||
playlists.Add(ParseDeezerPlaylist(playlist));
|
||||
}
|
||||
}
|
||||
|
||||
return playlists;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return new List<ExternalPlaylist>();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ExternalPlaylist?> GetPlaylistAsync(string externalProvider, string externalId)
|
||||
{
|
||||
if (externalProvider != "deezer") return null;
|
||||
|
||||
try
|
||||
{
|
||||
var url = $"{BaseUrl}/playlist/{externalId}";
|
||||
var response = await _httpClient.GetAsync(url);
|
||||
|
||||
if (!response.IsSuccessStatusCode) return null;
|
||||
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
var playlistElement = JsonDocument.Parse(json).RootElement;
|
||||
|
||||
if (playlistElement.TryGetProperty("error", out _)) return null;
|
||||
|
||||
return ParseDeezerPlaylist(playlistElement);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<List<Song>> GetPlaylistTracksAsync(string externalProvider, string externalId)
|
||||
{
|
||||
if (externalProvider != "deezer") return new List<Song>();
|
||||
|
||||
try
|
||||
{
|
||||
var url = $"{BaseUrl}/playlist/{externalId}";
|
||||
var response = await _httpClient.GetAsync(url);
|
||||
|
||||
if (!response.IsSuccessStatusCode) return new List<Song>();
|
||||
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
var playlistElement = JsonDocument.Parse(json).RootElement;
|
||||
|
||||
if (playlistElement.TryGetProperty("error", out _)) return new List<Song>();
|
||||
|
||||
var songs = new List<Song>();
|
||||
|
||||
// Get playlist name for album field
|
||||
var playlistName = playlistElement.TryGetProperty("title", out var titleEl)
|
||||
? titleEl.GetString() ?? "Unknown Playlist"
|
||||
: "Unknown Playlist";
|
||||
|
||||
if (playlistElement.TryGetProperty("tracks", out var tracks) &&
|
||||
tracks.TryGetProperty("data", out var tracksData))
|
||||
{
|
||||
int trackIndex = 1;
|
||||
foreach (var track in tracksData.EnumerateArray())
|
||||
{
|
||||
// For playlists, use the track's own artist (not a single album artist)
|
||||
var song = ParseDeezerTrack(track, trackIndex);
|
||||
|
||||
// Override album name to be the playlist name
|
||||
song.Album = playlistName;
|
||||
|
||||
if (ShouldIncludeSong(song))
|
||||
{
|
||||
songs.Add(song);
|
||||
}
|
||||
trackIndex++;
|
||||
}
|
||||
}
|
||||
|
||||
return songs;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return new List<Song>();
|
||||
}
|
||||
}
|
||||
|
||||
private ExternalPlaylist ParseDeezerPlaylist(JsonElement playlist)
|
||||
{
|
||||
var externalId = playlist.GetProperty("id").GetInt64().ToString();
|
||||
|
||||
// Get curator/creator name
|
||||
string? curatorName = null;
|
||||
if (playlist.TryGetProperty("user", out var user) &&
|
||||
user.TryGetProperty("name", out var userName))
|
||||
{
|
||||
curatorName = userName.GetString();
|
||||
}
|
||||
else if (playlist.TryGetProperty("creator", out var creator) &&
|
||||
creator.TryGetProperty("name", out var creatorName))
|
||||
{
|
||||
curatorName = creatorName.GetString();
|
||||
}
|
||||
|
||||
// Get creation date
|
||||
DateTime? createdDate = null;
|
||||
if (playlist.TryGetProperty("creation_date", out var creationDateEl))
|
||||
{
|
||||
var dateStr = creationDateEl.GetString();
|
||||
if (!string.IsNullOrEmpty(dateStr) && DateTime.TryParse(dateStr, out var date))
|
||||
{
|
||||
createdDate = date;
|
||||
}
|
||||
}
|
||||
|
||||
return new ExternalPlaylist
|
||||
{
|
||||
Id = Common.PlaylistIdHelper.CreatePlaylistId("deezer", externalId),
|
||||
Name = playlist.GetProperty("title").GetString() ?? "",
|
||||
Description = playlist.TryGetProperty("description", out var desc)
|
||||
? desc.GetString()
|
||||
: null,
|
||||
CuratorName = curatorName,
|
||||
Provider = "deezer",
|
||||
ExternalId = externalId,
|
||||
TrackCount = playlist.TryGetProperty("nb_tracks", out var nbTracks)
|
||||
? nbTracks.GetInt32()
|
||||
: 0,
|
||||
Duration = playlist.TryGetProperty("duration", out var duration)
|
||||
? duration.GetInt32()
|
||||
: 0,
|
||||
CoverUrl = playlist.TryGetProperty("picture_medium", out var picture)
|
||||
? picture.GetString()
|
||||
: (playlist.TryGetProperty("picture_big", out var pictureBig)
|
||||
? pictureBig.GetString()
|
||||
: null),
|
||||
CreatedDate = createdDate
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether a song should be included based on the explicit content filter setting
|
||||
/// </summary>
|
||||
|
||||
@@ -42,6 +42,14 @@ public interface IDownloadService
|
||||
/// </summary>
|
||||
DownloadInfo? GetDownloadStatus(string songId);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the local path for a song if it has been downloaded already
|
||||
/// </summary>
|
||||
/// <param name="externalProvider">The provider (deezer, qobuz, etc.)</param>
|
||||
/// <param name="externalId">The ID on the external provider</param>
|
||||
/// <returns>The local file path if exists, null otherwise</returns>
|
||||
Task<string?> GetLocalPathIfExistsAsync(string externalProvider, string externalId);
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the service is properly configured and functional
|
||||
/// </summary>
|
||||
|
||||
@@ -54,4 +54,28 @@ public interface IMusicMetadataService
|
||||
/// Gets an artist's albums
|
||||
/// </summary>
|
||||
Task<List<Album>> GetArtistAlbumsAsync(string externalProvider, string externalId);
|
||||
|
||||
/// <summary>
|
||||
/// Searches for playlists on external providers
|
||||
/// </summary>
|
||||
/// <param name="query">Search term</param>
|
||||
/// <param name="limit">Maximum number of results</param>
|
||||
/// <returns>List of found playlists</returns>
|
||||
Task<List<ExternalPlaylist>> SearchPlaylistsAsync(string query, int limit = 20);
|
||||
|
||||
/// <summary>
|
||||
/// Gets details of an external playlist (metadata only, not tracks)
|
||||
/// </summary>
|
||||
/// <param name="externalProvider">Provider name (e.g., "deezer", "qobuz")</param>
|
||||
/// <param name="externalId">Playlist ID from the provider</param>
|
||||
/// <returns>Playlist details or null if not found</returns>
|
||||
Task<ExternalPlaylist?> GetPlaylistAsync(string externalProvider, string externalId);
|
||||
|
||||
/// <summary>
|
||||
/// Gets all tracks from an external playlist
|
||||
/// </summary>
|
||||
/// <param name="externalProvider">Provider name (e.g., "deezer", "qobuz")</param>
|
||||
/// <param name="externalId">Playlist ID from the provider</param>
|
||||
/// <returns>List of songs in the playlist</returns>
|
||||
Task<List<Song>> GetPlaylistTracksAsync(string externalProvider, string externalId);
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ public class QobuzBundleService
|
||||
/// <summary>
|
||||
/// Gets the Qobuz App ID, extracting it from the bundle if not cached
|
||||
/// </summary>
|
||||
public async Task<string> GetAppIdAsync()
|
||||
public virtual async Task<string> GetAppIdAsync()
|
||||
{
|
||||
await EnsureInitializedAsync();
|
||||
return _cachedAppId!;
|
||||
@@ -49,7 +49,7 @@ public class QobuzBundleService
|
||||
/// <summary>
|
||||
/// Gets the Qobuz secrets list, extracting them from the bundle if not cached
|
||||
/// </summary>
|
||||
public async Task<List<string>> GetSecretsAsync()
|
||||
public virtual async Task<List<string>> GetSecretsAsync()
|
||||
{
|
||||
await EnsureInitializedAsync();
|
||||
return _cachedSecrets!;
|
||||
@@ -58,7 +58,7 @@ public class QobuzBundleService
|
||||
/// <summary>
|
||||
/// Gets a specific secret by index (used for signing requests)
|
||||
/// </summary>
|
||||
public async Task<string> GetSecretAsync(int index = 0)
|
||||
public virtual async Task<string> GetSecretAsync(int index = 0)
|
||||
{
|
||||
var secrets = await GetSecretsAsync();
|
||||
if (index < 0 || index >= secrets.Count)
|
||||
|
||||
@@ -8,6 +8,7 @@ using octo_fiesta.Models.Search;
|
||||
using octo_fiesta.Models.Subsonic;
|
||||
using octo_fiesta.Services.Local;
|
||||
using octo_fiesta.Services.Common;
|
||||
using octo_fiesta.Services.Subsonic;
|
||||
using Microsoft.Extensions.Options;
|
||||
using IOFile = System.IO.File;
|
||||
|
||||
@@ -43,8 +44,9 @@ public class QobuzDownloadService : BaseDownloadService
|
||||
QobuzBundleService bundleService,
|
||||
IOptions<SubsonicSettings> subsonicSettings,
|
||||
IOptions<QobuzSettings> qobuzSettings,
|
||||
IServiceProvider serviceProvider,
|
||||
ILogger<QobuzDownloadService> logger)
|
||||
: base(configuration, localLibraryService, metadataService, subsonicSettings.Value, logger)
|
||||
: base(configuration, localLibraryService, metadataService, subsonicSettings.Value, serviceProvider, logger)
|
||||
{
|
||||
_httpClient = httpClientFactory.CreateClient();
|
||||
_bundleService = bundleService;
|
||||
@@ -108,7 +110,8 @@ public class QobuzDownloadService : BaseDownloadService
|
||||
|
||||
// Build organized folder structure using AlbumArtist (fallback to Artist for singles)
|
||||
var artistForPath = song.AlbumArtist ?? song.Artist;
|
||||
var outputPath = PathHelper.BuildTrackPath(DownloadPath, artistForPath, song.Album, song.Title, song.Track, extension);
|
||||
var basePath = SubsonicSettings.StorageMode == StorageMode.Cache ? CachePath : DownloadPath;
|
||||
var outputPath = PathHelper.BuildTrackPath(basePath, artistForPath, song.Album, song.Title, song.Track, extension);
|
||||
|
||||
var albumFolder = Path.GetDirectoryName(outputPath)!;
|
||||
EnsureDirectoryExists(albumFolder);
|
||||
|
||||
@@ -212,6 +212,12 @@ public class QobuzMetadataService : IMusicMetadataService
|
||||
foreach (var track in tracksData.EnumerateArray())
|
||||
{
|
||||
var song = ParseQobuzTrack(track);
|
||||
|
||||
// Ensure album metadata is set (tracks in album response may not have full album object)
|
||||
song.Album = album.Title;
|
||||
song.AlbumId = album.Id;
|
||||
song.AlbumArtist = album.Artist;
|
||||
|
||||
if (ShouldIncludeSong(song))
|
||||
{
|
||||
album.Songs.Add(song);
|
||||
@@ -305,6 +311,180 @@ public class QobuzMetadataService : IMusicMetadataService
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<List<ExternalPlaylist>> SearchPlaylistsAsync(string query, int limit = 20)
|
||||
{
|
||||
try
|
||||
{
|
||||
var appId = await _bundleService.GetAppIdAsync();
|
||||
var url = $"{BaseUrl}playlist/search?query={Uri.EscapeDataString(query)}&limit={limit}&app_id={appId}";
|
||||
|
||||
var response = await GetWithAuthAsync(url);
|
||||
if (!response.IsSuccessStatusCode) return new List<ExternalPlaylist>();
|
||||
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
var result = JsonDocument.Parse(json);
|
||||
|
||||
var playlists = new List<ExternalPlaylist>();
|
||||
if (result.RootElement.TryGetProperty("playlists", out var playlistsData) &&
|
||||
playlistsData.TryGetProperty("items", out var items))
|
||||
{
|
||||
foreach (var playlist in items.EnumerateArray())
|
||||
{
|
||||
playlists.Add(ParseQobuzPlaylist(playlist));
|
||||
}
|
||||
}
|
||||
|
||||
return playlists;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to search playlists for query: {Query}", query);
|
||||
return new List<ExternalPlaylist>();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ExternalPlaylist?> GetPlaylistAsync(string externalProvider, string externalId)
|
||||
{
|
||||
if (externalProvider != "qobuz") return null;
|
||||
|
||||
try
|
||||
{
|
||||
var appId = await _bundleService.GetAppIdAsync();
|
||||
var url = $"{BaseUrl}playlist/get?playlist_id={externalId}&app_id={appId}";
|
||||
|
||||
var response = await GetWithAuthAsync(url);
|
||||
if (!response.IsSuccessStatusCode) return null;
|
||||
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
var playlistElement = JsonDocument.Parse(json).RootElement;
|
||||
|
||||
if (playlistElement.TryGetProperty("error", out _)) return null;
|
||||
|
||||
return ParseQobuzPlaylist(playlistElement);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to get playlist {ExternalId}", externalId);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<List<Song>> GetPlaylistTracksAsync(string externalProvider, string externalId)
|
||||
{
|
||||
if (externalProvider != "qobuz") return new List<Song>();
|
||||
|
||||
try
|
||||
{
|
||||
var appId = await _bundleService.GetAppIdAsync();
|
||||
var url = $"{BaseUrl}playlist/get?playlist_id={externalId}&app_id={appId}&extra=tracks";
|
||||
|
||||
var response = await GetWithAuthAsync(url);
|
||||
if (!response.IsSuccessStatusCode) return new List<Song>();
|
||||
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
var playlistElement = JsonDocument.Parse(json).RootElement;
|
||||
|
||||
if (playlistElement.TryGetProperty("error", out _)) return new List<Song>();
|
||||
|
||||
var songs = new List<Song>();
|
||||
|
||||
// Get playlist name for album field
|
||||
var playlistName = playlistElement.TryGetProperty("name", out var nameEl)
|
||||
? nameEl.GetString() ?? "Unknown Playlist"
|
||||
: "Unknown Playlist";
|
||||
|
||||
if (playlistElement.TryGetProperty("tracks", out var tracks) &&
|
||||
tracks.TryGetProperty("items", out var tracksData))
|
||||
{
|
||||
int trackIndex = 1;
|
||||
foreach (var track in tracksData.EnumerateArray())
|
||||
{
|
||||
// For playlists, use the track's own artist (not a single album artist)
|
||||
var song = ParseQobuzTrack(track);
|
||||
|
||||
// Override album name to be the playlist name
|
||||
song.Album = playlistName;
|
||||
song.Track = trackIndex;
|
||||
|
||||
if (ShouldIncludeSong(song))
|
||||
{
|
||||
songs.Add(song);
|
||||
}
|
||||
trackIndex++;
|
||||
}
|
||||
}
|
||||
|
||||
return songs;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to get playlist tracks for {ExternalId}", externalId);
|
||||
return new List<Song>();
|
||||
}
|
||||
}
|
||||
|
||||
private ExternalPlaylist ParseQobuzPlaylist(JsonElement playlist)
|
||||
{
|
||||
var externalId = GetIdAsString(playlist.GetProperty("id"));
|
||||
|
||||
// Get curator/creator name
|
||||
string? curatorName = null;
|
||||
if (playlist.TryGetProperty("owner", out var owner) &&
|
||||
owner.TryGetProperty("name", out var ownerName))
|
||||
{
|
||||
curatorName = ownerName.GetString();
|
||||
}
|
||||
|
||||
// Get creation date
|
||||
DateTime? createdDate = null;
|
||||
if (playlist.TryGetProperty("created_at", out var createdAtEl))
|
||||
{
|
||||
var timestamp = createdAtEl.GetInt64();
|
||||
createdDate = DateTimeOffset.FromUnixTimeSeconds(timestamp).DateTime;
|
||||
}
|
||||
|
||||
// Get cover URL from images
|
||||
string? coverUrl = null;
|
||||
if (playlist.TryGetProperty("images300", out var images300))
|
||||
{
|
||||
var imagesArray = images300.EnumerateArray().ToList();
|
||||
if (imagesArray.Count > 0)
|
||||
{
|
||||
coverUrl = imagesArray[0].GetString();
|
||||
}
|
||||
}
|
||||
else if (playlist.TryGetProperty("image_rectangle", out var imageRect))
|
||||
{
|
||||
var imagesArray = imageRect.EnumerateArray().ToList();
|
||||
if (imagesArray.Count > 0)
|
||||
{
|
||||
coverUrl = imagesArray[0].GetString();
|
||||
}
|
||||
}
|
||||
|
||||
return new ExternalPlaylist
|
||||
{
|
||||
Id = Common.PlaylistIdHelper.CreatePlaylistId("qobuz", externalId),
|
||||
Name = playlist.TryGetProperty("name", out var name)
|
||||
? name.GetString() ?? ""
|
||||
: "",
|
||||
Description = playlist.TryGetProperty("description", out var desc)
|
||||
? desc.GetString()
|
||||
: null,
|
||||
CuratorName = curatorName,
|
||||
Provider = "qobuz",
|
||||
ExternalId = externalId,
|
||||
TrackCount = playlist.TryGetProperty("tracks_count", out var tracksCount)
|
||||
? tracksCount.GetInt32()
|
||||
: 0,
|
||||
Duration = playlist.TryGetProperty("duration", out var duration)
|
||||
? duration.GetInt32()
|
||||
: 0,
|
||||
CoverUrl = coverUrl,
|
||||
CreatedDate = createdDate
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Safely gets an ID value as a string, handling both number and string types from JSON
|
||||
/// </summary>
|
||||
|
||||
411
octo-fiesta/Services/Subsonic/PlaylistSyncService.cs
Normal file
411
octo-fiesta/Services/Subsonic/PlaylistSyncService.cs
Normal file
@@ -0,0 +1,411 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Text;
|
||||
using Microsoft.Extensions.Options;
|
||||
using octo_fiesta.Models.Domain;
|
||||
using octo_fiesta.Models.Settings;
|
||||
using octo_fiesta.Models.Subsonic;
|
||||
using octo_fiesta.Services.Common;
|
||||
using IOFile = System.IO.File;
|
||||
|
||||
namespace octo_fiesta.Services.Subsonic;
|
||||
|
||||
/// <summary>
|
||||
/// Service responsible for downloading playlist tracks and creating M3U files
|
||||
/// </summary>
|
||||
public class PlaylistSyncService
|
||||
{
|
||||
private readonly IMusicMetadataService _deezerMetadataService;
|
||||
private readonly IMusicMetadataService _qobuzMetadataService;
|
||||
private readonly IEnumerable<IDownloadService> _downloadServices;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly SubsonicSettings _subsonicSettings;
|
||||
private readonly ILogger<PlaylistSyncService> _logger;
|
||||
|
||||
// In-memory cache to track which playlist a track belongs to
|
||||
// Key: trackId (format: ext-{provider}-{externalId}), Value: playlistId
|
||||
// TTL: 5 minutes (tracks expire automatically)
|
||||
private readonly ConcurrentDictionary<string, (string PlaylistId, DateTime ExpiresAt)> _trackPlaylistCache = new();
|
||||
private static readonly TimeSpan CacheTTL = TimeSpan.FromMinutes(5);
|
||||
|
||||
private readonly string _musicDirectory;
|
||||
private readonly string _playlistDirectory;
|
||||
|
||||
// Cancellation token for background cleanup task
|
||||
private readonly CancellationTokenSource _cleanupCancellationTokenSource = new();
|
||||
private readonly Task _cleanupTask;
|
||||
|
||||
public PlaylistSyncService(
|
||||
IEnumerable<IMusicMetadataService> metadataServices,
|
||||
IEnumerable<IDownloadService> downloadServices,
|
||||
IConfiguration configuration,
|
||||
IOptions<SubsonicSettings> subsonicSettings,
|
||||
ILogger<PlaylistSyncService> logger)
|
||||
{
|
||||
// Get Deezer and Qobuz metadata services
|
||||
_deezerMetadataService = metadataServices.FirstOrDefault(s => s.GetType().Name.Contains("Deezer"))
|
||||
?? throw new InvalidOperationException("Deezer metadata service not found");
|
||||
_qobuzMetadataService = metadataServices.FirstOrDefault(s => s.GetType().Name.Contains("Qobuz"))
|
||||
?? throw new InvalidOperationException("Qobuz metadata service not found");
|
||||
|
||||
_downloadServices = downloadServices;
|
||||
_configuration = configuration;
|
||||
_subsonicSettings = subsonicSettings.Value;
|
||||
_logger = logger;
|
||||
|
||||
_musicDirectory = configuration["Library:DownloadPath"] ?? "./downloads";
|
||||
_playlistDirectory = Path.Combine(_musicDirectory, _subsonicSettings.PlaylistsDirectory ?? "playlists");
|
||||
|
||||
// Ensure playlists directory exists
|
||||
if (!Directory.Exists(_playlistDirectory))
|
||||
{
|
||||
Directory.CreateDirectory(_playlistDirectory);
|
||||
}
|
||||
|
||||
// Start background cleanup task for expired cache entries
|
||||
_cleanupTask = Task.Run(() => CleanupExpiredCacheEntriesAsync(_cleanupCancellationTokenSource.Token));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the metadata service for the specified provider
|
||||
/// </summary>
|
||||
private IMusicMetadataService? GetMetadataServiceForProvider(string provider)
|
||||
{
|
||||
return provider.ToLower() switch
|
||||
{
|
||||
"deezer" => _deezerMetadataService,
|
||||
"qobuz" => _qobuzMetadataService,
|
||||
_ => null
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a track to the playlist context cache.
|
||||
/// This allows the download service to know which playlist a track belongs to.
|
||||
/// </summary>
|
||||
public void AddTrackToPlaylistCache(string trackId, string playlistId)
|
||||
{
|
||||
var expiresAt = DateTime.UtcNow.Add(CacheTTL);
|
||||
_trackPlaylistCache[trackId] = (playlistId, expiresAt);
|
||||
_logger.LogInformation("Added track {TrackId} to playlist cache with playlistId {PlaylistId}", trackId, playlistId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the playlist ID for a given track ID from cache.
|
||||
/// Returns null if not found or expired.
|
||||
/// </summary>
|
||||
public string? GetPlaylistIdForTrack(string trackId)
|
||||
{
|
||||
if (_trackPlaylistCache.TryGetValue(trackId, out var entry))
|
||||
{
|
||||
if (entry.ExpiresAt > DateTime.UtcNow)
|
||||
{
|
||||
return entry.PlaylistId;
|
||||
}
|
||||
|
||||
// Expired, remove it
|
||||
_trackPlaylistCache.TryRemove(trackId, out _);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Downloads all tracks from a playlist and creates an M3U file.
|
||||
/// This is triggered when a user stars a playlist.
|
||||
/// </summary>
|
||||
public async Task DownloadFullPlaylistAsync(string playlistId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
_logger.LogInformation("Starting download for playlist {PlaylistId}", playlistId);
|
||||
|
||||
// Parse playlist ID
|
||||
if (!PlaylistIdHelper.IsExternalPlaylist(playlistId))
|
||||
{
|
||||
_logger.LogWarning("Invalid playlist ID format: {PlaylistId}", playlistId);
|
||||
return;
|
||||
}
|
||||
|
||||
var (provider, externalId) = PlaylistIdHelper.ParsePlaylistId(playlistId);
|
||||
|
||||
// Get playlist metadata
|
||||
var metadataService = GetMetadataServiceForProvider(provider);
|
||||
if (metadataService == null)
|
||||
{
|
||||
throw new NotSupportedException($"Provider '{provider}' not supported for playlists");
|
||||
}
|
||||
|
||||
var playlist = await metadataService.GetPlaylistAsync(provider, externalId);
|
||||
if (playlist == null)
|
||||
{
|
||||
_logger.LogWarning("Playlist not found: {PlaylistId}", playlistId);
|
||||
return;
|
||||
}
|
||||
|
||||
var tracks = await metadataService.GetPlaylistTracksAsync(provider, externalId);
|
||||
if (tracks == null || tracks.Count == 0)
|
||||
{
|
||||
_logger.LogWarning("No tracks found in playlist {PlaylistId}", playlistId);
|
||||
return;
|
||||
}
|
||||
|
||||
_logger.LogInformation("Found {TrackCount} tracks in playlist '{PlaylistName}'", tracks.Count, playlist.Name);
|
||||
|
||||
// Get the appropriate download service for this provider
|
||||
var downloadService = _downloadServices.FirstOrDefault(s =>
|
||||
s.GetType().Name.Contains(provider, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
if (downloadService == null)
|
||||
{
|
||||
_logger.LogError("No download service found for provider '{Provider}'", provider);
|
||||
return;
|
||||
}
|
||||
|
||||
// Download all tracks (M3U will be created once at the end)
|
||||
var downloadedTracks = new List<(Song Song, string LocalPath)>();
|
||||
|
||||
foreach (var track in tracks)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrEmpty(track.ExternalId))
|
||||
{
|
||||
_logger.LogWarning("Track has no external ID, skipping: {Title}", track.Title);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Add track to playlist cache BEFORE downloading
|
||||
// This marks it as part of a full playlist download, so AddTrackToM3UAsync will skip real-time updates
|
||||
var trackId = $"ext-{provider}-{track.ExternalId}";
|
||||
AddTrackToPlaylistCache(trackId, playlistId);
|
||||
|
||||
_logger.LogInformation("Downloading track '{Artist} - {Title}'", track.Artist, track.Title);
|
||||
var localPath = await downloadService.DownloadSongAsync(provider, track.ExternalId, cancellationToken);
|
||||
|
||||
downloadedTracks.Add((track, localPath));
|
||||
_logger.LogDebug("Downloaded: {Path}", localPath);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Failed to download track '{Artist} - {Title}'", track.Artist, track.Title);
|
||||
}
|
||||
}
|
||||
|
||||
if (downloadedTracks.Count == 0)
|
||||
{
|
||||
_logger.LogWarning("No tracks were successfully downloaded for playlist '{PlaylistName}'", playlist.Name);
|
||||
return;
|
||||
}
|
||||
|
||||
// Create M3U file ONCE at the end with all downloaded tracks
|
||||
await CreateM3UPlaylistAsync(playlist.Name, downloadedTracks);
|
||||
|
||||
_logger.LogInformation("Playlist download completed: {DownloadedCount}/{TotalCount} tracks for '{PlaylistName}'",
|
||||
downloadedTracks.Count, tracks.Count, playlist.Name);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to download playlist {PlaylistId}", playlistId);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates an M3U playlist file with relative paths to downloaded tracks
|
||||
/// </summary>
|
||||
private async Task CreateM3UPlaylistAsync(string playlistName, List<(Song Song, string LocalPath)> tracks)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Sanitize playlist name for file system
|
||||
var fileName = PathHelper.SanitizeFileName(playlistName) + ".m3u";
|
||||
var playlistPath = Path.Combine(_playlistDirectory, fileName);
|
||||
|
||||
var m3uContent = new StringBuilder();
|
||||
m3uContent.AppendLine("#EXTM3U");
|
||||
|
||||
foreach (var (song, localPath) in tracks)
|
||||
{
|
||||
// Calculate relative path from playlist directory to track
|
||||
var relativePath = Path.GetRelativePath(_playlistDirectory, localPath);
|
||||
|
||||
// Convert backslashes to forward slashes for M3U compatibility
|
||||
relativePath = relativePath.Replace('\\', '/');
|
||||
|
||||
// Add EXTINF line with duration and artist - title
|
||||
var duration = song.Duration ?? 0;
|
||||
m3uContent.AppendLine($"#EXTINF:{duration},{song.Artist} - {song.Title}");
|
||||
m3uContent.AppendLine(relativePath);
|
||||
}
|
||||
|
||||
await IOFile.WriteAllTextAsync(playlistPath, m3uContent.ToString());
|
||||
_logger.LogInformation("Created M3U playlist: {Path}", playlistPath);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to create M3U playlist for '{PlaylistName}'", playlistName);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a track to an existing M3U playlist or creates it if it doesn't exist.
|
||||
/// Called when individual tracks are played/downloaded (NOT during full playlist download).
|
||||
/// The M3U is rebuilt in the correct playlist order each time.
|
||||
/// </summary>
|
||||
/// <param name="isFullPlaylistDownload">If true, skips M3U update (will be done at the end by DownloadFullPlaylistAsync)</param>
|
||||
public async Task AddTrackToM3UAsync(string playlistId, Song track, string localPath, bool isFullPlaylistDownload = false)
|
||||
{
|
||||
// Skip real-time updates during full playlist download (M3U will be created once at the end)
|
||||
if (isFullPlaylistDownload)
|
||||
{
|
||||
_logger.LogDebug("Skipping M3U update for track {TrackId} (full playlist download in progress)", track.Id);
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// Get playlist metadata to get the name and track order
|
||||
if (!PlaylistIdHelper.IsExternalPlaylist(playlistId))
|
||||
{
|
||||
_logger.LogWarning("Invalid playlist ID format: {PlaylistId}", playlistId);
|
||||
return;
|
||||
}
|
||||
|
||||
var (provider, externalId) = PlaylistIdHelper.ParsePlaylistId(playlistId);
|
||||
|
||||
var metadataService = GetMetadataServiceForProvider(provider);
|
||||
if (metadataService == null)
|
||||
{
|
||||
_logger.LogWarning("No metadata service found for provider '{Provider}'", provider);
|
||||
return;
|
||||
}
|
||||
|
||||
var playlist = await metadataService.GetPlaylistAsync(provider, externalId);
|
||||
if (playlist == null)
|
||||
{
|
||||
_logger.LogWarning("Playlist not found: {PlaylistId}", playlistId);
|
||||
return;
|
||||
}
|
||||
|
||||
// Get all tracks from the playlist to maintain order
|
||||
var allPlaylistTracks = await metadataService.GetPlaylistTracksAsync(provider, externalId);
|
||||
if (allPlaylistTracks == null || allPlaylistTracks.Count == 0)
|
||||
{
|
||||
_logger.LogWarning("No tracks found in playlist: {PlaylistId}", playlistId);
|
||||
return;
|
||||
}
|
||||
|
||||
// Sanitize playlist name for file system
|
||||
var fileName = PathHelper.SanitizeFileName(playlist.Name) + ".m3u";
|
||||
var playlistPath = Path.Combine(_playlistDirectory, fileName);
|
||||
|
||||
// Build M3U content in the correct order
|
||||
var m3uContent = new StringBuilder();
|
||||
m3uContent.AppendLine("#EXTM3U");
|
||||
|
||||
int addedCount = 0;
|
||||
foreach (var playlistTrack in allPlaylistTracks)
|
||||
{
|
||||
// Check if this track has been downloaded locally
|
||||
string? trackLocalPath = null;
|
||||
|
||||
// If this is the track we just downloaded
|
||||
if (playlistTrack.Id == track.Id)
|
||||
{
|
||||
trackLocalPath = localPath;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Check if track was previously downloaded
|
||||
var trackProvider = playlistTrack.ExternalProvider;
|
||||
var trackExternalId = playlistTrack.ExternalId;
|
||||
|
||||
if (!string.IsNullOrEmpty(trackProvider) && !string.IsNullOrEmpty(trackExternalId))
|
||||
{
|
||||
// Try to find the download service for this provider
|
||||
var downloadService = _downloadServices.FirstOrDefault(s =>
|
||||
s.GetType().Name.Contains(trackProvider, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
if (downloadService != null)
|
||||
{
|
||||
trackLocalPath = await downloadService.GetLocalPathIfExistsAsync(trackProvider, trackExternalId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If track is downloaded, add it to M3U
|
||||
if (!string.IsNullOrEmpty(trackLocalPath) && IOFile.Exists(trackLocalPath))
|
||||
{
|
||||
var relativePath = Path.GetRelativePath(_playlistDirectory, trackLocalPath);
|
||||
relativePath = relativePath.Replace('\\', '/');
|
||||
|
||||
var duration = playlistTrack.Duration ?? 0;
|
||||
m3uContent.AppendLine($"#EXTINF:{duration},{playlistTrack.Artist} - {playlistTrack.Title}");
|
||||
m3uContent.AppendLine(relativePath);
|
||||
addedCount++;
|
||||
}
|
||||
}
|
||||
|
||||
// Write the M3U file (overwrites existing)
|
||||
await IOFile.WriteAllTextAsync(playlistPath, m3uContent.ToString());
|
||||
_logger.LogInformation("Updated M3U playlist '{PlaylistName}' with {Count} tracks (in correct order)",
|
||||
playlist.Name, addedCount);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to add track to M3U playlist");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Background task to clean up expired cache entries every minute
|
||||
/// </summary>
|
||||
private async Task CleanupExpiredCacheEntriesAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
while (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
try
|
||||
{
|
||||
await Task.Delay(TimeSpan.FromMinutes(1), cancellationToken);
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
var expiredKeys = _trackPlaylistCache
|
||||
.Where(kvp => kvp.Value.ExpiresAt <= now)
|
||||
.Select(kvp => kvp.Key)
|
||||
.ToList();
|
||||
|
||||
foreach (var key in expiredKeys)
|
||||
{
|
||||
_trackPlaylistCache.TryRemove(key, out _);
|
||||
}
|
||||
|
||||
if (expiredKeys.Count > 0)
|
||||
{
|
||||
_logger.LogDebug("Cleaned up {Count} expired playlist cache entries", expiredKeys.Count);
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
// Expected when cancellation is requested
|
||||
break;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Error during playlist cache cleanup");
|
||||
}
|
||||
}
|
||||
|
||||
_logger.LogInformation("Playlist cache cleanup task stopped");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stops the background cleanup task
|
||||
/// </summary>
|
||||
public async Task StopCleanupAsync()
|
||||
{
|
||||
_cleanupCancellationTokenSource.Cancel();
|
||||
await _cleanupTask;
|
||||
_cleanupCancellationTokenSource.Dispose();
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Xml.Linq;
|
||||
using octo_fiesta.Models.Search;
|
||||
using octo_fiesta.Models.Subsonic;
|
||||
|
||||
namespace octo_fiesta.Services.Subsonic;
|
||||
|
||||
@@ -97,22 +98,23 @@ public class SubsonicModelMapper
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Merges local search results with external search results, deduplicating by name.
|
||||
/// Merges local and external search results (songs, albums, artists, playlists).
|
||||
/// </summary>
|
||||
public (List<object> MergedSongs, List<object> MergedAlbums, List<object> MergedArtists) MergeSearchResults(
|
||||
List<object> localSongs,
|
||||
List<object> localAlbums,
|
||||
List<object> localArtists,
|
||||
SearchResult externalResult,
|
||||
List<ExternalPlaylist> externalPlaylists,
|
||||
bool isJson)
|
||||
{
|
||||
if (isJson)
|
||||
{
|
||||
return MergeSearchResultsJson(localSongs, localAlbums, localArtists, externalResult);
|
||||
return MergeSearchResultsJson(localSongs, localAlbums, localArtists, externalResult, externalPlaylists);
|
||||
}
|
||||
else
|
||||
{
|
||||
return MergeSearchResultsXml(localSongs, localAlbums, localArtists, externalResult);
|
||||
return MergeSearchResultsXml(localSongs, localAlbums, localArtists, externalResult, externalPlaylists);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,14 +122,17 @@ public class SubsonicModelMapper
|
||||
List<object> localSongs,
|
||||
List<object> localAlbums,
|
||||
List<object> localArtists,
|
||||
SearchResult externalResult)
|
||||
SearchResult externalResult,
|
||||
List<ExternalPlaylist> externalPlaylists)
|
||||
{
|
||||
var mergedSongs = localSongs
|
||||
.Concat(externalResult.Songs.Select(s => _responseBuilder.ConvertSongToJson(s)))
|
||||
.ToList();
|
||||
|
||||
// Merge albums with playlists (playlists appear as albums with genre "Playlist")
|
||||
var mergedAlbums = localAlbums
|
||||
.Concat(externalResult.Albums.Select(a => _responseBuilder.ConvertAlbumToJson(a)))
|
||||
.Concat(externalPlaylists.Select(p => ConvertPlaylistToAlbumJson(p)))
|
||||
.ToList();
|
||||
|
||||
// Deduplicate artists by name - prefer local artists over external ones
|
||||
@@ -157,7 +162,8 @@ public class SubsonicModelMapper
|
||||
List<object> localSongs,
|
||||
List<object> localAlbums,
|
||||
List<object> localArtists,
|
||||
SearchResult externalResult)
|
||||
SearchResult externalResult,
|
||||
List<ExternalPlaylist> externalPlaylists)
|
||||
{
|
||||
var ns = XNamespace.Get("http://subsonic.org/restapi");
|
||||
|
||||
@@ -196,6 +202,11 @@ public class SubsonicModelMapper
|
||||
{
|
||||
mergedAlbums.Add(_responseBuilder.ConvertAlbumToXml(album, ns));
|
||||
}
|
||||
// Add playlists as albums
|
||||
foreach (var playlist in externalPlaylists)
|
||||
{
|
||||
mergedAlbums.Add(ConvertPlaylistToAlbumXml(playlist, ns));
|
||||
}
|
||||
|
||||
// Songs
|
||||
var mergedSongs = new List<object>();
|
||||
@@ -211,4 +222,81 @@ public class SubsonicModelMapper
|
||||
|
||||
return (mergedSongs, mergedAlbums, mergedArtists);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts an ExternalPlaylist to a JSON object representing an album.
|
||||
/// Playlists are represented as albums with genre "Playlist" and artist "🎵 {Provider} {Curator}".
|
||||
/// </summary>
|
||||
private Dictionary<string, object> ConvertPlaylistToAlbumJson(ExternalPlaylist playlist)
|
||||
{
|
||||
var artistName = $"🎵 {char.ToUpper(playlist.Provider[0])}{playlist.Provider.Substring(1)}";
|
||||
if (!string.IsNullOrEmpty(playlist.CuratorName))
|
||||
{
|
||||
artistName += $" {playlist.CuratorName}";
|
||||
}
|
||||
|
||||
var artistId = $"curator-{playlist.Provider}-{playlist.CuratorName?.ToLowerInvariant().Replace(" ", "-") ?? "unknown"}";
|
||||
|
||||
var album = new Dictionary<string, object>
|
||||
{
|
||||
["id"] = playlist.Id,
|
||||
["name"] = playlist.Name,
|
||||
["artist"] = artistName,
|
||||
["artistId"] = artistId,
|
||||
["genre"] = "Playlist",
|
||||
["songCount"] = playlist.TrackCount,
|
||||
["duration"] = playlist.Duration
|
||||
};
|
||||
|
||||
if (playlist.CreatedDate.HasValue)
|
||||
{
|
||||
album["year"] = playlist.CreatedDate.Value.Year;
|
||||
album["created"] = playlist.CreatedDate.Value.ToString("yyyy-MM-ddTHH:mm:ss");
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(playlist.CoverUrl))
|
||||
{
|
||||
album["coverArt"] = playlist.Id;
|
||||
}
|
||||
|
||||
return album;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts an ExternalPlaylist to an XML element representing an album.
|
||||
/// Playlists are represented as albums with genre "Playlist" and artist "🎵 {Provider} {Curator}".
|
||||
/// </summary>
|
||||
private XElement ConvertPlaylistToAlbumXml(ExternalPlaylist playlist, XNamespace ns)
|
||||
{
|
||||
var artistName = $"🎵 {char.ToUpper(playlist.Provider[0])}{playlist.Provider.Substring(1)}";
|
||||
if (!string.IsNullOrEmpty(playlist.CuratorName))
|
||||
{
|
||||
artistName += $" {playlist.CuratorName}";
|
||||
}
|
||||
|
||||
var artistId = $"curator-{playlist.Provider}-{playlist.CuratorName?.ToLowerInvariant().Replace(" ", "-") ?? "unknown"}";
|
||||
|
||||
var album = new XElement(ns + "album",
|
||||
new XAttribute("id", playlist.Id),
|
||||
new XAttribute("name", playlist.Name),
|
||||
new XAttribute("artist", artistName),
|
||||
new XAttribute("artistId", artistId),
|
||||
new XAttribute("genre", "Playlist"),
|
||||
new XAttribute("songCount", playlist.TrackCount),
|
||||
new XAttribute("duration", playlist.Duration)
|
||||
);
|
||||
|
||||
if (playlist.CreatedDate.HasValue)
|
||||
{
|
||||
album.Add(new XAttribute("year", playlist.CreatedDate.Value.Year));
|
||||
album.Add(new XAttribute("created", playlist.CreatedDate.Value.ToString("yyyy-MM-ddTHH:mm:ss")));
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(playlist.CoverUrl))
|
||||
{
|
||||
album.Add(new XAttribute("coverArt", playlist.Id));
|
||||
}
|
||||
|
||||
return album;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Mvc;
|
||||
using System.Xml.Linq;
|
||||
using System.Text.Json;
|
||||
using octo_fiesta.Models.Domain;
|
||||
using octo_fiesta.Models.Subsonic;
|
||||
|
||||
namespace octo_fiesta.Services.Subsonic;
|
||||
|
||||
@@ -137,6 +138,81 @@ public class SubsonicResponseBuilder
|
||||
);
|
||||
return new ContentResult { Content = doc.ToString(), ContentType = "application/xml" };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a Subsonic response for a playlist represented as an album.
|
||||
/// Playlists appear as albums with genre "Playlist".
|
||||
/// </summary>
|
||||
public IActionResult CreatePlaylistAsAlbumResponse(string format, ExternalPlaylist playlist, List<Song> tracks)
|
||||
{
|
||||
var totalDuration = tracks.Sum(s => s.Duration ?? 0);
|
||||
|
||||
// Build artist name with emoji and curator
|
||||
var artistName = $"🎵 {char.ToUpper(playlist.Provider[0])}{playlist.Provider.Substring(1)}";
|
||||
if (!string.IsNullOrEmpty(playlist.CuratorName))
|
||||
{
|
||||
artistName += $" {playlist.CuratorName}";
|
||||
}
|
||||
|
||||
var artistId = $"curator-{playlist.Provider}-{playlist.CuratorName?.ToLowerInvariant().Replace(" ", "-") ?? "unknown"}";
|
||||
|
||||
if (format == "json")
|
||||
{
|
||||
return CreateJsonResponse(new
|
||||
{
|
||||
status = "ok",
|
||||
version = SubsonicVersion,
|
||||
album = new
|
||||
{
|
||||
id = playlist.Id,
|
||||
name = playlist.Name,
|
||||
artist = artistName,
|
||||
artistId = artistId,
|
||||
coverArt = playlist.Id,
|
||||
songCount = tracks.Count,
|
||||
duration = totalDuration,
|
||||
year = playlist.CreatedDate?.Year ?? 0,
|
||||
genre = "Playlist",
|
||||
isCompilation = false,
|
||||
created = playlist.CreatedDate?.ToString("yyyy-MM-ddTHH:mm:ss"),
|
||||
song = tracks.Select(s => ConvertSongToJson(s)).ToList()
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
var ns = XNamespace.Get(SubsonicNamespace);
|
||||
var albumElement = new XElement(ns + "album",
|
||||
new XAttribute("id", playlist.Id),
|
||||
new XAttribute("name", playlist.Name),
|
||||
new XAttribute("artist", artistName),
|
||||
new XAttribute("artistId", artistId),
|
||||
new XAttribute("songCount", tracks.Count),
|
||||
new XAttribute("duration", totalDuration),
|
||||
new XAttribute("genre", "Playlist"),
|
||||
new XAttribute("coverArt", playlist.Id)
|
||||
);
|
||||
|
||||
if (playlist.CreatedDate.HasValue)
|
||||
{
|
||||
albumElement.Add(new XAttribute("year", playlist.CreatedDate.Value.Year));
|
||||
albumElement.Add(new XAttribute("created", playlist.CreatedDate.Value.ToString("yyyy-MM-ddTHH:mm:ss")));
|
||||
}
|
||||
|
||||
// Add songs
|
||||
foreach (var song in tracks)
|
||||
{
|
||||
albumElement.Add(ConvertSongToXml(song, ns));
|
||||
}
|
||||
|
||||
var doc = new XDocument(
|
||||
new XElement(ns + "subsonic-response",
|
||||
new XAttribute("status", "ok"),
|
||||
new XAttribute("version", SubsonicVersion),
|
||||
albumElement
|
||||
)
|
||||
);
|
||||
return new ContentResult { Content = doc.ToString(), ContentType = "application/xml" };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a Subsonic response containing an artist with albums.
|
||||
|
||||
Reference in New Issue
Block a user