24 lines
790 B
JavaScript
24 lines
790 B
JavaScript
import StreamZip from 'node-stream-zip';
|
|
import sharp from 'sharp';
|
|
|
|
/** @type {import('./$types').RequestHandler} */
|
|
export async function GET({ params }) {
|
|
|
|
let entryData = null;
|
|
try {
|
|
const zip = new StreamZip.async({ file: `./zip/${params.slug}.zip` });
|
|
const entries = Object.values(await zip.entries()).filter(entry => entry.name.endsWith('.jpg'));
|
|
const entry = entries[Math.floor(Math.random() * entries.length)];
|
|
const content = await zip.entryData(entry.name);
|
|
await zip.close();
|
|
entryData = await sharp(content).resize(400).avif({ quality: 70, effort: 0 }).toBuffer()
|
|
} catch (err) {
|
|
console.error(err);
|
|
}
|
|
return new Response(entryData, {
|
|
headers: {
|
|
'Content-Type': 'image/avif',
|
|
'Content-Disposition': 'inline'
|
|
}
|
|
});
|
|
} |