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
|
|
@ -13,6 +13,7 @@ const app = new Elysia({prefix: "/api"})
|
|||
.use(import("./routes/auth"))
|
||||
.use(import("./routes/post"))
|
||||
.use(import("./routes/pixiv"))
|
||||
.use(import("./routes/proxy"))
|
||||
|
||||
.listen(config.server.port)
|
||||
;
|
||||
|
|
|
|||
|
|
@ -16,16 +16,12 @@ async function checkTweetData(url: string, selected: Array<boolean>) {
|
|||
}
|
||||
|
||||
async function fetchTweetData(url: string) {
|
||||
const apiUrl = `https://api.fxtwitter.com/${url.replace(/^https?:\/\/(www\.)?(x\.com|twitter\.com|fxtwitter\.com|fixupx\.com|vxwitter\.com)\//, "")}`;
|
||||
const apiUrl = `https://api.fxtwitter.com/${url.replace(/^https?:\/\/(www\.)?(x\.com|twitter\.com|fxtwitter\.com|fixupx\.com|vxtwitter\.com)\//, "")}`;
|
||||
const response = await fetch(apiUrl);
|
||||
if (response.ok) {
|
||||
// const dataText = await response.text();
|
||||
// console.log("Raw API response:", dataText);
|
||||
const data = await response.json();
|
||||
return data;
|
||||
} else {
|
||||
throw new Error(`Failed to fetch tweet data: ${response.status} ${response.statusText}`);
|
||||
}
|
||||
// fxtwitter returns JSON even for non-200 responses (e.g. {"code":404,"tweet":null})
|
||||
// so we always parse JSON and let the caller handle missing tweet
|
||||
const data = await response.json();
|
||||
return data;
|
||||
}
|
||||
|
||||
export { checkTweetData, fetchTweetData };
|
||||
|
|
@ -610,11 +610,18 @@ export default new Elysia({ prefix: "/post" })
|
|||
const tweetData = await fetchTweetData(body.url);
|
||||
let savedCount = 0;
|
||||
let failedCount = 0;
|
||||
const savedIds: string[] = [];
|
||||
|
||||
if (!tweetData.tweet) {
|
||||
const apiCode = tweetData.code ?? "unknown";
|
||||
console.warn(`[Upload failed] fxtwitter returned no tweet, code=${apiCode} url=${body.url}`);
|
||||
return status(404, uploadError(`트윗을 찾을 수 없거나 비공개 계정입니다. (code: ${apiCode})`));
|
||||
}
|
||||
|
||||
if (tweetData.tweet) {
|
||||
const media = tweetData.tweet.media.all || tweetData.tweet.media.photos || [];
|
||||
const media = tweetData.tweet.media?.all || tweetData.tweet.media?.photos || [];
|
||||
if (media.length > 0) {
|
||||
const hasExplicitSelection = body.selected.length > 0;
|
||||
const savedIds: string[] = [];
|
||||
for (const [index, mediaItem] of media.entries()) {
|
||||
const isSelected = hasExplicitSelection
|
||||
? body.selected[index] === true
|
||||
|
|
|
|||
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