Export/import: fix lost images and malformed-file crashes
- An image URL used more than once in the same string (e.g. rich text) was only replaced once, leaving broken __IMG__ placeholders after import. - Images that failed to export were still replaced by placeholders that the import could never resolve; keep the original URL instead. - Validate the uploaded file's shape and JSON (was a 500 on bad input). - Build lookup filters with pb.filter so names/slugs with quotes don't create duplicates. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KgF3Kf47LjPGHKQHszpUAv
This commit is contained in:
parent
ff54adb00c
commit
1eb58c71ba
3 changed files with 394 additions and 374 deletions
|
|
@ -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.
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue