feat: add cache control headers and implement random post retrieval endpoint

This commit is contained in:
암냥 2026-04-18 05:46:35 +09:00
commit 55af0549e7
No known key found for this signature in database

View file

@ -122,6 +122,12 @@ export default new Elysia({ prefix: "/post" })
secret: config.auth.jwt_secret, secret: config.auth.jwt_secret,
}), }),
) )
.onAfterHandle(({ set }) => {
set.headers["Cache-Control"] = "no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0, s-maxage=0";
set.headers["Pragma"] = "no-cache";
set.headers["Expires"] = "0";
set.headers["Surrogate-Control"] = "no-store";
})
.get("/total", async ({ query }) => { .get("/total", async ({ query }) => {
const filterTags = normalizeQueryTags(query.tags); const filterTags = normalizeQueryTags(query.tags);
const filter = filterTags.length > 0 const filter = filterTags.length > 0
@ -339,15 +345,6 @@ export default new Elysia({ prefix: "/post" })
return status(400, "No media found in the Pixiv artwork."); return status(400, "No media found in the Pixiv artwork.");
} }
// const normalizedTags = normalizeTags(
// body.tag
// ?? (Array.isArray(pixivData.tags)
// ? pixivData.tags
// .filter((tag: unknown): tag is string => typeof tag === "string")
// .map((tag: string) => tag.replace(/^#/, ""))
// : ["미분류"]),
// );
const normalizedTags = normalizeTags(body.tag ?? ["미분류"]); const normalizedTags = normalizeTags(body.tag ?? ["미분류"]);
let savedCount = 0; let savedCount = 0;
@ -475,7 +472,6 @@ export default new Elysia({ prefix: "/post" })
const media = tweetData.tweet.media.photos || []; const media = tweetData.tweet.media.photos || [];
if (media.length > 0) { if (media.length > 0) {
const mediaUrls = media.map((m: any) => m.url); const mediaUrls = media.map((m: any) => m.url);
// Upload to S3
const hasExplicitSelection = body.selected.length > 0; const hasExplicitSelection = body.selected.length > 0;
for (const [index, url] of mediaUrls.entries()) { for (const [index, url] of mediaUrls.entries()) {
const isSelected = hasExplicitSelection const isSelected = hasExplicitSelection
@ -633,3 +629,21 @@ export default new Elysia({ prefix: "/post" })
ids: t.Array(t.String({ minLength: 1 }), { minItems: 1 }), ids: t.Array(t.String({ minLength: 1 }), { minItems: 1 }),
}), }),
}) })
.get("/random", async ({ status }) => {
const count = await MediaUpload.countDocuments();
console.log(`Total posts count: ${count}`);
if (count === 0) {
return status(404, "포스트를 찾을 수 없습니다.");
}
const randomIndex = Math.floor(Math.random() * count);
console.log(`Random index: ${randomIndex}`);
const randomPost = await MediaUpload.findOne().skip(randomIndex);
console.log(`Random post: ${randomPost?._id}`);
if (!randomPost) {
return status(404, "포스트를 찾을 수 없습니다.");
}
return fetch(randomPost.mediaUrl, { cache: "no-store" });
});