diff --git a/apps/backend/src/routes/post.ts b/apps/backend/src/routes/post.ts index 6d3a7c7..02af917 100644 --- a/apps/backend/src/routes/post.ts +++ b/apps/backend/src/routes/post.ts @@ -122,6 +122,12 @@ export default new Elysia({ prefix: "/post" }) 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 }) => { const filterTags = normalizeQueryTags(query.tags); 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."); } - // 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 ?? ["미분류"]); let savedCount = 0; @@ -475,7 +472,6 @@ export default new Elysia({ prefix: "/post" }) const media = tweetData.tweet.media.photos || []; if (media.length > 0) { const mediaUrls = media.map((m: any) => m.url); - // Upload to S3 const hasExplicitSelection = body.selected.length > 0; for (const [index, url] of mediaUrls.entries()) { const isSelected = hasExplicitSelection @@ -633,3 +629,21 @@ export default new Elysia({ prefix: "/post" }) 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" }); + });