Admin polish: stability fixes, schema security, consistent UI + sidebar #1

Merged
reudy merged 21 commits from session/polish into main 2026-10-07 16:30:40 +02:00
3 changed files with 394 additions and 374 deletions
Showing only changes of commit 1eb58c71ba - Show all commits

View file

@ -53,7 +53,7 @@ export function replaceUrlsWithKeys(
if (typeof value === 'string') { if (typeof value === 'string') {
let result = value; let result = value;
for (const [url, key] of urlToKey) { for (const [url, key] of urlToKey) {
result = result.replace(url, `__IMG__${key}__`); result = result.replaceAll(url, `__IMG__${key}__`);
} }
return result; return result;
} }
@ -80,7 +80,7 @@ export function restorePlaceholders(
if (typeof value === 'string') { if (typeof value === 'string') {
let result = value; let result = value;
for (const [key, url] of keyToUrl) { for (const [key, url] of keyToUrl) {
result = result.replace(`__IMG__${key}__`, url); result = result.replaceAll(`__IMG__${key}__`, url);
} }
return result; return result;
} }
@ -124,6 +124,17 @@ export async function fetchImageAsBase64(url: string): Promise<string> {
return `data:${blob.type};base64,${base64}`; return `data:${blob.type};base64,${base64}`;
} }
/** Shape check for an uploaded export file; returns an error message or null. */
export function validateSiteExport(body: unknown): string | null {
if (!body || typeof body !== 'object') return 'The file is not a CMS export.';
const b = body as Partial<SiteExport>;
if (b.version !== 1) return 'Unsupported export version.';
if (!Array.isArray(b.pages) || !Array.isArray(b.globals)) return 'The export is missing pages or globals.';
if (b.events !== undefined && !Array.isArray(b.events)) return 'The export has an invalid events list.';
if (!b.images || typeof b.images !== 'object' || Array.isArray(b.images)) return 'The export is missing its images.';
return null;
}
/** /**
* Convert a base64 data-URI back to a File object for upload. * Convert a base64 data-URI back to a File object for upload.
*/ */

View file

@ -46,10 +46,13 @@ export const GET: RequestHandler = async ({ locals }) => {
} }
} }
// Build URL → key mapping // Build URL → key mapping — only for images that were actually exported.
// A URL whose image couldn't be fetched is left as-is rather than turned
// into a placeholder the import could never resolve.
const urlToKeyMap = new Map<string, string>(); const urlToKeyMap = new Map<string, string>();
for (const url of allUrls) { for (const url of allUrls) {
urlToKeyMap.set(url, urlToKey(url)); const key = urlToKey(url);
if (key in images) urlToKeyMap.set(url, key);
} }
// Replace URLs with placeholders in pages // Replace URLs with placeholders in pages

View file

@ -4,15 +4,19 @@ import type { RequestHandler } from './$types';
import { import {
dataUriToFile, dataUriToFile,
restorePlaceholders, restorePlaceholders,
validateSiteExport,
type SiteExport type SiteExport
} from '$lib/exportImport'; } from '$lib/exportImport';
export const POST: RequestHandler = async ({ request, locals }) => { export const POST: RequestHandler = async ({ request, locals }) => {
const body = (await request.json()) as SiteExport; let body: SiteExport;
try {
if (!body || body.version !== 1) { body = await request.json();
throw error(400, 'Invalid or unsupported export file.'); } catch {
throw error(400, 'The file is not valid JSON.');
} }
const invalid = validateSiteExport(body);
if (invalid) throw error(400, invalid);
const results = { const results = {
imagesUploaded: 0, imagesUploaded: 0,
@ -55,7 +59,7 @@ export const POST: RequestHandler = async ({ request, locals }) => {
// Try to find existing page by slug // Try to find existing page by slug
let existing; let existing;
try { try {
existing = await locals.pb.collection('pages').getFirstListItem(`slug = "${page.slug}"`); existing = await locals.pb.collection('pages').getFirstListItem(locals.pb.filter('slug = {:slug}', { slug: page.slug }));
} catch { } catch {
existing = null; existing = null;
} }
@ -87,7 +91,7 @@ export const POST: RequestHandler = async ({ request, locals }) => {
let existing; let existing;
try { try {
existing = await locals.pb.collection('globals').getFirstListItem(`key = "${g.key}"`); existing = await locals.pb.collection('globals').getFirstListItem(locals.pb.filter('key = {:key}', { key: g.key }));
} catch { } catch {
existing = null; existing = null;
} }
@ -108,7 +112,9 @@ export const POST: RequestHandler = async ({ request, locals }) => {
try { try {
let existing; let existing;
try { try {
existing = await locals.pb.collection('events').getFirstListItem(`name = "${event.name}" && start = "${event.start}"`); existing = await locals.pb
.collection('events')
.getFirstListItem(locals.pb.filter('name = {:name} && start = {:start}', { name: event.name, start: event.start }));
} catch { } catch {
existing = null; existing = null;
} }