Files
Vocaluxe/VocaluxeSongServer/Library/CLibraryIndex.cs
Simon DiesenreiterandClaude Opus 5.5 1e65864c8f Song server: search and download songs, mobile web UI
- Sources: "free" (Creative Commons songs with media from
  github.com/UltraStar-Deluxe/songs) and "usdb" (usdb.animux.de with an
  account; audio/video via yt-dlp from the song's #VIDEO hints or YouTube
  links in the comments, covers from fanart.tv/YouTube)
- Aggregated search (all sources in parallel, in-library flag), download
  queue into <library>/Downloads/<source>/<Artist - Title>, then rescan
- Web UI in wwwroot (plain JS, no build): search, downloads with progress,
  library; phone first, dark mode, installable
- Docker image with yt-dlp, ffmpeg and deno; library mounted writable
- Tests for matching, USDB parsing and song rewriting

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 23:15:48 +01:00

109 lines
4.1 KiB
C#

using Microsoft.Extensions.Options;
namespace VocaluxeSongServer.Library
{
public sealed class CLibraryOptions
{
/// <summary>Root folder of the song library (in Docker: the mounted share, default /songs).</summary>
public string Path { get; set; } = "/songs";
/// <summary>Minutes between automatic rescans. 0 = only at startup and via POST /api/v1/library/rescan.</summary>
public int ScanIntervalMinutes { get; set; } = 10;
/// <summary>Folder below the library root for downloaded songs (one subfolder per source).</summary>
public string DownloadFolder { get; set; } = "Downloads";
/// <summary>If set, every /api request needs the header X-Api-Key with this value.</summary>
public string ApiKey { get; set; } = "";
}
/// <summary>Holds the current library snapshot and rescans it in the background.</summary>
public sealed class CLibraryIndex : BackgroundService
{
private readonly CLibraryScanner _Scanner;
private readonly CLibraryOptions _Options;
private readonly ILogger<CLibraryIndex> _Logger;
private readonly SemaphoreSlim _ScanLock = new(1, 1);
private readonly SemaphoreSlim _RescanRequested = new(0, 1);
private volatile CLibrarySnapshot? _Current;
public CLibraryIndex(CLibraryScanner scanner, IOptions<CLibraryOptions> options, ILogger<CLibraryIndex> logger)
{
_Scanner = scanner;
_Options = options.Value;
_Logger = logger;
}
/// <summary>The last completed scan, or null while the first scan is running.</summary>
public CLibrarySnapshot? Current => _Current;
public string Root => System.IO.Path.GetFullPath(_Options.Path);
/// <summary>Asks for a rescan as soon as possible (returns immediately).</summary>
public void RequestRescan()
{
if (_RescanRequested.CurrentCount == 0)
{
try
{
_RescanRequested.Release();
}
catch (SemaphoreFullException)
{
// Already requested.
}
}
}
public async Task ScanNowAsync(CancellationToken ct = default)
{
await _ScanLock.WaitAsync(ct);
try
{
if (!Directory.Exists(Root))
{
_Logger.LogError("Library folder {Root} does not exist", Root);
return;
}
var started = DateTime.UtcNow;
var snapshot = await Task.Run(() => _Scanner.Scan(Root), ct);
var changed = _Current?.Dto.Version != snapshot.Dto.Version;
_Current = snapshot;
_Logger.LogInformation("Scanned {Root}: {Songs} songs, {Files} files in {Seconds:0.0} s{Changed}", Root,
snapshot.Dto.Songs.Count, snapshot.Dto.Files.Count, (DateTime.UtcNow - started).TotalSeconds, changed ? "" : " (unchanged)");
}
finally
{
_ScanLock.Release();
}
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
try
{
await ScanNowAsync(stoppingToken);
}
catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
{
return;
}
catch (Exception e)
{
_Logger.LogError(e, "Library scan failed");
}
var interval = _Options.ScanIntervalMinutes > 0 ? TimeSpan.FromMinutes(_Options.ScanIntervalMinutes) : Timeout.InfiniteTimeSpan;
try
{
await _RescanRequested.WaitAsync(interval, stoppingToken);
}
catch (OperationCanceledException)
{
return;
}
}
}
}
}