feat: add image proxy route and integrate it into media handling for improved Twitter content fetching
This commit is contained in:
parent
051dbac5bf
commit
907fc4491d
6 changed files with 87 additions and 12 deletions
45
apps/backend/src/routes/proxy.ts
Normal file
45
apps/backend/src/routes/proxy.ts
Normal 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(),
|
||||
}),
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue