Refactor track API routes to improve parameter handling and response formatting
This commit is contained in:
parent
c471925ebc
commit
5124e38399
1 changed files with 16 additions and 16 deletions
30
index.ts
30
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) {
|
||||
|
@ -81,3 +80,4 @@ for await (const track of tracks.items) {
|
|||
}
|
||||
|
||||
console.log("Done")
|
||||
*/
|
Loading…
Add table
Reference in a new issue