import { db, tables } from './db/index.ts'; import { EXPORT_FORMAT, EXPORT_VERSION, type ManuscriptJson } from './export.ts'; import { bodyToText } from './markdown.ts'; import { countWords } from '#lib/words.ts'; import { indexNote, indexScene } from './search.ts'; import { templateOrNull } from './note-types.ts'; import { sanitiseBody } from '#lib/body.ts'; import { readManuscriptZip, listZipEntries, readEntry, type ZipEntry } from './archive-input.ts'; import { ImageRefusedError, MAX_IMAGE_BYTES, removeImage, storeImage, type ImageFacts } from './uploads.ts'; import { MAX_ARCHIVE_IMAGE_BYTES } from '#lib/import-limits.ts'; import { getInstance } from './instance.ts'; import { isMatterPosition } from '#lib/matter.ts'; import { SNAPSHOT_SOURCES } from '#lib/snapshot-sources.ts'; import { ImportError } from './import-error.ts'; import type { PreparedSimpleProject } from './import-plan.ts'; import { insertIndexedScene } from './manuscript.ts'; export { ImportError } from './import-error.ts'; export type ImportResult = { projectId: string; title: string; chapters: number; scenes: number; notes: number; words: number; warnings?: string[]; }; type Json = Record; const asString = (value: unknown, fallback = ''): string => typeof value === 'string' ? value : fallback; const asNumber = (value: unknown, fallback = 0): number => typeof value === 'number' && Number.isFinite(value) ? value : fallback; const asRecord = (value: unknown): Json | null => value && typeof value === 'object' && !Array.isArray(value) ? (value as Json) : null; function damaged(message: string): never { throw new ImportError(`That manuscript is damaged: ${message}.`); } function records(row: Json, key: string, path = key): Json[] { const value = row[key]; if (value === undefined) return []; if (!Array.isArray(value)) damaged(`${path} must be an array`); return value.map((entry, index) => { const record = asRecord(entry); if (!record) damaged(`${path}[${index}] must be an object`); return record; }); } function stringField(row: Json, key: string, path: string, required = false, nullable = false) { const value = row[key]; if (value === undefined && !required) return; if (value === null && nullable) return; if (typeof value !== 'string' || (required && value.trim().length === 0)) { damaged(`${path}.${key} must be ${required ? 'a nonempty string' : 'a string'}`); } } function dateField(row: Json, key: string, path: string, nullable = false, required = false) { const value = row[key]; if (value === undefined && !required) return; if (value === null && nullable) return; if ( typeof value !== 'string' || !/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{1,3})?(?:Z|[+-]\d{2}:\d{2})$/.test(value) || !Number.isFinite(new Date(value).getTime()) || new Date(`${value.slice(0, 19)}Z`).toISOString().slice(0, 19) !== value.slice(0, 19) ) { damaged(`${path}.${key} must be an ISO timestamp`); } } function integerField(row: Json, key: string, path: string, required = false) { const value = row[key]; if (value === undefined && !required) return; if ( typeof value !== 'number' || !Number.isSafeInteger(value) || (key !== 'ordinal' && value < 0) ) { damaged(`${path}.${key} must be ${key === 'ordinal' ? 'an integer' : 'a nonnegative integer'}`); } } function validateRow(row: Json, path: string) { for (const key of ['title', 'summary', 'name', 'pluralName', 'accent', 'bodyText']) { stringField(row, key, path); } for (const key of ['subtitle', 'description', 'author', 'type', 'storyline', 'label']) { stringField(row, key, path, false, true); } for (const key of ['ordinal', 'wordCount', 'revision']) integerField(row, key, path); for (const key of ['createdAt', 'updatedAt']) dateField(row, key, path); for (const key of ['deletedAt', 'lastOpenedAt']) dateField(row, key, path, true); } function register(row: Json, path: string, ids: Set) { validateRow(row, path); stringField(row, 'id', path, true); const id = row.id as string; if (ids.has(id)) damaged(`${path}.id duplicates ${id}`); ids.add(id); } function reference(row: Json, key: string, path: string, ids: Set, required = false) { const value = row[key]; if ((value === undefined || value === null) && !required) return; if (typeof value !== 'string' || !ids.has(value)) { damaged(`${path}.${key} refers to a missing row`); } } /** * How many rows carry each value of `labelKey`, counted once so a legacy name can * be resolved per row without rescanning every row. A count of exactly one is what * makes a name unambiguous. */ function labelCounts(rows: Json[], labelKey: string): Map { const counts = new Map(); for (const row of rows) { const label = row[labelKey]; counts.set(label, (counts.get(label) ?? 0) + 1); } return counts; } function namedReference( row: Json, idKey: string, nameKey: string, path: string, ids: Set, labels: Map ) { // An explicit null must not fall back to a legacy name. if (Object.hasOwn(row, idKey)) { reference(row, idKey, path, ids); return; } const name = row[nameKey]; if (name === undefined || name === null || name === '') return; if (labels.get(name) !== 1) { damaged(`${path}.${nameKey} does not name exactly one row`); } } function enumField( row: Json, key: string, path: string, choices: readonly string[], required = false ) { if (row[key] === undefined && !required) return; if (typeof row[key] !== 'string' || !choices.includes(row[key] as string)) { damaged(`${path}.${key} has an unsupported value`); } } function historyBody(row: Json, path: string) { if (!Object.hasOwn(row, 'body') || row.body === undefined) damaged(`${path} has no body`); if (typeof row.bodyText !== 'string') damaged(`${path}.bodyText must be a string`); dateField(row, 'createdAt', path, false, true); } function validateManuscript(value: unknown, insideCheckpoint = false): ManuscriptJson { const root = asRecord(value); if (!root || root.format !== EXPORT_FORMAT) { throw new ImportError( 'That file is not a Fablewright manuscript. Look for the file called manuscript.json inside the export.' ); } const version = root.version; if (typeof version === 'number' && version > EXPORT_VERSION) { throw new ImportError( `That manuscript was exported by a newer version of Fablewright (format ${version}). This instance reads up to ${EXPORT_VERSION}.` ); } if (typeof version !== 'number' || !Number.isInteger(version) || version < 1) { throw new ImportError('That manuscript has an unsupported format version.'); } const project = asRecord(root.project); if (!project || asString(project.title).trim().length === 0) { throw new ImportError('That manuscript has no project title.'); } validateRow(project, 'project'); if (Object.hasOwn(project, 'id')) stringField(project, 'id', 'project', true); dateField(root, 'exportedAt', 'manuscript'); const bookIds = new Set(); const partIds = new Set(); const chapterIds = new Set(); const sceneIds = new Set(); const typeIds = new Set(); const storylineIds = new Set(); const noteIds = new Set(); const cardIds = new Set(); const noteTypes = records(root, 'noteTypes'); const storylines = records(root, 'storylines'); const notes = records(root, 'notes'); const cards = records(root, 'plotCards'); for (const [index, row] of noteTypes.entries()) register(row, `noteTypes[${index}]`, typeIds); for (const [index, row] of storylines.entries()) register(row, `storylines[${index}]`, storylineIds); const typeNames = labelCounts(noteTypes, 'name'); const storylineTitles = labelCounts(storylines, 'title'); const readChapter = (chapter: Json, path: string) => { register(chapter, path, chapterIds); if ( chapter.matter !== undefined && chapter.matter !== null && !isMatterPosition(chapter.matter) ) { damaged(`${path}.matter must be front, back, or null`); } enumField(chapter, 'kind', path, ['chapter', 'section']); stringField(chapter, 'synopsis', path); for (const [index, scene] of records(chapter, 'scenes', `${path}.scenes`).entries()) { const scenePath = `${path}.scenes[${index}]`; register(scene, scenePath, sceneIds); enumField(scene, 'status', scenePath, ['drafting', 'revising', 'complete']); stringField(scene, 'synopsis', scenePath); stringField(scene, 'viewpoint', scenePath); namedReference(scene, 'storylineId', 'storyline', scenePath, storylineIds, storylineTitles); } }; for (const [index, book] of records(root, 'books').entries()) { const path = `books[${index}]`; register(book, path, bookIds); for (const [partIndex, part] of records(book, 'parts', `${path}.parts`).entries()) { const partPath = `${path}.parts[${partIndex}]`; register(part, partPath, partIds); for (const [chapterIndex, chapter] of records( part, 'chapters', `${partPath}.chapters` ).entries()) { if (chapter.matter != null) damaged(`${partPath}.chapters[${chapterIndex}] is a matter page inside a part`); readChapter(chapter, `${partPath}.chapters[${chapterIndex}]`); } } for (const [chapterIndex, chapter] of records(book, 'chapters', `${path}.chapters`).entries()) { readChapter(chapter, `${path}.chapters[${chapterIndex}]`); } } for (const [index, note] of notes.entries()) { const path = `notes[${index}]`; register(note, path, noteIds); namedReference(note, 'noteTypeId', 'type', path, typeIds, typeNames); } for (const [index, card] of cards.entries()) { const path = `plotCards[${index}]`; register(card, path, cardIds); stringField(card, 'body', path); reference(card, 'storylineId', path, storylineIds, true); reference(card, 'sceneId', path, sceneIds); reference(card, 'chapterId', path, chapterIds); if (card.sceneId != null && card.chapterId != null) { damaged(`${path} refers to both a scene and a chapter`); } } const links = (key: string, leftKey: string, leftIds: Set) => { const seen = new Set(); for (const [index, link] of records(root, key).entries()) { const path = `${key}[${index}]`; reference(link, leftKey, path, leftIds, true); reference(link, 'noteId', path, noteIds, true); const pair = JSON.stringify([link[leftKey], link.noteId]); if (seen.has(pair)) damaged(`${path} duplicates a link`); seen.add(pair); } }; links('sceneNotes', 'sceneId', sceneIds); links('plotCardNotes', 'cardId', cardIds); // A note holds one picture, and `note_image` is keyed by the note, so a second // row can never be stored. Refusing the duplicate is what stops a file naming // the same note a thousand times from buying a thousand transcodes. const picturedNoteIds = new Set(); for (const [index, noteImage] of records(root, 'noteImages').entries()) { const path = `noteImages[${index}]`; reference(noteImage, 'noteId', path, noteIds, true); const noteId = noteImage.noteId as string; if (picturedNoteIds.has(noteId)) { damaged(`${path}.noteId names a note that already has a picture`); } picturedNoteIds.add(noteId); if (noteImage.imageId !== null && noteImage.imageId !== undefined) { stringField(noteImage, 'imageId', path, true); } stringField(noteImage, 'mime', path); integerField(noteImage, 'bytes', path); integerField(noteImage, 'width', path); integerField(noteImage, 'height', path); stringField(noteImage, 'alt', path); stringField(noteImage, 'originalName', path); } const snapshotIds = new Set(); for (const [index, snapshot] of records(root, 'sceneSnapshots').entries()) { const path = `sceneSnapshots[${index}]`; register(snapshot, path, snapshotIds); reference(snapshot, 'sceneId', path, sceneIds, true); historyBody(snapshot, path); integerField(snapshot, 'wordCount', path, true); enumField(snapshot, 'source', path, SNAPSHOT_SOURCES, true); } const versionIds = new Set(); for (const [index, item] of records(root, 'itemVersions').entries()) { const path = `itemVersions[${index}]`; register(item, path, versionIds); enumField(item, 'kind', path, ['note', 'plot_card'], true); reference(item, 'itemId', path, item.kind === 'note' ? noteIds : cardIds, true); historyBody(item, path); if (typeof item.title !== 'string' || typeof item.summary !== 'string') { damaged(`${path} must keep its title and summary`); } enumField(item, 'source', path, ['edit', 'pre_restore', 'conflict'], true); } const checkpoints = records(root, 'checkpoints'); if (insideCheckpoint && checkpoints.length > 0) { damaged('a project version cannot contain project versions'); } const checkpointIds = new Set(); for (const [index, checkpoint] of checkpoints.entries()) { const path = `checkpoints[${index}]`; register(checkpoint, path, checkpointIds); if (typeof checkpoint.label !== 'string') damaged(`${path}.label must be a string`); enumField(checkpoint, 'source', path, ['manual', 'structure'], true); dateField(checkpoint, 'createdAt', path, false, true); // Checkpoints stop at one level, so a file cannot hide unsupported data deeper down. validateManuscript(checkpoint.manuscript, true); } const feedbackIds = new Set(); for (const [index, item] of records(root, 'feedback').entries()) { const path = `feedback[${index}]`; register(item, path, feedbackIds); reference(item, 'sceneId', path, sceneIds); reference(item, 'chapterId', path, chapterIds); if (Boolean(item.sceneId) === Boolean(item.chapterId)) damaged(`${path} must name one scene or chapter`); if (item.anchorStart != null) integerField(item, 'anchorStart', path, true); for (const key of ['anchorPrefix', 'anchorSuffix']) stringField(item, key, path); for (const key of ['author', 'message', 'quote', 'suggestion']) if (typeof item[key] !== 'string') damaged(`${path}.${key} must be a string`); integerField(item, 'revision', path, true); if (typeof item.resolved !== 'boolean') damaged(`${path}.resolved must be a boolean`); dateField(item, 'createdAt', path, false, true); } return root as ManuscriptJson; } export function parseManuscriptJson(raw: string): ManuscriptJson { let parsed: unknown; try { parsed = JSON.parse(raw); } catch { throw new ImportError('That file is not valid JSON.'); } return validateManuscript(parsed); } function timestamps(row: { createdAt?: string; updatedAt?: string }, now: Date) { return { createdAt: row.createdAt ? new Date(row.createdAt) : now, updatedAt: row.updatedAt ? new Date(row.updatedAt) : now }; } const nullableDate = (value: string | null | undefined) => (value ? new Date(value) : null); const emptyBody = () => ({ type: 'doc', content: [{ type: 'paragraph' }] }); /** A body in the wrong shape comes back walkable; a null body stays null. */ const storedBody = (body: unknown): unknown => body === null ? null : (sanitiseBody(body) ?? emptyBody()); const mapped = (ids: Map, id: string | null | undefined) => id ? (ids.get(id) ?? null) : null; export type StoredImageData = { imageId: string | null; mime: string; bytes: number; width: number; height: number; alt: string; originalName: string; isNewUpload?: boolean; }; /** One picture in an archive, worked out once however many notes name it. */ type PictureOutcome = | { kind: 'stored'; picture: ImageFacts; hadOriginal: boolean } | { kind: 'missing' } | { kind: 'refused'; reason: string }; /** * Restores a project from an archive. The work is linear in the archive: one index * of the zip, one lookup per note, and one stored file per picture no matter how * many notes name it. Each picture the archive expands is counted against * `MAX_ARCHIVE_IMAGE_BYTES` before it is read, because inflating and checksumming * a large entry holds the event loop for the whole read. */ export async function importArchive( ownerId: string, zipBytes: Uint8Array, directory?: Map ): Promise { const entries = directory ?? listZipEntries(zipBytes); const manuscript = parseManuscriptJson(readManuscriptZip(zipBytes, entries)); const { preserveOriginals } = getInstance(); const warnings: string[] = []; const noteTitles = new Map((manuscript.notes ?? []).map((note) => [note.id, note.title])); const storedImagesByNoteId = new Map(); const outcomesByImageId = new Map(); const keepOriginalByImageId = new Map(); let expanded = 0; // One picture can be named by more than one note, so whether its full-size // original is worth keeping is decided once for the file. Deciding per row // would drop an original that another row asked to keep, and say nothing. for (const noteImg of manuscript.noteImages ?? []) { if (!noteImg.imageId) continue; const keep = keepOriginalByImageId.get(noteImg.imageId) ?? false; keepOriginalByImageId.set(noteImg.imageId, keep || (preserveOriginals && noteImg.bytes > 0)); } const readPicture = (entry: ZipEntry | undefined): Uint8Array | null => { if (!entry || entry.uncompressedSize > MAX_IMAGE_BYTES) return null; expanded += entry.uncompressedSize; if (expanded > MAX_ARCHIVE_IMAGE_BYTES) { throw new ImportError( `The pictures inside that zip expand to more than ${MAX_ARCHIVE_IMAGE_BYTES / (1024 * 1024)} MB. Import an archive holding fewer or smaller pictures.` ); } // A damaged entry costs the writer its picture and says so in a warning. The // reader throws for a checksum or a forged size, and neither may stop the // words beside it from arriving. try { return readEntry(zipBytes, entry); } catch { return null; } }; const resolvePicture = async ( imageId: string, keepOriginal: boolean ): Promise => { // An archive writes the display copy of every image, and the full-size // original beside it when the source instance kept one. The display copy is // only the fallback, so expanding it when an original was read buys nothing. const original = readPicture(entries.get(`images/${imageId}.orig`)); const display = original ? null : readPicture(entries.get(`images/${imageId}.webp`)); const source = original ?? display; if (!source) return { kind: 'missing' }; try { const picture = await storeImage(source, { preserveOriginal: keepOriginal && Boolean(original) }); return { kind: 'stored', picture, hadOriginal: Boolean(original) }; } catch (error) { // A picture that fails validation costs the writer that picture. A disk // that refuses the write would leave the project reporting pictures it // does not hold, so it stops the import instead of warning about it. if (!(error instanceof ImageRefusedError)) throw error; return { kind: 'refused', reason: error.message }; } }; try { for (const noteImg of manuscript.noteImages ?? []) { const noteTitle = noteTitles.get(noteImg.noteId) ?? 'Note'; // A row with no picture still carries a description and a file name. The // media type is empty because there is no file whose type it could describe. const rowFacts = (picture: ImageFacts | null): StoredImageData => ({ imageId: picture?.imageId ?? null, mime: picture?.mime ?? '', bytes: picture?.bytes ?? 0, width: picture?.width ?? 0, height: picture?.height ?? 0, alt: noteImg.alt ?? '', originalName: noteImg.originalName ?? '', isNewUpload: Boolean(picture) }); if (!noteImg.imageId) { storedImagesByNoteId.set(noteImg.noteId, rowFacts(null)); continue; } let outcome = outcomesByImageId.get(noteImg.imageId); if (!outcome) { outcome = await resolvePicture( noteImg.imageId, keepOriginalByImageId.get(noteImg.imageId) ?? false ); outcomesByImageId.set(noteImg.imageId, outcome); } if (outcome.kind === 'refused') { warnings.push( `Image for note "${noteTitle}" was omitted because it failed validation: ${outcome.reason}` ); continue; } if (outcome.kind === 'missing') { if (noteImg.bytes > 0) { warnings.push(`Image for note "${noteTitle}" could not be read from archive.`); continue; } storedImagesByNoteId.set(noteImg.noteId, rowFacts(null)); continue; } if (outcome.hadOriginal && noteImg.bytes > 0 && !preserveOriginals) { warnings.push( `Original for note "${noteTitle}" was not preserved because this instance does not preserve originals.` ); } if (noteImg.bytes > 0 && !outcome.hadOriginal) { warnings.push( `The full-size original for note "${noteTitle}" was missing from the archive, so the display copy was restored.` ); } storedImagesByNoteId.set(noteImg.noteId, rowFacts(outcome.picture)); } return importManuscript(ownerId, manuscript, { storedImages: storedImagesByNoteId, warnings }); } catch (error) { // One file per picture, so this removes each written file once. for (const outcome of outcomesByImageId.values()) { if (outcome.kind === 'stored') removeImage(outcome.picture.imageId); } throw error; } } export function importManuscript( ownerId: string, manuscript: ManuscriptJson, options?: { /** Pictures from the file, keyed by the note id the file uses, not the new one. */ storedImages?: Map; warnings?: string[]; } ): ImportResult { // Callers can import checkpoint objects without passing through the JSON parser. validateManuscript(manuscript); const warnings: string[] = [...(options?.warnings ?? [])]; const storedImagesByNoteId = options?.storedImages ?? new Map(); if (!options?.storedImages) { const noteImages = manuscript.noteImages ?? []; // A JSON file holds no image bytes. An id in it names a picture on whichever // instance wrote the file, so adopting the id would attach bytes this project // does not own. Only an archive carries pictures, under new ids. for (const noteImg of noteImages) { storedImagesByNoteId.set(noteImg.noteId, { imageId: null, mime: '', bytes: 0, width: 0, height: 0, alt: noteImg.alt ?? '', originalName: noteImg.originalName ?? '' }); } const named = noteImages.filter((noteImg) => noteImg.imageId).length; if (named > 0) { warnings.push( `${named} ${named === 1 ? 'picture was' : 'pictures were'} not restored. A JSON file holds no picture data, but the .zip export does.` ); } } const now = new Date(); const projectId = crypto.randomUUID(); let chapters = 0; let scenes = 0; let notes = 0; let words = 0; const chapterIds = new Map(); const sceneIds = new Map(); const liveSceneIds: string[] = []; const noteTypeIds = new Map(); const noteIds = new Map(); const storylineIds = new Map(); const cardIds = new Map(); try { db.transaction((tx) => { tx.insert(tables.project) .values({ id: projectId, ownerId, title: manuscript.project.title.trim(), subtitle: manuscript.project.subtitle ?? null, description: manuscript.project.description ?? null, author: manuscript.project.author ?? null, accent: manuscript.project.accent ?? 'rust', ...timestamps(manuscript.project, now) }) .run(); tx.insert(tables.projectMember) .values({ id: crypto.randomUUID(), projectId, userId: ownerId, role: 'owner', createdAt: now }) .run(); const noteTypeByName = new Map(); for (const [index, type] of (manuscript.noteTypes ?? []).entries()) { const id = crypto.randomUUID(); noteTypeIds.set(type.id, id); noteTypeByName.set(type.name, id); tx.insert(tables.noteType) .values({ id, projectId, name: asString(type.name, 'Note'), pluralName: asString(type.pluralName, 'Notes'), accent: asString(type.accent, 'blue-screen'), template: templateOrNull(type.template), ordinal: asNumber(type.ordinal, index) }) .run(); } const storylineByTitle = new Map(); for (const [index, storyline] of (manuscript.storylines ?? []).entries()) { const id = crypto.randomUUID(); storylineIds.set(storyline.id, id); storylineByTitle.set(storyline.title, id); tx.insert(tables.storyline) .values({ id, projectId, title: asString(storyline.title, 'Storyline'), accent: asString(storyline.accent, 'dead-thread'), ordinal: asNumber(storyline.ordinal, index), deletedAt: nullableDate(storyline.deletedAt), ...timestamps(storyline, now) }) .run(); } const writeChapter = ( chapter: ManuscriptJson['books'][number]['chapters'][number], bookId: string, partId: string | null, ordinal: number, parentLive: boolean ) => { const chapterId = crypto.randomUUID(); chapterIds.set(chapter.id, chapterId); const live = parentLive && !chapter.deletedAt; tx.insert(tables.chapter) .values({ id: chapterId, bookId, partId, title: asString(chapter.title, 'Chapter'), kind: chapter.matter || chapter.kind === 'section' ? 'section' : 'chapter', matter: chapter.matter === 'front' || chapter.matter === 'back' ? chapter.matter : null, synopsis: asString(chapter.synopsis), ordinal, deletedAt: nullableDate(chapter.deletedAt), ...timestamps(chapter, now) }) .run(); if (live) chapters += 1; for (const [sceneIndex, scene] of (chapter.scenes ?? []).entries()) { const sceneId = crypto.randomUUID(); sceneIds.set(scene.id, sceneId); const body = scene.body === undefined ? emptyBody() : storedBody(scene.body); const bodyText = scene.bodyText ?? bodyToText(body); const wordCount = scene.wordCount ?? countWords(bodyText); const storylineId = Object.hasOwn(scene, 'storylineId') ? mapped(storylineIds, scene.storylineId) : (storylineByTitle.get(scene.storyline ?? '') ?? null); tx.insert(tables.scene) .values({ id: sceneId, chapterId, title: asString(scene.title, 'Scene'), body, bodyText, wordCount, revision: scene.revision ?? 0, status: scene.status === 'revising' || scene.status === 'complete' ? scene.status : 'drafting', synopsis: asString(scene.synopsis), viewpoint: asString(scene.viewpoint), storylineId, lastOpenedAt: nullableDate(scene.lastOpenedAt), ordinal: asNumber(scene.ordinal, sceneIndex), deletedAt: nullableDate(scene.deletedAt), ...timestamps(scene, now) }) .run(); if (live && !scene.deletedAt) { scenes += 1; words += wordCount; liveSceneIds.push(sceneId); } } }; for (const [bookIndex, book] of (manuscript.books ?? []).entries()) { const bookId = crypto.randomUUID(); tx.insert(tables.book) .values({ id: bookId, projectId, title: asString(book.title, 'Book'), ordinal: asNumber(book.ordinal, bookIndex), deletedAt: nullableDate(book.deletedAt), ...timestamps(book, now) }) .run(); for (const [partIndex, part] of (book.parts ?? []).entries()) { const partId = crypto.randomUUID(); tx.insert(tables.part) .values({ id: partId, bookId, title: asString(part.title, 'Part'), ordinal: asNumber(part.ordinal, partIndex), deletedAt: nullableDate(part.deletedAt), ...timestamps(part, now) }) .run(); for (const [index, chapter] of (part.chapters ?? []).entries()) { writeChapter( chapter, bookId, partId, asNumber(chapter.ordinal, index), !book.deletedAt && !part.deletedAt ); } } for (const [index, chapter] of (book.chapters ?? []).entries()) { writeChapter(chapter, bookId, null, asNumber(chapter.ordinal, index), !book.deletedAt); } } for (const [index, note] of (manuscript.notes ?? []).entries()) { const id = crypto.randomUUID(); noteIds.set(note.id, id); const noteTypeId = Object.hasOwn(note, 'noteTypeId') ? mapped(noteTypeIds, note.noteTypeId) : (noteTypeByName.get(note.type ?? '') ?? null); const body = note.body === undefined ? emptyBody() : storedBody(note.body); tx.insert(tables.note) .values({ id, projectId, noteTypeId, title: asString(note.title, 'Note'), summary: asString(note.summary), body, bodyText: note.bodyText ?? bodyToText(body), revision: note.revision ?? 0, ordinal: asNumber(note.ordinal, index), deletedAt: nullableDate(note.deletedAt), ...timestamps(note, now) }) .run(); if (!note.deletedAt) notes += 1; const img = storedImagesByNoteId.get(note.id); if (img) { tx.insert(tables.noteImage) .values({ noteId: id, imageId: img.imageId, mime: img.mime, bytes: img.bytes, width: img.width, height: img.height, alt: img.alt, originalName: img.originalName }) .run(); } } for (const [index, card] of (manuscript.plotCards ?? []).entries()) { const id = crypto.randomUUID(); cardIds.set(card.id, id); tx.insert(tables.plotCard) .values({ id, storylineId: storylineIds.get(card.storylineId)!, sceneId: mapped(sceneIds, card.sceneId), chapterId: mapped(chapterIds, card.chapterId), title: asString(card.title), body: asString(card.body), ordinal: asNumber(card.ordinal, index), deletedAt: nullableDate(card.deletedAt), ...timestamps(card, now) }) .run(); } for (const link of manuscript.sceneNotes ?? []) { tx.insert(tables.sceneNote) .values({ sceneId: sceneIds.get(link.sceneId)!, noteId: noteIds.get(link.noteId)! }) .run(); } for (const link of manuscript.plotCardNotes ?? []) { tx.insert(tables.plotCardNote) .values({ cardId: cardIds.get(link.cardId)!, noteId: noteIds.get(link.noteId)! }) .run(); } for (const item of manuscript.feedback ?? []) { tx.insert(tables.sceneFeedback) .values({ ...item, id: crypto.randomUUID(), projectId, sceneId: mapped(sceneIds, item.sceneId), chapterId: mapped(chapterIds, item.chapterId), shareId: null, createdAt: new Date(item.createdAt) }) .run(); } for (const snapshot of manuscript.sceneSnapshots ?? []) { tx.insert(tables.sceneSnapshot) .values({ id: crypto.randomUUID(), sceneId: sceneIds.get(snapshot.sceneId)!, body: storedBody(snapshot.body), bodyText: snapshot.bodyText, wordCount: snapshot.wordCount, label: snapshot.label ?? null, source: snapshot.source, createdAt: new Date(snapshot.createdAt) }) .run(); } for (const item of manuscript.itemVersions ?? []) { tx.insert(tables.itemVersion) .values({ id: crypto.randomUUID(), projectId, kind: item.kind, itemId: (item.kind === 'note' ? noteIds : cardIds).get(item.itemId)!, title: item.title, summary: item.summary, body: item.kind === 'note' ? storedBody(item.body) : item.body, bodyText: item.bodyText, source: item.source, createdAt: new Date(item.createdAt) }) .run(); } for (const checkpoint of manuscript.checkpoints ?? []) { tx.insert(tables.projectCheckpoint) .values({ id: crypto.randomUUID(), projectId, label: checkpoint.label, source: checkpoint.source, manuscript: checkpoint.manuscript, createdAt: new Date(checkpoint.createdAt) }) .run(); } // The index shares this connection, so a failed index write rolls back the import too. for (const sceneId of liveSceneIds) indexScene(sceneId); for (const noteId of noteIds.values()) indexNote(noteId); }); } catch (error) { for (const img of storedImagesByNoteId.values()) { if (img.imageId && img.isNewUpload) { removeImage(img.imageId); } } throw error; } return { projectId, title: manuscript.project.title.trim(), chapters, scenes, notes, words, warnings: warnings.length > 0 ? warnings : undefined }; } /** * Writes an already-prepared project (chapters and scenes with normalised/derived * body metrics) in one transaction, enforcing search-index creation via * `insertIndexedScene`. Shared by Markdown and Word imports. */ export function commitPreparedProject( ownerId: string, prepared: PreparedSimpleProject ): ImportResult { const now = new Date(); const projectId = crypto.randomUUID(); let words = 0; let scenesCount = 0; db.transaction((tx) => { tx.insert(tables.project) .values({ id: projectId, ownerId, title: prepared.title, createdAt: now, updatedAt: now }) .run(); tx.insert(tables.projectMember) .values({ id: crypto.randomUUID(), projectId, userId: ownerId, role: 'owner', createdAt: now }) .run(); const bookId = crypto.randomUUID(); tx.insert(tables.book) .values({ id: bookId, projectId, title: prepared.title, ordinal: 0, createdAt: now, updatedAt: now }) .run(); let chapterOrdinal = 0; for (const chapter of prepared.chapters) { const chapterId = crypto.randomUUID(); tx.insert(tables.chapter) .values({ id: chapterId, bookId, title: chapter.title, ordinal: chapterOrdinal++, createdAt: now, updatedAt: now }) .run(); let sceneOrdinal = 0; for (const scene of chapter.scenes) { insertIndexedScene(tx, { chapterId, title: scene.title, ordinal: sceneOrdinal++, prepared: { body: scene.body, bodyText: scene.bodyText, wordCount: scene.wordCount }, createdAt: now, updatedAt: now }); words += scene.wordCount; scenesCount += 1; } } }); return { projectId, title: prepared.title, chapters: prepared.chapters.length, scenes: scenesCount, notes: 0, words }; }