From 5124e3839993382233d7a87a12a08f3da027dbcd Mon Sep 17 00:00:00 2001 From: seth Date: Sun, 20 Apr 2025 02:48:43 -0400 Subject: [PATCH] Refactor track API routes to improve parameter handling and response formatting --- index.ts | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/index.ts b/index.ts index 2599ebe..c61339c 100644 --- a/index.ts +++ b/index.ts @@ -6,21 +6,13 @@ const utils = new Utils(await Auth()); Bun.serve({ routes: { - "/track": async (req: { url: string }) => { - const url = new URL(req.url) - - const id = url.searchParams.get("id") - - if (!id) { - return new Response("Missing id", { status: 400 }) - } - - const trackId = parseInt(id) + "/api/track/:id": async req => { + const trackId = parseInt(req.params.id) const file = Bun.file(`downloaded/${trackId}.flac`) if (await file.exists()) { - return new Response(file, { + return new Response(await file.arrayBuffer(), { headers: { "Content-Type": "audio/flac", "Content-Disposition": `attachment; filename="${trackId}.flac"`, @@ -30,13 +22,13 @@ Bun.serve({ }) } - const { manifestMimeType, manifest } = await utils.fetchTrack(parseInt(id)) + const { manifestMimeType, manifest } = await utils.fetchTrack(trackId) const audio = await utils.tagFlac(trackId, await utils.downloadFlac(manifestMimeType, manifest)) await Bun.write(`downloaded/${trackId}.flac`, audio) - return Response.json(audio, { + return new Response(audio, { headers: { "Content-Type": "audio/flac", "Content-Disposition": `attachment; filename="${trackId}.flac"`, @@ -45,15 +37,22 @@ Bun.serve({ } }) }, - "/tracks": async () => { + "/api/@me/tracks": async () => { const tracks = await utils.fetchTracks(); - return Response.json(tracks); + return new Response(Bun.gzipSync(JSON.stringify(tracks)), { + headers: { + "Content-Type": "application/json", + "Content-Encoding": "gzip", + } + }) } }, development: true }) +/* + const tracks = await utils.fetchTracks(); for await (const track of tracks.items) { @@ -80,4 +79,5 @@ for await (const track of tracks.items) { } } -console.log("Done") \ No newline at end of file +console.log("Done") +*/ \ No newline at end of file