From 3a9d00dcdba25792b6112325563ad1f4d0705a38 Mon Sep 17 00:00:00 2001 From: Josh Patra Date: Fri, 6 Feb 2026 23:55:07 -0500 Subject: [PATCH] Fix downloads endpoint to only show kept files with debug logging - Downloads endpoint now ONLY shows /app/kept (favorited tracks) - Removed cache downloads from this endpoint (separate box needed) - Added debug logging to troubleshoot why kept files weren't showing - Logs directory existence and file count --- allstarr/Controllers/AdminController.cs | 119 +++++++++--------------- 1 file changed, 45 insertions(+), 74 deletions(-) diff --git a/allstarr/Controllers/AdminController.cs b/allstarr/Controllers/AdminController.cs index e15ec78..0085e8f 100644 --- a/allstarr/Controllers/AdminController.cs +++ b/allstarr/Controllers/AdminController.cs @@ -3324,108 +3324,79 @@ public class LinkPlaylistRequest /// /// GET /api/admin/downloads - /// Lists all downloaded files in the downloads directory AND kept folder + /// Lists all downloaded files in the KEPT folder only (favorited tracks) /// [HttpGet("downloads")] public IActionResult GetDownloads() { try { - var downloadPath = _configuration["Library:DownloadPath"] ?? "./downloads"; var keptPath = "/app/kept"; + _logger.LogInformation("📂 Checking kept folder: {Path}", keptPath); + _logger.LogInformation("📂 Directory exists: {Exists}", Directory.Exists(keptPath)); + + if (!Directory.Exists(keptPath)) + { + _logger.LogWarning("Kept folder does not exist: {Path}", keptPath); + return Ok(new { files = new List(), totalSize = 0, count = 0 }); + } + var files = new List(); long totalSize = 0; - // Recursively get all audio files from both locations + // Recursively get all audio files from kept folder var audioExtensions = new[] { ".flac", ".mp3", ".m4a", ".opus" }; - // Get files from downloads folder (cache) - if (Directory.Exists(downloadPath)) + var allFiles = Directory.GetFiles(keptPath, "*.*", SearchOption.AllDirectories) + .Where(f => audioExtensions.Contains(Path.GetExtension(f).ToLowerInvariant())) + .ToList(); + + _logger.LogInformation("📂 Found {Count} audio files in kept folder", allFiles.Count); + + foreach (var filePath in allFiles) { - var downloadFiles = Directory.GetFiles(downloadPath, "*.*", SearchOption.AllDirectories) - .Where(f => audioExtensions.Contains(Path.GetExtension(f).ToLowerInvariant())) - .ToList(); + _logger.LogDebug("📂 Processing file: {Path}", filePath); - foreach (var filePath in downloadFiles) + var fileInfo = new FileInfo(filePath); + var relativePath = Path.GetRelativePath(keptPath, filePath); + + // Parse artist/album/track from path structure + var parts = relativePath.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); + var artist = parts.Length > 0 ? parts[0] : ""; + var album = parts.Length > 1 ? parts[1] : ""; + var fileName = parts.Length > 2 ? parts[^1] : Path.GetFileName(filePath); + + files.Add(new { - var fileInfo = new FileInfo(filePath); - var relativePath = Path.GetRelativePath(downloadPath, filePath); - - // Parse artist/album/track from path structure - var parts = relativePath.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); - var artist = parts.Length > 0 ? parts[0] : ""; - var album = parts.Length > 1 ? parts[1] : ""; - var fileName = parts.Length > 2 ? parts[^1] : Path.GetFileName(filePath); - - files.Add(new - { - path = relativePath, - fullPath = filePath, - artist, - album, - fileName, - size = fileInfo.Length, - sizeFormatted = FormatFileSize(fileInfo.Length), - lastModified = fileInfo.LastWriteTimeUtc, - extension = fileInfo.Extension, - location = "cache" - }); - - totalSize += fileInfo.Length; - } + path = relativePath, + fullPath = filePath, + artist, + album, + fileName, + size = fileInfo.Length, + sizeFormatted = FormatFileSize(fileInfo.Length), + lastModified = fileInfo.LastWriteTimeUtc, + extension = fileInfo.Extension + }); + + totalSize += fileInfo.Length; } - // Get files from kept folder (favorited) - if (Directory.Exists(keptPath)) - { - var keptFiles = Directory.GetFiles(keptPath, "*.*", SearchOption.AllDirectories) - .Where(f => audioExtensions.Contains(Path.GetExtension(f).ToLowerInvariant())) - .ToList(); - - foreach (var filePath in keptFiles) - { - var fileInfo = new FileInfo(filePath); - var relativePath = Path.GetRelativePath(keptPath, filePath); - - // Parse artist/album/track from path structure - var parts = relativePath.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); - var artist = parts.Length > 0 ? parts[0] : ""; - var album = parts.Length > 1 ? parts[1] : ""; - var fileName = parts.Length > 2 ? parts[^1] : Path.GetFileName(filePath); - - files.Add(new - { - path = relativePath, - fullPath = filePath, - artist, - album, - fileName, - size = fileInfo.Length, - sizeFormatted = FormatFileSize(fileInfo.Length), - lastModified = fileInfo.LastWriteTimeUtc, - extension = fileInfo.Extension, - location = "kept" - }); - - totalSize += fileInfo.Length; - } - } + _logger.LogInformation("📂 Returning {Count} kept files, total size: {Size}", files.Count, FormatFileSize(totalSize)); return Ok(new { files = files.OrderBy(f => ((dynamic)f).artist).ThenBy(f => ((dynamic)f).album).ThenBy(f => ((dynamic)f).fileName), totalSize, totalSizeFormatted = FormatFileSize(totalSize), - count = files.Count, - cacheCount = files.Count(f => ((dynamic)f).location == "cache"), - keptCount = files.Count(f => ((dynamic)f).location == "kept") + count = files.Count }); } catch (Exception ex) { - _logger.LogError(ex, "Failed to list downloads"); - return StatusCode(500, new { error = "Failed to list downloads" }); + _logger.LogError(ex, "Failed to list kept downloads"); + return StatusCode(500, new { error = "Failed to list kept downloads" }); } }