From 4bdb5c539d247d18b496cf1d1b188ca554cfabc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B2=E6=82=A6?= Date: Wed, 14 Jan 2026 16:19:52 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=94=B9=E8=BF=9B=E9=9F=B3=E4=B9=90API?= =?UTF-8?q?=E4=BB=A3=E7=90=86=E7=9A=84=E5=9B=BE=E7=89=87=E7=AB=AF=E7=82=B9?= =?UTF-8?q?=E5=A4=84=E7=90=86=EF=BC=8C=E5=B0=86JSON=E5=93=8D=E5=BA=94?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2=E4=B8=BA=E9=87=8D=E5=AE=9A=E5=90=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sync-server/server.js | 58 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 50 insertions(+), 8 deletions(-) diff --git a/sync-server/server.js b/sync-server/server.js index 1047649..10a3fe8 100644 --- a/sync-server/server.js +++ b/sync-server/server.js @@ -310,7 +310,8 @@ function downloadFile(url, dest) { // --- Music API Proxy Function --- function proxyRequest(targetUrl, req, res) { - const parsedTarget = url.parse(targetUrl); + const parsedTarget = url.parse(targetUrl, true); + const isPicRequest = parsedTarget.query && parsedTarget.query.type === 'pic'; const options = { hostname: parsedTarget.hostname, @@ -325,6 +326,53 @@ function proxyRequest(targetUrl, req, res) { }; const proxyReq = https.request(options, (proxyRes) => { + const passThrough = () => { + const headers = { ...proxyRes.headers }; + delete headers['content-encoding']; + delete headers['transfer-encoding']; + + res.writeHead(proxyRes.statusCode, headers); + proxyRes.pipe(res); + }; + + // pic endpoint now returns JSON, turn it into a redirect for + if (isPicRequest) { + if (proxyRes.statusCode >= 300 && proxyRes.statusCode < 400 && proxyRes.headers.location) { + res.writeHead(302, { Location: proxyRes.headers.location }); + proxyRes.resume(); + res.end(); + return; + } + + const contentType = proxyRes.headers['content-type'] || ''; + if (contentType.startsWith('image/')) { + passThrough(); + return; + } + + let body = ''; + proxyRes.setEncoding('utf8'); + proxyRes.on('data', chunk => body += chunk); + proxyRes.on('end', () => { + try { + const parsedBody = JSON.parse(body); + if (parsedBody && parsedBody.url) { + res.writeHead(302, { Location: parsedBody.url }); + res.end(); + return; + } + } catch (e) { + // fall through + } + + res.writeHead(proxyRes.statusCode || 200, { + 'Content-Type': proxyRes.headers['content-type'] || 'application/json' + }); + res.end(body); + }); + return; + } + // Handle redirects if (proxyRes.statusCode >= 300 && proxyRes.statusCode < 400 && proxyRes.headers.location) { // For redirects, return the redirect URL to client @@ -333,13 +381,7 @@ function proxyRequest(targetUrl, req, res) { return; } - // Copy response headers (except problematic ones) - const headers = { ...proxyRes.headers }; - delete headers['content-encoding']; - delete headers['transfer-encoding']; - - res.writeHead(proxyRes.statusCode, headers); - proxyRes.pipe(res); + passThrough(); }); proxyReq.on('error', (e) => {