Files
patriothacks/api/blob/[id].ts
T

24 lines
748 B
TypeScript

import type { IncomingMessage, ServerResponse } from "node:http";
import { getBlob } from "../_lib/blobStore.ts";
import { sendJson } from "../_lib/httpUtils.ts";
export default async function handler(req: IncomingMessage, res: ServerResponse): Promise<void> {
if (req.method !== "GET") {
sendJson(res, 405, { error: "method not allowed, use GET" });
return;
}
const id = new URL(req.url ?? "", "http://localhost").pathname.split("/").filter(Boolean).pop() ?? "";
const blob = await getBlob(id);
if (!blob) {
sendJson(res, 404, { error: `blob ${id} not found` });
return;
}
res.writeHead(200, {
"Content-Type": blob.contentType,
"Content-Length": String(blob.data.byteLength),
});
res.end(blob.data);
}