2024-08-19 20:06:38 +02:00
|
|
|
import { apiRoute, applyConfig, handleZodError } from "@/api";
|
2024-05-06 19:31:12 +02:00
|
|
|
import { zValidator } from "@hono/zod-validator";
|
2024-08-28 17:01:56 +02:00
|
|
|
import type { 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-05-06 19:31:12 +02:00
|
|
|
|
|
|
|
|
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()),
|
|
|
|
|
}),
|
|
|
|
|
};
|
|
|
|
|
|
2024-08-19 20:06:38 +02:00
|
|
|
export default apiRoute((app) =>
|
2024-05-06 19:31:12 +02:00
|
|
|
app.on(
|
|
|
|
|
meta.allowedMethods,
|
|
|
|
|
meta.route,
|
|
|
|
|
zValidator("param", schemas.param, handleZodError),
|
|
|
|
|
async (context) => {
|
|
|
|
|
const { id } = context.req.valid("param");
|
|
|
|
|
|
|
|
|
|
// Check if URL is valid
|
2024-06-13 04:26:43 +02:00
|
|
|
if (!URL.canParse(id)) {
|
2024-08-19 21:03:59 +02:00
|
|
|
return context.json(
|
|
|
|
|
{ error: "Invalid URL (it should be encoded as base64url" },
|
2024-05-06 19:31:12 +02:00
|
|
|
400,
|
|
|
|
|
);
|
2024-06-13 04:26:43 +02:00
|
|
|
}
|
2024-05-06 19:31:12 +02:00
|
|
|
|
2024-06-06 04:07:56 +02:00
|
|
|
const media = await fetch(id, {
|
2024-05-06 19:50:50 +02:00
|
|
|
headers: {
|
2024-06-06 04:26:19 +02:00
|
|
|
"Accept-Encoding": "br",
|
2024-05-06 19:50:50 +02:00
|
|
|
},
|
2024-08-26 18:15:14 +02:00
|
|
|
// @ts-expect-error Proxy is a Bun-specific feature
|
2024-06-26 05:13:40 +02:00
|
|
|
proxy: config.http.proxy.address,
|
2024-05-06 19:31:12 +02:00
|
|
|
});
|
2024-06-06 03:56:40 +02:00
|
|
|
|
2024-06-16 13:50:32 +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-06 06:07:22 +02:00
|
|
|
const realFilename =
|
|
|
|
|
media.headers
|
|
|
|
|
.get("Content-Disposition")
|
|
|
|
|
?.match(/filename="(.+)"/)?.[1] || id.split("/").pop();
|
|
|
|
|
|
2024-08-28 17:01:56 +02:00
|
|
|
return context.newResponse(media.body, media.status as StatusCode, {
|
2024-06-06 04:07:56 +02:00
|
|
|
"Content-Type":
|
|
|
|
|
media.headers.get("Content-Type") ||
|
|
|
|
|
"application/octet-stream",
|
|
|
|
|
"Content-Length": media.headers.get("Content-Length") || "0",
|
|
|
|
|
"Content-Security-Policy": "",
|
2024-06-06 04:35:25 +02:00
|
|
|
"Content-Encoding": "",
|
2024-06-06 06:07:22 +02:00
|
|
|
// Real filename
|
|
|
|
|
"Content-Disposition": `inline; filename="${realFilename}"`,
|
2024-06-06 04:07:56 +02:00
|
|
|
});
|
2024-05-06 19:31:12 +02:00
|
|
|
},
|
2024-08-19 20:06:38 +02:00
|
|
|
),
|
|
|
|
|
);
|