This commit is contained in:
wont-stream 2025-04-08 02:07:54 -04:00
parent 54dfc1d822
commit 67dce9ddc0
9 changed files with 457 additions and 1 deletions

54
index.ts Normal file
View file

@ -0,0 +1,54 @@
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
})