feat: add image proxy route and integrate it into media handling for improved Twitter content fetching

This commit is contained in:
암냥 2026-05-23 22:31:02 +09:00
commit 907fc4491d
No known key found for this signature in database
6 changed files with 87 additions and 12 deletions

View file

@ -0,0 +1,45 @@
import { Elysia, t } from "elysia";
const ALLOWED_HOSTS = [
"pbs.twimg.com",
"video.twimg.com",
"ton.twimg.com",
"abs.twimg.com",
];
export default new Elysia({ prefix: "/proxy" })
.get("/image", async ({ query, status, set }) => {
const targetUrl = query.url;
let parsed: URL;
try {
parsed = new URL(targetUrl);
} catch {
return status(400, "Invalid URL");
}
if (!ALLOWED_HOSTS.includes(parsed.hostname)) {
return status(403, `Host not allowed: ${parsed.hostname}`);
}
const response = await fetch(targetUrl, {
headers: {
"User-Agent": "Mozilla/5.0 (compatible; bot/1.0)",
"Referer": "https://twitter.com/",
},
});
if (!response.ok) {
return status(response.status as any, `Upstream error: ${response.status}`);
}
const contentType = response.headers.get("content-type") ?? "application/octet-stream";
set.headers["Content-Type"] = contentType;
set.headers["Cache-Control"] = "public, max-age=86400, immutable";
return response;
}, {
query: t.Object({
url: t.String(),
}),
});