From 4aa0812342816567e55f16c2339a149628cee467 Mon Sep 17 00:00:00 2001 From: Yufan Sheng Date: Thu, 28 Nov 2024 13:56:27 +0800 Subject: [PATCH] feat: move the generated images to a static like url endpoint. --- src/actions/index.ts | 2 +- src/components/comment/CommentItem.astro | 2 +- src/components/sidebar/OwspaceCalendar.astro | 23 +++--------- src/helpers/tools.ts | 4 +++ src/pages/{ => images}/avatar/[hash].webp.ts | 8 ++--- .../images/calendar/[year]/[time].jpg.ts | 36 +++++++++++++++++++ 6 files changed, 48 insertions(+), 27 deletions(-) rename src/pages/{ => images}/avatar/[hash].webp.ts (89%) create mode 100644 src/pages/images/calendar/[year]/[time].jpg.ts diff --git a/src/actions/index.ts b/src/actions/index.ts index 97ebfd4..826cc40 100644 --- a/src/actions/index.ts +++ b/src/actions/index.ts @@ -50,7 +50,7 @@ const normalActions = { handler: async ({ email }) => { const id = await queryUserId(email); const hash = id === null ? encodedEmail(email) : `${id}`; - return { avatar: urlJoin(import.meta.env.SITE, 'avatar', `${hash}.webp`) }; + return { avatar: urlJoin(import.meta.env.SITE, 'images/avatar', `${hash}.webp`) }; }, }), }; diff --git a/src/components/comment/CommentItem.astro b/src/components/comment/CommentItem.astro index 7227bfc..677f355 100644 --- a/src/components/comment/CommentItem.astro +++ b/src/components/comment/CommentItem.astro @@ -18,7 +18,7 @@ const { comment, depth, pending } = Astro.props;
{comment.nick} => { - const now = DateTime.now().setZone(options.settings.timeZone); - const link = `https://img.owspace.com/Public/uploads/Download/${now.year}/${now.toFormat('LLdd')}.jpg`; - const response = await fetch(link, { - referrer: '', - }); - - if (!response.ok) { - console.error(`Failed to fetch image ${link}`); - return ''; - } - - const encodedImage = ( - await sharp(await response.arrayBuffer()) - .extract({ width: 1096, height: 1550, left: 90, top: 110 }) - .toBuffer() - ).toString('base64'); - return `data:image/jpeg;base64,${encodedImage}`; + const now = DateTime.now().setZone(options.settings.timeZone).setLocale(options.settings.locale); + return urlJoin(import.meta.env.SITE, 'images/calendar', `${now.year}`, `${now.toFormat('LLdd')}.jpg`); }; const calendarImage = !options.settings.sidebar.calendar ? '' : await loadCalendarImage(); @@ -30,7 +15,7 @@ const calendarImage = !options.settings.sidebar.calendar ? '' : await loadCalend calendarImage !== '' && (
单向历
- +
) } diff --git a/src/helpers/tools.ts b/src/helpers/tools.ts index fe7f40a..0d0cc51 100644 --- a/src/helpers/tools.ts +++ b/src/helpers/tools.ts @@ -22,3 +22,7 @@ export const urlJoin = (base: string, ...paths: string[]): string => { export const encodedEmail = (email: string): string => crypto.createHash('md5').update(email.trim().toLowerCase()).digest('hex'); + +export const isNumeric = (str: string): boolean => { + return /^-?\d+$/.test(str); +}; diff --git a/src/pages/avatar/[hash].webp.ts b/src/pages/images/avatar/[hash].webp.ts similarity index 89% rename from src/pages/avatar/[hash].webp.ts rename to src/pages/images/avatar/[hash].webp.ts index 9219f56..7f4a006 100644 --- a/src/pages/avatar/[hash].webp.ts +++ b/src/pages/images/avatar/[hash].webp.ts @@ -1,5 +1,5 @@ import { queryEmail } from '@/helpers/db/query'; -import { encodedEmail, urlJoin } from '@/helpers/tools'; +import { encodedEmail, isNumeric, urlJoin } from '@/helpers/tools'; import options from '@/options'; import type { APIRoute, ValidRedirectStatus } from 'astro'; @@ -7,10 +7,6 @@ const defaultAvatar = (): string => { return urlJoin(options.assetsPrefix(), '/images/default-avatar.png'); }; -function isNumeric(str: string) { - return /^-?\d+$/.test(str); -} - const avatarImage = async ( hash: string, redirect: (path: string, status?: ValidRedirectStatus) => Response, @@ -29,7 +25,7 @@ const avatarImage = async ( return new Response(Buffer.from(await resp.arrayBuffer()), { headers: { 'Content-Type': 'image/webp', - 'Cache-control': 'max-age=604800', + 'Cache-Control': 'public, max-age=604800', }, }); }; diff --git a/src/pages/images/calendar/[year]/[time].jpg.ts b/src/pages/images/calendar/[year]/[time].jpg.ts new file mode 100644 index 0000000..199881e --- /dev/null +++ b/src/pages/images/calendar/[year]/[time].jpg.ts @@ -0,0 +1,36 @@ +import type { APIRoute } from 'astro'; +import sharp from 'sharp'; + +const loadCalendarImage = async (year: string, time: string): Promise => { + const link = `https://img.owspace.com/Public/uploads/Download/${year}/${time}.jpg`; + const response = await fetch(link, { + referrer: '', + }); + + if (!response.ok) { + console.error(`Failed to fetch image ${link}`); + return response; + } + + const croppedImage = await sharp(await response.arrayBuffer()) + .extract({ width: 1096, height: 1550, left: 90, top: 110 }) + .toBuffer(); + + return new Response(croppedImage, { + headers: { + 'Content-Type': 'image/jpeg', + 'Cache-Control': 'public, max-age=604800', + }, + }); +}; + +const timeRegex = /\d{4}/; + +export const GET: APIRoute = async ({ params, redirect }) => { + const { year, time } = params; + if (year === undefined || !timeRegex.test(year) || time === undefined || !timeRegex.test(time)) { + return redirect('/404'); + } + + return loadCalendarImage(year, time); +};