Admin polish: stability fixes, schema security, consistent UI + sidebar #1
3 changed files with 394 additions and 374 deletions
|
|
@ -53,7 +53,7 @@ export function replaceUrlsWithKeys(
|
|||
if (typeof value === 'string') {
|
||||
let result = value;
|
||||
for (const [url, key] of urlToKey) {
|
||||
result = result.replace(url, `__IMG__${key}__`);
|
||||
result = result.replaceAll(url, `__IMG__${key}__`);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
|
@ -80,7 +80,7 @@ export function restorePlaceholders(
|
|||
if (typeof value === 'string') {
|
||||
let result = value;
|
||||
for (const [key, url] of keyToUrl) {
|
||||
result = result.replace(`__IMG__${key}__`, url);
|
||||
result = result.replaceAll(`__IMG__${key}__`, url);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
|
@ -124,6 +124,17 @@ export async function fetchImageAsBase64(url: string): Promise<string> {
|
|||
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.
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -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>();
|
||||
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
|
||||
|
|
|
|||
|
|
@ -4,15 +4,19 @@ import type { RequestHandler } from './$types';
|
|||
import {
|
||||
dataUriToFile,
|
||||
restorePlaceholders,
|
||||
validateSiteExport,
|
||||
type SiteExport
|
||||
} from '$lib/exportImport';
|
||||
|
||||
export const POST: RequestHandler = async ({ request, locals }) => {
|
||||
const body = (await request.json()) as SiteExport;
|
||||
|
||||
if (!body || body.version !== 1) {
|
||||
throw error(400, 'Invalid or unsupported export file.');
|
||||
let body: SiteExport;
|
||||
try {
|
||||
body = await request.json();
|
||||
} catch {
|
||||
throw error(400, 'The file is not valid JSON.');
|
||||
}
|
||||
const invalid = validateSiteExport(body);
|
||||
if (invalid) throw error(400, invalid);
|
||||
|
||||
const results = {
|
||||
imagesUploaded: 0,
|
||||
|
|
@ -55,7 +59,7 @@ export const POST: RequestHandler = async ({ request, locals }) => {
|
|||
// Try to find existing page by slug
|
||||
let existing;
|
||||
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 {
|
||||
existing = null;
|
||||
}
|
||||
|
|
@ -87,7 +91,7 @@ export const POST: RequestHandler = async ({ request, locals }) => {
|
|||
|
||||
let existing;
|
||||
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 {
|
||||
existing = null;
|
||||
}
|
||||
|
|
@ -108,7 +112,9 @@ export const POST: RequestHandler = async ({ request, locals }) => {
|
|||
try {
|
||||
let existing;
|
||||
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 {
|
||||
existing = null;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue