Naturalised env import

This commit is contained in:
Alex Rennie-Lis
2026-06-28 23:21:59 +01:00
parent bed8de789e
commit 0cfd200ac5
3 changed files with 32 additions and 41 deletions

View File

@@ -1,48 +1,46 @@
import { json, error } from '@sveltejs/kit';
import { S3Client, ListObjectsV2Command, GetObjectCommand } from '@aws-sdk/client-s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
import {
B2_ENDPOINT, B2_REGION, B2_ACCESS_KEY_ID, B2_SECRET_ACCESS_KEY, B2_BUCKET_NAME
} from '$env/dynamic/private';
const s3 = new S3Client({
endpoint: B2_ENDPOINT,
region: B2_REGION,
credentials: {
accessKeyId: B2_ACCESS_KEY_ID,
secretAccessKey: B2_SECRET_ACCESS_KEY
}
});
import { env } from '$env/dynamic/private';
export async function GET({ url }) {
const nextToken = url.searchParams.get('next') || undefined;
try {
// 1. Fetch a batch of files from the thumbs/ folder
const s3 = new S3Client({
endpoint: env.B2_ENDPOINT,
region: env.B2_REGION,
credentials: {
accessKeyId: env.B2_ACCESS_KEY_ID,
secretAccessKey: env.B2_SECRET_ACCESS_KEY
}
});
// Fetch a batch of files from the thumbs/ folder
const listCommand = new ListObjectsV2Command({
Bucket: B2_BUCKET_NAME,
Bucket: env.B2_BUCKET_NAME,
Prefix: 'thumbs/',
MaxKeys: 40, // Increased slight headroom since we filter on the server side now
MaxKeys: 40,
ContinuationToken: nextToken
});
const listResponse = await s3.send(listCommand);
const contents = listResponse.Contents || [];
// 2. Strict Filter: Only allow actual .jpg or .jpeg extensions
// Strict Filter: Only allow actual .jpg or .jpeg extensions
const imageFiles = contents.filter(file => {
const lowerKey = file.Key.toLowerCase();
return lowerKey.endsWith('.jpg') || lowerKey.endsWith('.jpeg');
});
// 3. Generate presigned URLs for validated items
// Generate presigned URLs for validated items
const photos = await Promise.all(
imageFiles.map(async (file) => {
const id = file.Key.replace('thumbs/', '');
const fullSizeKey = `images/${id}`;
const thumbCommand = new GetObjectCommand({ Bucket: B2_BUCKET_NAME, Key: file.Key });
const fullCommand = new GetObjectCommand({ Bucket: B2_BUCKET_NAME, Key: fullSizeKey });
const thumbCommand = new GetObjectCommand({ Bucket: env.B2_BUCKET_NAME, Key: file.Key });
const fullCommand = new GetObjectCommand({ Bucket: env.B2_BUCKET_NAME, Key: fullSizeKey });
// 10 minutes = 600 seconds
const [thumbUrl, fullUrl] = await Promise.all([
@@ -56,7 +54,6 @@ export async function GET({ url }) {
return json({
photos,
// Pass the original continuation token back so pagination structural flow remains intact
nextContinuationToken: listResponse.NextContinuationToken || null
});
} catch (err) {