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
This commit is contained in:
2026-02-06 23:55:07 -05:00
parent 2389b80733
commit 3a9d00dcdb

View File

@@ -3324,67 +3324,40 @@ public class LinkPlaylistRequest
/// <summary> /// <summary>
/// GET /api/admin/downloads /// 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)
/// </summary> /// </summary>
[HttpGet("downloads")] [HttpGet("downloads")]
public IActionResult GetDownloads() public IActionResult GetDownloads()
{ {
try try
{ {
var downloadPath = _configuration["Library:DownloadPath"] ?? "./downloads";
var keptPath = "/app/kept"; 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<object>(), totalSize = 0, count = 0 });
}
var files = new List<object>(); var files = new List<object>();
long totalSize = 0; 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" }; var audioExtensions = new[] { ".flac", ".mp3", ".m4a", ".opus" };
// Get files from downloads folder (cache) var allFiles = Directory.GetFiles(keptPath, "*.*", SearchOption.AllDirectories)
if (Directory.Exists(downloadPath))
{
var downloadFiles = Directory.GetFiles(downloadPath, "*.*", SearchOption.AllDirectories)
.Where(f => audioExtensions.Contains(Path.GetExtension(f).ToLowerInvariant())) .Where(f => audioExtensions.Contains(Path.GetExtension(f).ToLowerInvariant()))
.ToList(); .ToList();
foreach (var filePath in downloadFiles) _logger.LogInformation("📂 Found {Count} audio files in kept folder", allFiles.Count);
foreach (var filePath in allFiles)
{ {
var fileInfo = new FileInfo(filePath); _logger.LogDebug("📂 Processing file: {Path}", 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;
}
}
// 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 fileInfo = new FileInfo(filePath);
var relativePath = Path.GetRelativePath(keptPath, filePath); var relativePath = Path.GetRelativePath(keptPath, filePath);
@@ -3404,28 +3377,26 @@ public class LinkPlaylistRequest
size = fileInfo.Length, size = fileInfo.Length,
sizeFormatted = FormatFileSize(fileInfo.Length), sizeFormatted = FormatFileSize(fileInfo.Length),
lastModified = fileInfo.LastWriteTimeUtc, lastModified = fileInfo.LastWriteTimeUtc,
extension = fileInfo.Extension, extension = fileInfo.Extension
location = "kept"
}); });
totalSize += fileInfo.Length; totalSize += fileInfo.Length;
} }
}
_logger.LogInformation("📂 Returning {Count} kept files, total size: {Size}", files.Count, FormatFileSize(totalSize));
return Ok(new return Ok(new
{ {
files = files.OrderBy(f => ((dynamic)f).artist).ThenBy(f => ((dynamic)f).album).ThenBy(f => ((dynamic)f).fileName), files = files.OrderBy(f => ((dynamic)f).artist).ThenBy(f => ((dynamic)f).album).ThenBy(f => ((dynamic)f).fileName),
totalSize, totalSize,
totalSizeFormatted = FormatFileSize(totalSize), totalSizeFormatted = FormatFileSize(totalSize),
count = files.Count, count = files.Count
cacheCount = files.Count(f => ((dynamic)f).location == "cache"),
keptCount = files.Count(f => ((dynamic)f).location == "kept")
}); });
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "Failed to list downloads"); _logger.LogError(ex, "Failed to list kept downloads");
return StatusCode(500, new { error = "Failed to list downloads" }); return StatusCode(500, new { error = "Failed to list kept downloads" });
} }
} }