54 lines
No EOL
1.7 KiB
TypeScript
54 lines
No EOL
1.7 KiB
TypeScript
import Auth from "./src/helpers/auth";
|
|
import Utils from "./src/helpers/utils";
|
|
|
|
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)
|
|
|
|
const file = Bun.file(`downloaded/${trackId}.flac`)
|
|
|
|
if (await file.exists()) {
|
|
return new Response(file, {
|
|
headers: {
|
|
"Content-Type": "audio/flac",
|
|
"Content-Disposition": `attachment; filename="${trackId}.flac"`,
|
|
"Cache-Control": "public, max-age=31536000",
|
|
"ETag": trackId.toString(),
|
|
}
|
|
})
|
|
}
|
|
|
|
const { manifestMimeType, manifest } = await utils.fetchTrack(parseInt(id))
|
|
|
|
const audio = await utils.tagFlac(trackId, await utils.downloadFlac(manifestMimeType, manifest))
|
|
|
|
await Bun.write(`downloaded/${trackId}.flac`, audio)
|
|
|
|
return Response.json(audio, {
|
|
headers: {
|
|
"Content-Type": "audio/flac",
|
|
"Content-Disposition": `attachment; filename="${trackId}.flac"`,
|
|
"Cache-Control": "public, max-age=31536000",
|
|
"ETag": trackId.toString(),
|
|
}
|
|
})
|
|
},
|
|
"/tracks": async () => {
|
|
const tracks = await utils.fetchTracks();
|
|
|
|
return Response.json(tracks);
|
|
}
|
|
},
|
|
development: true
|
|
}) |