mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 05:49:16 +01:00
refactor(api): 🚚 Use api/ for API routes instead of server/api/
This commit is contained in:
parent
dfc0bf4595
commit
3c1b330d4b
143 changed files with 5 additions and 5 deletions
77
api/media/proxy/:id.ts
Normal file
77
api/media/proxy/:id.ts
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
import { apiRoute, applyConfig, handleZodError } from "@/api";
|
||||
import { response } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { z } from "zod";
|
||||
import { config } from "~/packages/config-manager";
|
||||
|
||||
export const meta = applyConfig({
|
||||
allowedMethods: ["GET"],
|
||||
route: "/media/proxy/:id",
|
||||
ratelimits: {
|
||||
max: 100,
|
||||
duration: 60,
|
||||
},
|
||||
auth: {
|
||||
required: false,
|
||||
},
|
||||
});
|
||||
|
||||
export const schemas = {
|
||||
param: z.object({
|
||||
id: z
|
||||
.string()
|
||||
.transform((val) => Buffer.from(val, "base64url").toString()),
|
||||
}),
|
||||
};
|
||||
|
||||
export default apiRoute((app) =>
|
||||
app.on(
|
||||
meta.allowedMethods,
|
||||
meta.route,
|
||||
zValidator("param", schemas.param, handleZodError),
|
||||
async (context) => {
|
||||
const { id } = context.req.valid("param");
|
||||
|
||||
// Check if URL is valid
|
||||
if (!URL.canParse(id)) {
|
||||
return context.json(
|
||||
{ error: "Invalid URL (it should be encoded as base64url" },
|
||||
400,
|
||||
);
|
||||
}
|
||||
|
||||
const media = await fetch(id, {
|
||||
headers: {
|
||||
"Accept-Encoding": "br",
|
||||
},
|
||||
// @ts-expect-error Proxy is a Bun-specific feature
|
||||
proxy: config.http.proxy.address,
|
||||
});
|
||||
|
||||
// Check if file extension ends in svg or svg
|
||||
// Cloudflare R2 serves those as application/xml
|
||||
if (
|
||||
media.headers.get("Content-Type") === "application/xml" &&
|
||||
id.endsWith(".svg")
|
||||
) {
|
||||
media.headers.set("Content-Type", "image/svg+xml");
|
||||
}
|
||||
|
||||
const realFilename =
|
||||
media.headers
|
||||
.get("Content-Disposition")
|
||||
?.match(/filename="(.+)"/)?.[1] || id.split("/").pop();
|
||||
|
||||
return response(media.body, media.status, {
|
||||
"Content-Type":
|
||||
media.headers.get("Content-Type") ||
|
||||
"application/octet-stream",
|
||||
"Content-Length": media.headers.get("Content-Length") || "0",
|
||||
"Content-Security-Policy": "",
|
||||
"Content-Encoding": "",
|
||||
// Real filename
|
||||
"Content-Disposition": `inline; filename="${realFilename}"`,
|
||||
});
|
||||
},
|
||||
),
|
||||
);
|
||||
Loading…
Add table
Add a link
Reference in a new issue