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:
agent 2026-10-06 10:46:24 +00:00
parent ff54adb00c
commit 1eb58c71ba
3 changed files with 394 additions and 374 deletions

View file

@ -1,139 +1,150 @@
import type { CalendarEventRecord, ContentBlock } from '$lib/types';
/**
* The portable export format.
* Images are stored as base64 data-URIs so the file is fully self-contained.
*/
export type SiteExport = {
version: 1;
exportedAt: string;
pages: {
slug: string;
title: string;
blocks: ContentBlock[];
published: boolean;
}[];
globals: {
key: string;
data: Record<string, unknown>;
}[];
events?: Omit<CalendarEventRecord, 'id'>[];
/** Maps original PocketBase file URLs → base64 data-URI */
images: Record<string, string>;
};
/** Regex to match PocketBase file URLs */
const PB_FILE_RE = /https?:\/\/[^/]+\/api\/files\/([^/]+)\/([^/]+)\/([^\s"')]+)/g;
/**
* Extract all PocketBase image URLs from a value (recursive).
*/
export function extractImageUrls(value: unknown, urls = new Set<string>()): Set<string> {
if (typeof value === 'string') {
let match: RegExpExecArray | null;
const re = new RegExp(PB_FILE_RE.source, 'g');
while ((match = re.exec(value)) !== null) {
urls.add(match[0]);
}
} else if (Array.isArray(value)) {
for (const item of value) extractImageUrls(item, urls);
} else if (value && typeof value === 'object') {
for (const v of Object.values(value as Record<string, unknown>)) extractImageUrls(v, urls);
}
return urls;
}
/**
* Replace all PocketBase URLs in a value with a placeholder key.
*/
export function replaceUrlsWithKeys(
value: unknown,
urlToKey: Map<string, string>
): unknown {
if (typeof value === 'string') {
let result = value;
for (const [url, key] of urlToKey) {
result = result.replace(url, `__IMG__${key}__`);
}
return result;
}
if (Array.isArray(value)) {
return value.map((item) => replaceUrlsWithKeys(item, urlToKey));
}
if (value && typeof value === 'object') {
const obj: Record<string, unknown> = {};
for (const [k, v] of Object.entries(value as Record<string, unknown>)) {
obj[k] = replaceUrlsWithKeys(v, urlToKey);
}
return obj;
}
return value;
}
/**
* Restore image placeholders back to real URLs after import.
*/
export function restorePlaceholders(
value: unknown,
keyToUrl: Map<string, string>
): unknown {
if (typeof value === 'string') {
let result = value;
for (const [key, url] of keyToUrl) {
result = result.replace(`__IMG__${key}__`, url);
}
return result;
}
if (Array.isArray(value)) {
return value.map((item) => restorePlaceholders(item, keyToUrl));
}
if (value && typeof value === 'object') {
const obj: Record<string, unknown> = {};
for (const [k, v] of Object.entries(value as Record<string, unknown>)) {
obj[k] = restorePlaceholders(v, keyToUrl);
}
return obj;
}
return value;
}
/**
* Generate a short unique key for an image URL.
*/
export function urlToKey(url: string): string {
const parts = url.split('/');
const filename = parts[parts.length - 1] || 'img';
const id = parts[parts.length - 2]?.slice(0, 8) || 'unknown';
return `${id}_${filename}`;
}
/**
* Fetch an image and return it as a base64 data-URI string.
*/
export async function fetchImageAsBase64(url: string): Promise<string> {
const response = await fetch(url);
if (!response.ok) throw new Error(`Failed to fetch image: ${url} (${response.status})`);
const blob = await response.blob();
const buffer = await blob.arrayBuffer();
const bytes = new Uint8Array(buffer);
let binary = '';
for (let i = 0; i < bytes.length; i++) {
binary += String.fromCharCode(bytes[i]);
}
const base64 = btoa(binary);
return `data:${blob.type};base64,${base64}`;
}
/**
* Convert a base64 data-URI back to a File object for upload.
*/
export function dataUriToFile(dataUri: string, filename: string): File {
const [header, base64] = dataUri.split(',');
const mime = header.match(/data:(.*?);/)?.[1] || 'image/png';
const binary = atob(base64);
const bytes = new Uint8Array(binary.length);
for (let i = 0; i < binary.length; i++) {
bytes[i] = binary.charCodeAt(i);
}
return new File([bytes], filename, { type: mime });
import type { CalendarEventRecord, ContentBlock } from '$lib/types';
/**
* The portable export format.
* Images are stored as base64 data-URIs so the file is fully self-contained.
*/
export type SiteExport = {
version: 1;
exportedAt: string;
pages: {
slug: string;
title: string;
blocks: ContentBlock[];
published: boolean;
}[];
globals: {
key: string;
data: Record<string, unknown>;
}[];
events?: Omit<CalendarEventRecord, 'id'>[];
/** Maps original PocketBase file URLs → base64 data-URI */
images: Record<string, string>;
};
/** Regex to match PocketBase file URLs */
const PB_FILE_RE = /https?:\/\/[^/]+\/api\/files\/([^/]+)\/([^/]+)\/([^\s"')]+)/g;
/**
* Extract all PocketBase image URLs from a value (recursive).
*/
export function extractImageUrls(value: unknown, urls = new Set<string>()): Set<string> {
if (typeof value === 'string') {
let match: RegExpExecArray | null;
const re = new RegExp(PB_FILE_RE.source, 'g');
while ((match = re.exec(value)) !== null) {
urls.add(match[0]);
}
} else if (Array.isArray(value)) {
for (const item of value) extractImageUrls(item, urls);
} else if (value && typeof value === 'object') {
for (const v of Object.values(value as Record<string, unknown>)) extractImageUrls(v, urls);
}
return urls;
}
/**
* Replace all PocketBase URLs in a value with a placeholder key.
*/
export function replaceUrlsWithKeys(
value: unknown,
urlToKey: Map<string, string>
): unknown {
if (typeof value === 'string') {
let result = value;
for (const [url, key] of urlToKey) {
result = result.replaceAll(url, `__IMG__${key}__`);
}
return result;
}
if (Array.isArray(value)) {
return value.map((item) => replaceUrlsWithKeys(item, urlToKey));
}
if (value && typeof value === 'object') {
const obj: Record<string, unknown> = {};
for (const [k, v] of Object.entries(value as Record<string, unknown>)) {
obj[k] = replaceUrlsWithKeys(v, urlToKey);
}
return obj;
}
return value;
}
/**
* Restore image placeholders back to real URLs after import.
*/
export function restorePlaceholders(
value: unknown,
keyToUrl: Map<string, string>
): unknown {
if (typeof value === 'string') {
let result = value;
for (const [key, url] of keyToUrl) {
result = result.replaceAll(`__IMG__${key}__`, url);
}
return result;
}
if (Array.isArray(value)) {
return value.map((item) => restorePlaceholders(item, keyToUrl));
}
if (value && typeof value === 'object') {
const obj: Record<string, unknown> = {};
for (const [k, v] of Object.entries(value as Record<string, unknown>)) {
obj[k] = restorePlaceholders(v, keyToUrl);
}
return obj;
}
return value;
}
/**
* Generate a short unique key for an image URL.
*/
export function urlToKey(url: string): string {
const parts = url.split('/');
const filename = parts[parts.length - 1] || 'img';
const id = parts[parts.length - 2]?.slice(0, 8) || 'unknown';
return `${id}_${filename}`;
}
/**
* Fetch an image and return it as a base64 data-URI string.
*/
export async function fetchImageAsBase64(url: string): Promise<string> {
const response = await fetch(url);
if (!response.ok) throw new Error(`Failed to fetch image: ${url} (${response.status})`);
const blob = await response.blob();
const buffer = await blob.arrayBuffer();
const bytes = new Uint8Array(buffer);
let binary = '';
for (let i = 0; i < bytes.length; i++) {
binary += String.fromCharCode(bytes[i]);
}
const base64 = btoa(binary);
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.
*/
export function dataUriToFile(dataUri: string, filename: string): File {
const [header, base64] = dataUri.split(',');
const mime = header.match(/data:(.*?);/)?.[1] || 'image/png';
const binary = atob(base64);
const bytes = new Uint8Array(binary.length);
for (let i = 0; i < binary.length; i++) {
bytes[i] = binary.charCodeAt(i);
}
return new File([bytes], filename, { type: mime });
}