Change many lines

This commit is contained in:
Luca Bosin
2023-08-17 09:41:41 +02:00
parent 5e99dc4aa2
commit ba42d6958a
36 changed files with 1251 additions and 312 deletions

View File

@ -1,16 +1,26 @@
import { error } from '@sveltejs/kit';
import albums from '$lib/albums.json';
import StreamZip from 'node-stream-zip';
/** @type {import('./$types').PageLoad} */
export function load({ params }) {
const album = albums.find((album) => album.slug === params.slug);
if (album) {
return {
title: album.title,
content: album.description,
image: '/s/lga/?i=LFB04128-Enhanced-NR.jpg'
};
}
export async function load({ params }) {
const { slug, timestamp } = params;
const cslug = slug.replace(/[^\w-]/gi, '');
const ctimestamp = timestamp?.replace(/[^\w-]/gi, '');
throw error(404, 'Not found');
const zipFile = `./zip/${cslug}${ctimestamp ? '-' + ctimestamp :''}.zip`;
let entries = null;
try {
const zip = new StreamZip.async({ file: zipFile });
entries = await zip.entries();
await zip.close();
} catch (err) {
console.error(err);
throw error(404, 'Not found');
}
return {
slug: params.slug,
timestamp: params.timestamp,
entries
};
}

View File

@ -1,8 +1,14 @@
<script>
import { str, strf } from '$lib/data/language.js';
import Header from '$lib/components/Header.svelte';
import Gallery from '$lib/components/Gallery.svelte';
/** @type {import('./$types').PageData} */
export let data;
const uriBase = `/s/apitest.php?slug=${data.slug}` + (data.timestamp ? `&timestamp=${data.timestamp}` : '');
</script>
<h1>{data.title}</h1>
<div>{@html data.content}</div>
<img src={data.image} alt={data.title} />
<Header title={$strf(data.slug)}/>
<Gallery items={$album.items} base={uriBase} />

View File

@ -1,26 +0,0 @@
import { error } from '@sveltejs/kit';
/** @type {import('./$types').RequestHandler} */
export function GET({ url }) {
const noAttachment = url.searchParams.has('r');
const min = Number(url.searchParams.get('min') ?? '0');
const max = Number(url.searchParams.get('max') ?? '1');
const d = max - min;
if (isNaN(d) || d < 0) {
throw error(400, 'min and max must be numbers, and min must be less than max');
}
const random = min + Math.random() * d;
return new Response(String(random), {
headers: {
'Content-Type': 'text/plain',
'Content-Disposition': noAttachment ? 'inline' : 'attachment'
}
});
}

View File

@ -0,0 +1,20 @@
import { error } from '@sveltejs/kit';
import StreamZip from 'node-stream-zip';
/** @type {import('./$types').PageLoad} */
export async function load({ params }) {
let entries = null;
try {
const zip = new StreamZip.async({ file: `./zip/${params.slug}.zip` });
entries = await zip.entries();
await zip.close();
} catch (err) {
console.error(err);
}
error(404, 'Not found');
return {
slug: params.slug,
timestamp: params.timestamp,
entries
};
}

View File

@ -0,0 +1,13 @@
<script>
/** @type {import('./$types').PageData} */
export let data;
/** @type {import('node-stream-zip').ZipEntry[]} */
$: entries = data.entries !== null ? (Array.isArray(data.entries) ? data.entries : Object.values(data.entries)) : [];
</script>
<h2>Zip Entries</h2>
{#each entries as entry}
{entry.name}
{:else}
No entries
{/each}

View File

@ -0,0 +1,24 @@
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'
}
});
}