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({
|
Bun.serve({
|
||||||
routes: {
|
routes: {
|
||||||
"/track": async (req: { url: string }) => {
|
"/api/track/:id": async req => {
|
||||||
const url = new URL(req.url)
|
const trackId = parseInt(req.params.id)
|
||||||
|
|
||||||
const id = url.searchParams.get("id")
|
|
||||||
|
|
||||||
if (!id) {
|
|
||||||
return new Response("Missing id", { status: 400 })
|
|
||||||
}
|
|
||||||
|
|
||||||
const trackId = parseInt(id)
|
|
||||||
|
|
||||||
const file = Bun.file(`downloaded/${trackId}.flac`)
|
const file = Bun.file(`downloaded/${trackId}.flac`)
|
||||||
|
|
||||||
if (await file.exists()) {
|
if (await file.exists()) {
|
||||||
return new Response(file, {
|
return new Response(await file.arrayBuffer(), {
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "audio/flac",
|
"Content-Type": "audio/flac",
|
||||||
"Content-Disposition": `attachment; filename="${trackId}.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))
|
const audio = await utils.tagFlac(trackId, await utils.downloadFlac(manifestMimeType, manifest))
|
||||||
|
|
||||||
await Bun.write(`downloaded/${trackId}.flac`, audio)
|
await Bun.write(`downloaded/${trackId}.flac`, audio)
|
||||||
|
|
||||||
return Response.json(audio, {
|
return new Response(audio, {
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "audio/flac",
|
"Content-Type": "audio/flac",
|
||||||
"Content-Disposition": `attachment; filename="${trackId}.flac"`,
|
"Content-Disposition": `attachment; filename="${trackId}.flac"`,
|
||||||
|
@ -45,15 +37,22 @@ Bun.serve({
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
"/tracks": async () => {
|
"/api/@me/tracks": async () => {
|
||||||
const tracks = await utils.fetchTracks();
|
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
|
development: true
|
||||||
})
|
})
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
const tracks = await utils.fetchTracks();
|
const tracks = await utils.fetchTracks();
|
||||||
|
|
||||||
for await (const track of tracks.items) {
|
for await (const track of tracks.items) {
|
||||||
|
@ -81,3 +80,4 @@ for await (const track of tracks.items) {
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("Done")
|
console.log("Done")
|
||||||
|
*/
|
Loading…
Add table
Reference in a new issue