Add image loading
This commit is contained in:
@ -25,7 +25,7 @@
|
||||
<b>Title:</b> {$strf(item.title)}<br />
|
||||
<b>Description:</b> {#if item.description}{$strf(item.description)}{:else}<i>no description</i>{/if}
|
||||
</p>
|
||||
<img src={`${base}&item=${item.item}`} alt={$strf(item.title)} />
|
||||
<img src={`${base}${item.item}/t`} alt={$strf(item.title) || item.item} />
|
||||
</li>
|
||||
{/each}
|
||||
</ul>
|
||||
|
||||
@ -60,6 +60,7 @@ export const strf = derived(language, $language => {
|
||||
* @param {...any} args
|
||||
*/
|
||||
function translate(translations, ...args) {
|
||||
if (translations === undefined) return undefined;
|
||||
if (typeof translations === 'string') return translations;
|
||||
const str = translations[$language];
|
||||
if (str === undefined) return translations.de;
|
||||
|
||||
3
src/lib/types.d.ts
vendored
3
src/lib/types.d.ts
vendored
@ -35,9 +35,12 @@ type Item = ItemMetadata;
|
||||
type Album = AlbumMetadata & {
|
||||
slug: string;
|
||||
uriTimestamp?: string;
|
||||
allowDownload?: boolean;
|
||||
items: Item[];
|
||||
};
|
||||
|
||||
type ApiError = {
|
||||
error: string;
|
||||
};
|
||||
|
||||
type GetFileFunction = (zipName: string, entryName: string, keepOpen: T) => Promise<T extends true ? { content: Buffer, zip: import('node-stream-zip').StreamZipAsync } : Buffer>;
|
||||
|
||||
96
src/lib/util/album.js
Normal file
96
src/lib/util/album.js
Normal file
@ -0,0 +1,96 @@
|
||||
import { error } from "@sveltejs/kit";
|
||||
import StreamZip from "node-stream-zip";
|
||||
|
||||
/** @param {string} zipName */
|
||||
async function _getZip(zipName) {
|
||||
return new StreamZip.async({ file: `./zip/${zipName}` });
|
||||
}
|
||||
/** @param {import('node-stream-zip').StreamZipAsync} zip @param {string} entryName */
|
||||
async function _getFile(zip, entryName) {
|
||||
return await zip.entryData(entryName);
|
||||
}
|
||||
/** @param {import('node-stream-zip').StreamZipAsync} zip */
|
||||
async function _getMetadata(zip) {
|
||||
const metadataContent = await _getFile(zip, 'album.json');
|
||||
return JSON.parse(metadataContent.toString());
|
||||
}
|
||||
/** @param {import('node-stream-zip').StreamZipAsync} zip @param {boolean} keepOpen */
|
||||
async function _close(zip, keepOpen) {
|
||||
if (!keepOpen)
|
||||
await zip.close();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} zipName
|
||||
* @param {boolean} keepOpen
|
||||
* @returns {Promise<{album: Album, zip: import('node-stream-zip').StreamZipAsync}>}
|
||||
*/
|
||||
export async function getMetadataOpen(zipName, keepOpen = true) {
|
||||
const zip = await _getZip(zipName);
|
||||
const album = await _getMetadata(zip);
|
||||
await _close(zip, keepOpen);
|
||||
return { album, zip };
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} zipName
|
||||
* @returns {Promise<Album>}
|
||||
*/
|
||||
export async function getMetadata(zipName) {
|
||||
return (await getMetadataOpen(zipName, false)).album;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} zipName
|
||||
* @param {string} entryName
|
||||
* @param {boolean} keepOpen
|
||||
* @returns {Promise<{album: Album, content: Buffer, zip: import('node-stream-zip').StreamZipAsync}>}
|
||||
*/
|
||||
export async function getMetadataAndFileOpen(zipName, entryName, keepOpen = true) {
|
||||
const zip = await _getZip(zipName);
|
||||
const album = await _getMetadata(zip);
|
||||
const content = await _getFile(zip, entryName);
|
||||
await _close(zip, keepOpen);
|
||||
return { album, content, zip };
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} zipName
|
||||
* @param {string} entryName
|
||||
* @returns {Promise<{album: Album, content: Buffer}>}
|
||||
*/
|
||||
export async function getMetadataAndFile(zipName, entryName) {
|
||||
const { album, content } = await getMetadataAndFileOpen(zipName, entryName, false);
|
||||
return { album, content };
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} zipName
|
||||
* @param {string} entryName
|
||||
* @param {boolean} keepOpen
|
||||
* @returns {Promise<{content: Buffer, zip: import('node-stream-zip').StreamZipAsync}>}
|
||||
*/
|
||||
export async function getFileOpen(zipName, entryName, keepOpen = true) {
|
||||
const zip = await _getZip(zipName);
|
||||
let content = null;
|
||||
try {
|
||||
console.log(`Getting ${entryName} from ${zipName}`);
|
||||
content = await zip.entryData(entryName);
|
||||
} catch (err) {
|
||||
console.error(`${err.stack}`.replaceAll('/home/sveltekit', '.'));
|
||||
throw error(404, `File ${entryName} not found.`);
|
||||
}
|
||||
await _close(zip, keepOpen);
|
||||
return { content, zip };
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} zipName
|
||||
* @param {string} entryName
|
||||
* @returns {Promise<Buffer>}
|
||||
*/
|
||||
export async function getFile(zipName, entryName) {
|
||||
return (await getFileOpen(zipName, entryName, false)).content;
|
||||
}
|
||||
42
src/lib/util/links.js
Normal file
42
src/lib/util/links.js
Normal file
@ -0,0 +1,42 @@
|
||||
/**
|
||||
* @param {string} str
|
||||
* @returns {string}
|
||||
*/
|
||||
export function safe(str) {
|
||||
return str.replace(/[^\w.-]/gi, '');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} str
|
||||
* @returns {string}
|
||||
*/
|
||||
export function getFileName(str) {
|
||||
return safe(str.split('/').pop() || '');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} str
|
||||
* @returns {string}
|
||||
*/
|
||||
export function getFilePath(str) {
|
||||
console.log(`getFilePath(${str})`);
|
||||
return str.split('/').map(safe).join('/');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {any} params
|
||||
* @returns {string}
|
||||
*/
|
||||
export function getZipName(params) {
|
||||
const { slug, timestamp } = params;
|
||||
return `${safe(slug)}${timestamp ? '-' + safe(timestamp) :''}.zip`;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {any} params
|
||||
* @returns {string}
|
||||
*/
|
||||
export function getAlbumUri(params) {
|
||||
const { slug, timestamp } = params;
|
||||
return `/g/${safe(slug)}${timestamp ? '/' + safe(timestamp) :''}`;
|
||||
}
|
||||
Reference in New Issue
Block a user