diff options
| author | Yuval Adam <_@yuv.al> | 2025-09-24 14:34:35 +0200 |
|---|---|---|
| committer | Yuval Adam <_@yuv.al> | 2025-09-24 14:34:35 +0200 |
| commit | a75acdb10d0fdcca1d4c286073fce0d07eca6e3e (patch) | |
| tree | 89313a48538e2721b9fac38baf10e8c738bcba88 /src/pages/imgs.ts | |
| parent | d2a33bd7967602a00662c965a32ce0ae590b18de (diff) | |
Move /imgs/ path
Diffstat (limited to 'src/pages/imgs.ts')
| -rw-r--r-- | src/pages/imgs.ts | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/src/pages/imgs.ts b/src/pages/imgs.ts new file mode 100644 index 0000000..378d36d --- /dev/null +++ b/src/pages/imgs.ts @@ -0,0 +1,76 @@ +import type { APIRoute } from 'astro'; + +export const prerender = false; + +interface CloudflareEnv { + IMGS_BUCKET: R2Bucket; +} + +export const GET: APIRoute = async ({ locals }) => { + try { + const env = (locals as any).runtime?.env; + + if (!env?.IMGS_BUCKET) { + return new Response('R2 bucket not configured', { status: 500 }); + } + + const bucket = env.IMGS_BUCKET; + + // Get the latest images from yesterday onwards + const yesterday = new Date(); + yesterday.setDate(yesterday.getDate() - 1); + const yesterdayStr = yesterday.toISOString().slice(0, 10).replace(/-/g, ''); + + const objects = await bucket.list({ + prefix: 'imgs/', + startAfter: `imgs/${yesterdayStr}` + }); + + // Filter for 280.png files and get the latest 10 + const imageKeys = objects.objects + .map((obj: any) => obj.key) + .filter((key: string) => key.endsWith('/280.png')) + .sort() + .slice(-10); + + // Transform keys to full URLs + const images = imageKeys.map((key: string) => { + // Extract date/time from path: imgs/20250622/0930/280.png + const pathParts = key.split('/'); + const date = pathParts[1]; + const time = pathParts[2]; + const filename = pathParts[3]; + + return { + path: key, + url: `https://imgs.geshem.space/${date}/${time}/${filename}`, + date: date, + time: time + }; + }); + + return new Response(JSON.stringify({ + images: images, + count: images.length, + timestamp: new Date().toISOString() + }), { + status: 200, + headers: { + 'Content-Type': 'application/json', + 'Cache-Control': 'public, max-age=60, s-maxage=60' + } + }); + + } catch (error) { + console.error('Error fetching images:', error); + return new Response(JSON.stringify({ + error: 'Failed to fetch images', + message: error instanceof Error ? error.message : 'Unknown error' + }), { + status: 500, + headers: { + 'Content-Type': 'application/json' + } + }); + } +};
\ No newline at end of file |
