src/routes/+page.server.ts 1.1 K raw
1
import type { PageServerLoad } from "./$types";
2
import type { ImageItem } from "$lib";
3
4
// R2 public URL - update this after enabling public access on your bucket
5
const R2_BASE_URL = "https://r2.steve.photo";
6
7
const PAGE_SIZE = 15;
8
9
export const load: PageServerLoad = async ({ platform }) => {
10
	const db = platform?.env?.DB;
11
12
	const result = await db
13
		.prepare("SELECT * FROM photos ORDER BY date DESC LIMIT ?")
14
		.bind(PAGE_SIZE)
15
		.all();
16
17
	const countResult = await db
18
		.prepare("SELECT COUNT(*) as total FROM photos")
19
		.first();
20
	const total = (countResult?.total as number) || 0;
21
22
	const photos: ImageItem[] = result.results.map(
23
		(row: Record<string, unknown>) => ({
24
			slug: row.slug,
25
			title: row.title,
26
			date: row.date,
27
			image: `${R2_BASE_URL}/${row.image_key}`,
28
			thumb: `${R2_BASE_URL}/${row.thumb_key}`,
29
			type: row.type,
30
			camera: row.camera,
31
			lens: row.lens,
32
			aperture: row.aperture,
33
			exposure: row.exposure,
34
			focalLength: row.focal_length,
35
			iso: row.iso,
36
			make: row.make,
37
			blurData: row.blur_data as string,
38
		}),
39
	);
40
41
	return { photos, total, pageSize: PAGE_SIZE };
42
};