Fix getAlbum endpoint to return only tracks for specific album instead of all tracks

This commit is contained in:
2025-12-07 23:11:58 +00:00
parent aa720323b5
commit c7b66ce7ab

View File

@@ -176,25 +176,46 @@ router.get('/getAlbum.view', authenticateSubsonic, async (req, res) => {
try { try {
const { id } = req.query; const { id } = req.query;
// Get album info from first track // Get album info - we need to find tracks that match this album ID
const firstTrack = await get(` // The album ID is a hash of "album-{album_name}-{album_artist}"
SELECT album, album_artist, year, genre // So we need to get all tracks and find which album matches this ID
FROM tracks const allTracks = await all(`SELECT DISTINCT album, album_artist FROM tracks`);
LIMIT 1
`);
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(` const tracks = await all(`
SELECT * FROM tracks SELECT * FROM tracks
WHERE album = ? AND album_artist = ?
ORDER BY disc_number, track_number, title 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');
}
let albumXml = `<album id="${id}" name="${escapeXml(firstTrack?.album || 'Unknown')}" `; const firstTrack = tracks[0];
albumXml += `artist="${escapeXml(firstTrack?.album_artist || 'Unknown')}" `; const coverArt = firstTrack.artwork_path ? id : '';
let albumXml = `<album id="${id}" name="${escapeXml(firstTrack.album || 'Unknown')}" `;
albumXml += `artist="${escapeXml(firstTrack.album_artist || 'Unknown')}" `;
albumXml += `coverArt="${coverArt}" songCount="${tracks.length}" `; albumXml += `coverArt="${coverArt}" songCount="${tracks.length}" `;
albumXml += `duration="${Math.floor(tracks.reduce((sum, t) => sum + (t.duration || 0), 0))}" `; albumXml += `duration="${Math.floor(tracks.reduce((sum, t) => 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 => { tracks.forEach(track => {
albumXml += buildSongXml(track, req); albumXml += buildSongXml(track, req);
@@ -204,6 +225,7 @@ router.get('/getAlbum.view', authenticateSubsonic, async (req, res) => {
sendSubsonicResponse(res, albumXml); sendSubsonicResponse(res, albumXml);
} catch (error) { } catch (error) {
console.error('Error in getAlbum:', error);
sendSubsonicError(res, 0, error.message); sendSubsonicError(res, 0, error.message);
} }
}); });