2024-09-16 15:29:09 +02:00
|
|
|
import { apiRoute, applyConfig } from "@/api";
|
|
|
|
|
import { createRoute } from "@hono/zod-openapi";
|
2024-12-30 16:18:28 +01:00
|
|
|
import type { ContentfulStatusCode, StatusCode } from "hono/utils/http-status";
|
2024-05-06 19:31:12 +02:00
|
|
|
import { z } from "zod";
|
2024-06-26 05:13:40 +02:00
|
|
|
import { config } from "~/packages/config-manager";
|
2024-09-16 15:29:09 +02:00
|
|
|
import { ErrorSchema } from "~/types/api";
|
2024-05-06 19:31:12 +02:00
|
|
|
|
|
|
|
|
export const meta = applyConfig({
|
|
|
|
|
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()),
|
|
|
|
|
}),
|
|
|
|
|
};
|
|
|
|
|
|
2024-09-16 15:29:09 +02:00
|
|
|
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(),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
400: {
|
|
|
|
|
description: "Invalid URL to proxy",
|
|
|
|
|
content: {
|
|
|
|
|
"application/json": {
|
|
|
|
|
schema: ErrorSchema,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2024-08-19 20:06:38 +02:00
|
|
|
export default apiRoute((app) =>
|
2024-09-16 15:29:09 +02:00
|
|
|
app.openapi(route, async (context) => {
|
|
|
|
|
const { id } = context.req.valid("param");
|
2024-05-06 19:31:12 +02:00
|
|
|
|
2024-09-16 15:29:09 +02:00
|
|
|
// Check if URL is valid
|
|
|
|
|
if (!URL.canParse(id)) {
|
|
|
|
|
return context.json(
|
|
|
|
|
{ error: "Invalid URL (it should be encoded as base64url" },
|
|
|
|
|
400,
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-05-06 19:31:12 +02:00
|
|
|
|
2024-09-16 15:29:09 +02:00
|
|
|
const media = await fetch(id, {
|
|
|
|
|
headers: {
|
|
|
|
|
"Accept-Encoding": "br",
|
|
|
|
|
},
|
|
|
|
|
// @ts-expect-error Proxy is a Bun-specific feature
|
|
|
|
|
proxy: config.http.proxy.address,
|
|
|
|
|
});
|
2024-06-06 03:56:40 +02:00
|
|
|
|
2024-09-16 15:29:09 +02:00
|
|
|
// 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");
|
|
|
|
|
}
|
2024-06-16 13:50:32 +02:00
|
|
|
|
2024-09-16 15:29:09 +02:00
|
|
|
const realFilename =
|
|
|
|
|
media.headers
|
|
|
|
|
.get("Content-Disposition")
|
|
|
|
|
?.match(/filename="(.+)"/)?.[1] || id.split("/").pop();
|
2024-06-06 06:07:22 +02:00
|
|
|
|
2024-12-30 16:18:28 +01:00
|
|
|
if (!media.body) {
|
|
|
|
|
return context.body(null, media.status as StatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return context.body(media.body, media.status as ContentfulStatusCode, {
|
2024-09-16 15:29:09 +02:00
|
|
|
"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;
|
|
|
|
|
}),
|
2024-08-19 20:06:38 +02:00
|
|
|
);
|