mirror of
https://github.com/versia-pub/server.git
synced 2025-12-06 08:28:19 +01:00
fix: Fix crash on serving local media
This commit is contained in:
parent
c7c11d8d2b
commit
fdf32c0b6c
|
|
@ -22,11 +22,28 @@ export default async (
|
|||
|
||||
const id = matchedRoute.params.id;
|
||||
|
||||
// parse `Range` header
|
||||
const [start = 0, end = Infinity] = (
|
||||
(req.headers.get("Range") || "")
|
||||
.split("=") // ["Range: bytes", "0-100"]
|
||||
.at(-1) || ""
|
||||
) // "0-100"
|
||||
.split("-") // ["0", "100"]
|
||||
.map(Number); // [0, 100]
|
||||
|
||||
// Serve file from filesystem
|
||||
const file = Bun.file(`./uploads/${id}`);
|
||||
|
||||
const buffer = await file.arrayBuffer();
|
||||
|
||||
if (!(await file.exists())) return errorResponse("File not found", 404);
|
||||
|
||||
// @ts-expect-error Bun allows this
|
||||
return new Response(file);
|
||||
// Can't directly copy file into Response because this crashes Bun for now
|
||||
return new Response(buffer, {
|
||||
headers: {
|
||||
"Content-Type": file.type || "application/octet-stream",
|
||||
"Content-Length": `${file.size - start}`,
|
||||
"Content-Range": `bytes ${start}-${end}/${file.size}`,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in a new issue