From c7b66ce7ab9028a95fcb606d3c49e9f6bfc758f4 Mon Sep 17 00:00:00 2001 From: Paul Bailey Date: Sun, 7 Dec 2025 23:11:58 +0000 Subject: [PATCH] Fix getAlbum endpoint to return only tracks for specific album instead of all tracks --- src/routes/subsonic.js | 44 +++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/src/routes/subsonic.js b/src/routes/subsonic.js index 54b88f4..327f741 100644 --- a/src/routes/subsonic.js +++ b/src/routes/subsonic.js @@ -176,25 +176,46 @@ router.get('/getAlbum.view', authenticateSubsonic, async (req, res) => { try { const { id } = req.query; - // Get album info from first track - const firstTrack = await get(` - SELECT album, album_artist, year, genre - FROM tracks - LIMIT 1 - `); + // Get album info - we need to find tracks that match this album ID + // The album ID is a hash of "album-{album_name}-{album_artist}" + // So we need to get all tracks and find which album matches this ID + const allTracks = await all(`SELECT DISTINCT album, album_artist FROM tracks`); + + let targetAlbum = null; + let targetArtist = null; + + for (const track of allTracks) { + const testId = crypto.createHash('md5').update(`album-${track.album}-${track.album_artist}`).digest('hex'); + if (testId === id) { + targetAlbum = track.album; + targetArtist = track.album_artist; + break; + } + } + + if (!targetAlbum) { + return sendSubsonicError(res, 70, 'Album not found'); + } + // Get tracks for this specific album const tracks = await all(` SELECT * FROM tracks + WHERE album = ? AND album_artist = ? ORDER BY disc_number, track_number, title - `); + `, [targetAlbum, targetArtist]); - const coverArt = tracks[0]?.artwork_path ? id : ''; + if (tracks.length === 0) { + return sendSubsonicError(res, 70, 'Album has no tracks'); + } + + const firstTrack = tracks[0]; + const coverArt = firstTrack.artwork_path ? id : ''; - let albumXml = ` sum + (t.duration || 0), 0))}" `; - albumXml += `created="2024-01-01T00:00:00" year="${firstTrack?.year || ''}" genre="${escapeXml(firstTrack?.genre || '')}">\n`; + albumXml += `created="2024-01-01T00:00:00" year="${firstTrack.year || ''}" genre="${escapeXml(firstTrack.genre || '')}">\n`; tracks.forEach(track => { albumXml += buildSongXml(track, req); @@ -204,6 +225,7 @@ router.get('/getAlbum.view', authenticateSubsonic, async (req, res) => { sendSubsonicResponse(res, albumXml); } catch (error) { + console.error('Error in getAlbum:', error); sendSubsonicError(res, 0, error.message); } });