mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 13:59:16 +01:00
refactor(api): ♻️ More OpenAPI refactoring work
This commit is contained in:
parent
6d9e385a04
commit
5aa1c4e625
35 changed files with 4883 additions and 1815 deletions
|
|
@ -1,8 +1,9 @@
|
|||
import { apiRoute, applyConfig, handleZodError } from "@/api";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { apiRoute, applyConfig } from "@/api";
|
||||
import { createRoute } from "@hono/zod-openapi";
|
||||
import type { StatusCode } from "hono/utils/http-status";
|
||||
import { z } from "zod";
|
||||
import { config } from "~/packages/config-manager";
|
||||
import { ErrorSchema } from "~/types/api";
|
||||
|
||||
export const meta = applyConfig({
|
||||
allowedMethods: ["GET"],
|
||||
|
|
@ -24,54 +25,76 @@ export const schemas = {
|
|||
}),
|
||||
};
|
||||
|
||||
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",
|
||||
const route = createRoute({
|
||||
method: "get",
|
||||
path: "/media/proxy/{id}",
|
||||
summary: "Proxy media through the server",
|
||||
request: {
|
||||
params: schemas.param,
|
||||
},
|
||||
responses: {
|
||||
200: {
|
||||
description: "Media",
|
||||
content: {
|
||||
"*": {
|
||||
schema: z.any(),
|
||||
},
|
||||
// @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 context.newResponse(media.body, media.status as StatusCode, {
|
||||
"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}"`,
|
||||
});
|
||||
},
|
||||
},
|
||||
),
|
||||
400: {
|
||||
description: "Invalid URL to proxy",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: ErrorSchema,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export default apiRoute((app) =>
|
||||
app.openapi(route, 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 context.newResponse(media.body, media.status as StatusCode, {
|
||||
"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}"`,
|
||||
// biome-ignore lint/suspicious/noExplicitAny: Hono doesn't type this response so this has a TS error
|
||||
}) as any;
|
||||
}),
|
||||
);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue