From a2443479993b7de0578fbef2cbb913ba2bf5df23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B2=E6=82=A6?= Date: Tue, 6 Jan 2026 16:22:48 +0800 Subject: [PATCH] Modified processSong to check for res.statusCode === 302. If a redirect is encountered, the Location header is extracted and treated as the direct download URL for the music file. This aligns the code with the API behavior which redirects to the actual file location instead of returning a JSON response. --- sync-server/server.js | 45 ++++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/sync-server/server.js b/sync-server/server.js index 5956984..606c0f5 100644 --- a/sync-server/server.js +++ b/sync-server/server.js @@ -160,6 +160,32 @@ function processSong(song) { const apiUrl = `https://music-dl.sayqz.com/api/?source=${source}&id=${song.id}&type=url&br=flac`; https.get(apiUrl, (res) => { + const handleDownload = (url) => { + let ext = 'mp3'; + if (url.includes('.flac')) ext = 'flac'; + else if (url.includes('.m4a')) ext = 'm4a'; + else if (url.includes('.ogg')) ext = 'ogg'; + else if (url.includes('.wav')) ext = 'wav'; + + const dest = path.join(MUSIC_DIR, `${baseName}.${ext}`); + downloadFile(url, dest) + .then(() => { + console.log(`[Sync] Downloaded: ${baseName}.${ext}`); + resolve(); + }) + .catch(err => { + console.error(`[Sync] Download failed for ${song.name}:`, err.message); + resolve(); + }); + }; + + // Handle 302 Redirect (Standard API behavior) + if (res.statusCode === 302 && res.headers.location) { + res.resume(); + handleDownload(res.headers.location); + return; + } + if (res.statusCode !== 200) { console.error(`[Sync] API Request Failed for ${song.name}: Status ${res.statusCode}`); res.resume(); // Consume response data to free up memory @@ -179,24 +205,7 @@ function processSong(song) { resolve(); return; } - - // Determine extension - let ext = 'mp3'; - if (url.includes('.flac')) ext = 'flac'; - else if (url.includes('.m4a')) ext = 'm4a'; - else if (url.includes('.ogg')) ext = 'ogg'; - else if (url.includes('.wav')) ext = 'wav'; - - const dest = path.join(MUSIC_DIR, `${baseName}.${ext}`); - downloadFile(url, dest) - .then(() => { - console.log(`[Sync] Downloaded: ${baseName}.${ext}`); - resolve(); - }) - .catch(err => { - console.error(`[Sync] Download failed for ${song.name}:`, err.message); - resolve(); - }); + handleDownload(url); } catch (e) { console.error('[Sync] API Parse Error:', e); console.error('[Sync] Raw Response:', data);