mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 22:09:16 +01:00
fix(api): 🚚 Rename "id" to "hash"
This commit is contained in:
parent
2ec7e512e0
commit
731fc9847c
1 changed files with 4 additions and 4 deletions
64
server/api/media/:hash/:name/index.ts
Normal file
64
server/api/media/:hash/:name/index.ts
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
import { applyConfig, handleZodError } from "@/api";
|
||||
import { errorResponse, response } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import type { Hono } from "hono";
|
||||
import { z } from "zod";
|
||||
|
||||
export const meta = applyConfig({
|
||||
allowedMethods: ["GET"],
|
||||
route: "/media/:hash/:name",
|
||||
ratelimits: {
|
||||
max: 100,
|
||||
duration: 60,
|
||||
},
|
||||
auth: {
|
||||
required: false,
|
||||
},
|
||||
});
|
||||
|
||||
export const schemas = {
|
||||
param: z.object({
|
||||
hash: z.string(),
|
||||
name: z.string(),
|
||||
}),
|
||||
header: z.object({
|
||||
range: z.string().optional().default(""),
|
||||
}),
|
||||
};
|
||||
|
||||
export default (app: Hono) =>
|
||||
app.on(
|
||||
meta.allowedMethods,
|
||||
meta.route,
|
||||
zValidator("param", schemas.param, handleZodError),
|
||||
zValidator("header", schemas.header, handleZodError),
|
||||
async (context) => {
|
||||
const { hash, name } = context.req.valid("param");
|
||||
const { range } = context.req.valid("header");
|
||||
|
||||
// parse `Range` header
|
||||
const [start = 0, end = Number.POSITIVE_INFINITY] = (
|
||||
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/${hash}/${name}`);
|
||||
|
||||
const buffer = await file.arrayBuffer();
|
||||
|
||||
if (!(await file.exists())) {
|
||||
return errorResponse("File not found", 404);
|
||||
}
|
||||
|
||||
// Can't directly copy file into Response because this crashes Bun for now
|
||||
return response(buffer, 200, {
|
||||
"Content-Type": file.type || "application/octet-stream",
|
||||
"Content-Length": `${file.size - start}`,
|
||||
"Content-Range": `bytes ${start}-${end}/${file.size}`,
|
||||
});
|
||||
},
|
||||
);
|
||||
Loading…
Add table
Add a link
Reference in a new issue