mirror of
https://github.com/SoPat712/allstarr.git
synced 2026-02-09 23:55:10 -05:00
feat: add cache-only storage mode for streaming without library persistence
This commit is contained in:
@@ -23,6 +23,7 @@ public abstract class BaseDownloadService : IDownloadService
|
||||
protected readonly ILogger Logger;
|
||||
|
||||
protected readonly string DownloadPath;
|
||||
protected readonly string CachePath;
|
||||
|
||||
protected readonly Dictionary<string, DownloadInfo> ActiveDownloads = new();
|
||||
protected readonly SemaphoreSlim DownloadLock = new(1, 1);
|
||||
@@ -46,11 +47,17 @@ public abstract class BaseDownloadService : IDownloadService
|
||||
Logger = logger;
|
||||
|
||||
DownloadPath = configuration["Library:DownloadPath"] ?? "./downloads";
|
||||
CachePath = Path.Combine(Path.GetTempPath(), "octo-fiesta-cache");
|
||||
|
||||
if (!Directory.Exists(DownloadPath))
|
||||
{
|
||||
Directory.CreateDirectory(DownloadPath);
|
||||
}
|
||||
|
||||
if (!Directory.Exists(CachePath))
|
||||
{
|
||||
Directory.CreateDirectory(CachePath);
|
||||
}
|
||||
}
|
||||
|
||||
#region IDownloadService Implementation
|
||||
@@ -62,7 +69,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);
|
||||
}
|
||||
|
||||
@@ -130,13 +137,29 @@ 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))
|
||||
// Check if already downloaded (skip for cache mode as we want to check cache folder)
|
||||
if (!isCache)
|
||||
{
|
||||
Logger.LogInformation("Song already downloaded: {Path}", existingPath);
|
||||
return existingPath;
|
||||
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
|
||||
@@ -185,30 +208,39 @@ public abstract class BaseDownloadService : IDownloadService
|
||||
downloadInfo.CompletedAt = DateTime.UtcNow;
|
||||
|
||||
song.LocalPath = localPath;
|
||||
await LocalLibraryService.RegisterDownloadedSongAsync(song, localPath);
|
||||
|
||||
// Trigger a Subsonic library rescan (with debounce)
|
||||
_ = Task.Run(async () =>
|
||||
// Only register and scan if NOT in cache mode
|
||||
if (!isCache)
|
||||
{
|
||||
try
|
||||
await LocalLibraryService.RegisterDownloadedSongAsync(song, localPath);
|
||||
|
||||
// Trigger a Subsonic library rescan (with debounce)
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
await LocalLibraryService.TriggerLibraryScanAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
try
|
||||
{
|
||||
await LocalLibraryService.TriggerLibraryScanAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogWarning(ex, "Failed to trigger library scan after download");
|
||||
}
|
||||
});
|
||||
|
||||
// If download mode is Album and triggering is enabled, start background download of remaining tracks
|
||||
if (triggerAlbumDownload && SubsonicSettings.DownloadMode == DownloadMode.Album && !string.IsNullOrEmpty(song.AlbumId))
|
||||
{
|
||||
Logger.LogWarning(ex, "Failed to trigger library scan after download");
|
||||
var albumExternalId = ExtractExternalIdFromAlbumId(song.AlbumId);
|
||||
if (!string.IsNullOrEmpty(albumExternalId))
|
||||
{
|
||||
Logger.LogInformation("Download mode is Album, triggering background download for album {AlbumId}", albumExternalId);
|
||||
DownloadRemainingAlbumTracksInBackground(externalProvider, albumExternalId, externalId);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// If download mode is Album and triggering is enabled, start background download of remaining tracks
|
||||
if (triggerAlbumDownload && SubsonicSettings.DownloadMode == DownloadMode.Album && !string.IsNullOrEmpty(song.AlbumId))
|
||||
}
|
||||
else
|
||||
{
|
||||
var albumExternalId = ExtractExternalIdFromAlbumId(song.AlbumId);
|
||||
if (!string.IsNullOrEmpty(albumExternalId))
|
||||
{
|
||||
Logger.LogInformation("Download mode is Album, triggering background download for album {AlbumId}", albumExternalId);
|
||||
DownloadRemainingAlbumTracksInBackground(externalProvider, albumExternalId, externalId);
|
||||
}
|
||||
Logger.LogInformation("Cache mode: skipping library registration and scan");
|
||||
}
|
||||
|
||||
Logger.LogInformation("Download completed: {Path}", localPath);
|
||||
@@ -401,5 +433,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 = Path.Combine(Path.GetTempPath(), "octo-fiesta-cache");
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -109,7 +109,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)!;
|
||||
|
||||
@@ -108,7 +108,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);
|
||||
|
||||
Reference in New Issue
Block a user