From ba4683f867ddf49427cabf77462c588cbc62c4e6 Mon Sep 17 00:00:00 2001 From: Bobban Rydh Date: Mon, 26 Oct 2026 19:51:05 +0100 Subject: [PATCH] =?UTF-8?q?Export=20och=20import=20av=20hela=20b=C3=B6cker?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Export som zip (book.json + bilder) eller bara JSON, ersätter den gamla JSON-exporten - Import via /api/books/import skapar alltid en ny bok med nya id och pekar om kopplingar (relationer, grupper, kopplingsfält, markeringar, scheman) - Bevarar ID-nummer och räknar om räknarna, hoppar över ogiltiga bilder och länkar - Kontroll av filtyp på innehållet, gränser mot zip-bomber och Origin-kontroll - Höjd gräns för stora uppladdningar (serverActions och middlewareClientMaxBodySize) - Nytt beroende: fflate. Importkort på startsidan, exportknappar i bokinställningar, README Co-Authored-By: Claude Sonnet 5.5 --- README.md | 13 + next.config.mjs | 6 +- package-lock.json | 7 + package.json | 1 + src/app/(main)/page.tsx | 31 +- src/app/api/books/[bookId]/export/route.ts | 45 +- src/app/api/books/import/route.ts | 52 ++ src/app/books/[bookId]/page.tsx | 15 +- src/app/books/[bookId]/settings/page.tsx | 14 +- src/i18n/en.ts | 16 +- src/i18n/sv.ts | 16 +- src/lib/book-transfer.ts | 614 +++++++++++++++++++++ src/lib/files.ts | 31 +- src/lib/ids.ts | 2 +- 14 files changed, 831 insertions(+), 32 deletions(-) create mode 100644 src/app/api/books/import/route.ts create mode 100644 src/lib/book-transfer.ts diff --git a/README.md b/README.md index b4f459d..6d993b4 100644 --- a/README.md +++ b/README.md @@ -117,6 +117,19 @@ Standardmallarna använder det för Lokal och Skiss i Företag, Lokal, Bostad oc Ändrar du en mall försvinner ingen data: värden för borttagna fält finns kvar i databasen. +## Export och import + +Under *Bokinställningar* exporterar du hela boken: + +- **Med bilder (.zip):** innehåller `book.json` och alla bilder i `files/`. Det är den fullständiga säkerhetskopian. +- **Bara data (.json):** samma innehåll utan bilder, för snabb jämförelse eller redigering. + +Exporten innehåller poster (med ID, taggar och mallfält), relationer med deltagare, scheman, kartor med markeringar, mallar och bilderna. + +På startsidan *Mina böcker* importerar du en exportfil (.zip eller .json). Importen **skapar alltid en ny bok** och skriver aldrig över något. Alla poster får nya interna id, och kopplingar mellan dem (relationer, kopplingsfält i mallar, markeringar och scheman) pekar om till de nya. ID som KI-0001 behålls, och räknarna fortsätter därefter. Finns redan en bok med samma titel får den nya tillägget *(import)*. Poster, bilder och kopplingar som saknas eller är ogiltiga i filen hoppas över, och antalet visas efteråt. Filen kan flyttas mellan installationer, till exempel från dev till prod. + +Gränser: filer upp till 300 MB, bilder upp till 16 MB styck och 20 000 poster per sorts innehåll. Importen läser bara `book.json` och bilder under `files/` ur zip-filen, och bildtyp kontrolleras på filens innehåll. Ligger appen bakom en omvänd proxy måste den tillåta stora uppladdningar (i Nginx Proxy Manager: `client_max_body_size 0;`). + ## Automatiska ID Alla ID skapas automatiskt enligt ID-översikten, med löpande nummer per bok och typ (de börjar om i varje ny bok). ID tilldelas första gången posten sparas och ändras aldrig därefter. diff --git a/next.config.mjs b/next.config.mjs index 4315361..72d72aa 100644 --- a/next.config.mjs +++ b/next.config.mjs @@ -5,8 +5,10 @@ const nextConfig = { eslint: { ignoreDuringBuilds: true }, experimental: { optimizePackageImports: ["@tabler/icons-react"], - // Kartbilder laddas upp via server actions - serverActions: { bodySizeLimit: "20mb" }, + // Kartbilder och importfiler kan vara stora. Middleware (inloggningskontrollen) buffrar förfrågningar och + // kapar dem som standard efter 10 MB, så gränsen måste höjas dessutom. + serverActions: { bodySizeLimit: "300mb" }, + middlewareClientMaxBodySize: "300mb", }, }; diff --git a/package-lock.json b/package-lock.json index a2fc1c7..1ec2368 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "@prisma/client": "^6.19.3", "@tabler/core": "^1.7.0", "@tabler/icons-react": "^3.49.0", + "fflate": "^0.8.2", "leaflet": "^1.9.4", "next": "^15.5.28", "next-auth": "5.0.0-beta.32", @@ -1170,6 +1171,12 @@ "node": ">=8.0.0" } }, + "node_modules/fflate": { + "version": "0.8.3", + "resolved": "https://registry.npmjs.org/fflate/-/fflate-0.8.3.tgz", + "integrity": "sha512-tbZNuJrLwGUp3zshBtdy4W+ORxZuIh8a5ilyIEQDC5rY1f3U20JMry0Ll3WBzU58EZKsEuJFXhb5gwv8CsPvgA==", + "license": "MIT" + }, "node_modules/giget": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/giget/-/giget-2.0.0.tgz", diff --git a/package.json b/package.json index a5f7460..6115949 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "@prisma/client": "^6.19.3", "@tabler/core": "^1.7.0", "@tabler/icons-react": "^3.49.0", + "fflate": "^0.8.2", "leaflet": "^1.9.4", "next": "^15.5.28", "next-auth": "5.0.0-beta.32", diff --git a/src/app/(main)/page.tsx b/src/app/(main)/page.tsx index 3255bc6..94ac923 100644 --- a/src/app/(main)/page.tsx +++ b/src/app/(main)/page.tsx @@ -1,6 +1,7 @@ import Link from "next/link"; import { IconBook2 } from "@tabler/icons-react"; import { createBook } from "@/actions/books"; +import { IMPORT_ERROR_CODES, type ImportErrorCode } from "@/lib/book-transfer"; import { EmptyState, PageBody, PageHeader } from "@/components/page"; import { getT } from "@/i18n/server"; import { prisma } from "@/lib/db"; @@ -8,7 +9,12 @@ import { requireUser } from "@/lib/session"; export const metadata = { title: "Mina böcker" }; -export default async function BooksPage() { +export default async function BooksPage({ + searchParams, +}: { + searchParams: Promise<{ importError?: string; detail?: string }>; +}) { + const { importError, detail } = await searchParams; const user = await requireUser(); const { t } = await getT(); const books = await prisma.book.findMany({ @@ -21,6 +27,14 @@ export default async function BooksPage() { <> + {importError ? ( +
+ {t("import.failed", { + reason: t(`import.error.${IMPORT_ERROR_CODES.includes(importError as ImportErrorCode) ? importError : "invalid"}`), + })} + {detail ? ({detail.slice(0, 80)}) : null} +
+ ) : null}
{books.length === 0 ? ( @@ -82,6 +96,21 @@ export default async function BooksPage() {
+ +
+
+

{t("import.title")}

+
+
+

{t("import.help")}

+ +
+
+ +
+
diff --git a/src/app/api/books/[bookId]/export/route.ts b/src/app/api/books/[bookId]/export/route.ts index 010cf88..9b65f87 100644 --- a/src/app/api/books/[bookId]/export/route.ts +++ b/src/app/api/books/[bookId]/export/route.ts @@ -1,30 +1,39 @@ -import { getCurrentUser } from "@/lib/session"; +import { strToU8, zipSync, type Zippable } from "fflate"; +import { buildExport } from "@/lib/book-transfer"; import { prisma } from "@/lib/db"; +import { getCurrentUser } from "@/lib/session"; export const dynamic = "force-dynamic"; -export async function GET(_req: Request, { params }: { params: Promise<{ bookId: string }> }) { +// GET /api/books//export zip med book.json och bilder (standard) +// GET /api/books//export?format=json bara data, utan bilder +export async function GET(req: Request, { params }: { params: Promise<{ bookId: string }> }) { const user = await getCurrentUser(); if (!user) return new Response("Unauthorized", { status: 401 }); const { bookId } = await params; - const book = await prisma.book.findFirst({ - where: { id: bookId, ownerId: user.id }, - include: { - entities: true, - relations: { include: { members: true } }, - shifts: true, - templates: true, - maps: { include: { markers: true } }, - }, - }); + const book = await prisma.book.findFirst({ where: { id: bookId, ownerId: user.id }, select: { title: true } }); if (!book) return new Response("Not found", { status: 404 }); + const asJson = new URL(req.url).searchParams.get("format") === "json"; + const { data, files } = await buildExport(bookId, !asJson); + const json = JSON.stringify(data, null, 2); + const safeName = book.title.replace(/[^\p{L}\p{N}]+/gu, "-").replace(/^-|-$/g, "").slice(0, 60) || "book"; - return new Response(JSON.stringify({ exportedAt: new Date().toISOString(), book }, null, 2), { - headers: { - "Content-Type": "application/json; charset=utf-8", - "Content-Disposition": `attachment; filename="${safeName}.json"; filename*=UTF-8''${encodeURIComponent(safeName)}.json`, - }, - }); + const date = new Date().toISOString().slice(0, 10); + const filename = `${safeName}-${date}.${asJson ? "json" : "zip"}`; + const headers = { + "Content-Disposition": `attachment; filename="${filename.replace(/[^\x20-\x7e]/g, "_")}"; filename*=UTF-8''${encodeURIComponent(filename)}`, + "Cache-Control": "no-store", + }; + + if (asJson) { + return new Response(json, { headers: { ...headers, "Content-Type": "application/json; charset=utf-8" } }); + } + + // Bilder är redan komprimerade, så de lagras utan ny komprimering + const entries: Zippable = { "book.json": strToU8(json) }; + for (const [name, bytes] of files) entries[`files/${name}`] = [bytes, { level: 0 }]; + const zip = zipSync(entries); + return new Response(new Uint8Array(zip), { headers: { ...headers, "Content-Type": "application/zip" } }); } diff --git a/src/app/api/books/import/route.ts b/src/app/api/books/import/route.ts new file mode 100644 index 0000000..57ae709 --- /dev/null +++ b/src/app/api/books/import/route.ts @@ -0,0 +1,52 @@ +import { ImportError, MAX_IMPORT_BYTES, importBook, readImportFile } from "@/lib/book-transfer"; +import { getCurrentUser } from "@/lib/session"; + +export const dynamic = "force-dynamic"; + +const redirectTo = (path: string) => new Response(null, { status: 303, headers: { Location: path } }); + +/** Origin måste höra till samma adress som appen, så att en annan webbplats inte kan skicka in en import. */ +function sameOrigin(req: Request): boolean { + const origin = req.headers.get("origin"); + if (!origin) return true; + let host: string; + try { + host = new URL(origin).host; + } catch { + return false; + } + const allowed = [req.headers.get("host"), req.headers.get("x-forwarded-host"), process.env.AUTH_URL ? new URL(process.env.AUTH_URL).host : null]; + return allowed.includes(host); +} + +// POST /api/books/import (multipart, fältet "file"): skapar en ny bok av en exportfil +export async function POST(req: Request) { + const user = await getCurrentUser(); + if (!user) return new Response("Unauthorized", { status: 401 }); + if (!sameOrigin(req)) return new Response("Forbidden", { status: 403 }); + + try { + const form = await req.formData(); + const file = form.get("file"); + if (typeof file === "string" || !file || file.size === 0) throw new ImportError("empty"); + if (file.size > MAX_IMPORT_BYTES) throw new ImportError("too-large"); + + const { data, files } = readImportFile(new Uint8Array(await file.arrayBuffer())); + const result = await importBook(user.id, data, files); + + const query = new URLSearchParams({ + imported: String(result.entities), + relations: String(result.relations), + maps: String(result.maps), + skipped: String(result.skippedImages + result.skippedLinks), + }); + return redirectTo(`/books/${result.bookId}?${query}`); + } catch (error) { + if (!(error instanceof ImportError)) console.error("Import failed", error); + const query = new URLSearchParams({ + importError: error instanceof ImportError ? error.code : "invalid", + ...(error instanceof ImportError && error.detail ? { detail: error.detail } : {}), + }); + return redirectTo(`/?${query}`); + } +} diff --git a/src/app/books/[bookId]/page.tsx b/src/app/books/[bookId]/page.tsx index 268e217..5661451 100644 --- a/src/app/books/[bookId]/page.tsx +++ b/src/app/books/[bookId]/page.tsx @@ -7,8 +7,15 @@ import { prisma } from "@/lib/db"; import { KIND_INFO, KINDS } from "@/lib/kinds"; import { requireBook } from "@/lib/session"; -export default async function BookDashboard({ params }: { params: Promise<{ bookId: string }> }) { +export default async function BookDashboard({ + params, + searchParams, +}: { + params: Promise<{ bookId: string }>; + searchParams: Promise<{ imported?: string; relations?: string; maps?: string; skipped?: string }>; +}) { const { bookId } = await params; + const { imported, relations, maps, skipped } = await searchParams; const book = await requireBook(bookId); const { t } = await getT(); @@ -25,6 +32,12 @@ export default async function BookDashboard({ params }: { params: Promise<{ book pretitle={[book.subtitle, book.genre].filter(Boolean).join(" · ") || undefined} /> + {imported !== undefined ? ( +
+ {t("import.done", { entities: Number(imported) || 0, relations: Number(relations) || 0, maps: Number(maps) || 0 })} + {Number(skipped) > 0 ?
{t("import.skipped", { n: Number(skipped) })}
: null} +
+ ) : null} {book.description ?

{book.description}

: null}
diff --git a/src/app/books/[bookId]/settings/page.tsx b/src/app/books/[bookId]/settings/page.tsx index 6419443..4d779b8 100644 --- a/src/app/books/[bookId]/settings/page.tsx +++ b/src/app/books/[bookId]/settings/page.tsx @@ -52,10 +52,16 @@ export default async function SettingsPage({ params }: { params: Promise<{ bookI
diff --git a/src/i18n/en.ts b/src/i18n/en.ts index 73ef045..8eddeda 100644 --- a/src/i18n/en.ts +++ b/src/i18n/en.ts @@ -314,6 +314,18 @@ export const en: Record = { "settings.danger": "Danger zone", "settings.deleteBook": "Delete book", "settings.deleteBookHelp": "Permanently deletes the book and all its content. Export first if you want a copy.", - "settings.export": "Export as JSON", - "settings.exportHelp": "Downloads all items, relationships, schedules, maps and templates in the book.", + "settings.export": "Export with images (.zip)", + "settings.exportJson": "Export data only (.json)", + "import.title": "Import book", + "import.help": "Choose an export file (.zip or .json). The book is imported as a new book with new internal IDs, nothing is overwritten.", + "import.button": "Import", + "import.failed": "The import failed: {reason}", + "import.error.not-export": "the file is not an export from Story Planner", + "import.error.version": "the export file version is not supported", + "import.error.too-large": "the file is too large", + "import.error.empty": "no file was chosen or the file is empty", + "import.error.invalid": "the file is damaged or has the wrong format", + "import.done": "The book was imported: {entities} items, {relations} relationships and {maps} maps.", + "import.skipped": "{n} images or links were missing from the file and were skipped.", + "settings.exportHelp": "Downloads the whole book: items, relationships, schedules, maps, templates and images. The zip file can be imported as a new book here or in another installation. The JSON variant contains data only, without images.", }; diff --git a/src/i18n/sv.ts b/src/i18n/sv.ts index bc370dd..fc39c05 100644 --- a/src/i18n/sv.ts +++ b/src/i18n/sv.ts @@ -312,6 +312,18 @@ export const sv = { "settings.danger": "Farlig zon", "settings.deleteBook": "Ta bort boken", "settings.deleteBookHelp": "Raderar boken och allt innehåll permanent. Exportera först om du vill spara en kopia.", - "settings.export": "Exportera som JSON", - "settings.exportHelp": "Laddar ner alla poster, relationer, scheman, kartor och mallar i boken.", + "settings.export": "Exportera med bilder (.zip)", + "settings.exportJson": "Exportera bara data (.json)", + "import.title": "Importera bok", + "import.help": "Välj en exportfil (.zip eller .json). Boken importeras som en ny bok med nya ID-nummer i systemet, ingenting skrivs över.", + "import.button": "Importera", + "import.failed": "Importen misslyckades: {reason}", + "import.error.not-export": "filen är inte en exportfil från Bokplanerare", + "import.error.version": "exportfilens version stöds inte", + "import.error.too-large": "filen är för stor", + "import.error.empty": "ingen fil valdes eller filen är tom", + "import.error.invalid": "filen är skadad eller har fel format", + "import.done": "Boken importerades: {entities} poster, {relations} relationer och {maps} kartor.", + "import.skipped": "{n} bilder eller kopplingar saknades i filen och hoppades över.", + "settings.exportHelp": "Laddar ner hela boken: poster, relationer, scheman, kartor, mallar och bilder. Zip-filen kan importeras som en ny bok här eller i en annan installation. JSON-varianten innehåller bara data, utan bilder.", } as const; diff --git a/src/lib/book-transfer.ts b/src/lib/book-transfer.ts new file mode 100644 index 0000000..9c1eba7 --- /dev/null +++ b/src/lib/book-transfer.ts @@ -0,0 +1,614 @@ +import { randomUUID } from "node:crypto"; +import { strFromU8, unzipSync } from "fflate"; +import type { EntityKind, Prisma, WeekPattern } from "@prisma/client"; +import { prisma } from "./db"; +import { deleteImage, readImageBytes, saveImageBytes } from "./files"; +import { ENTITY_PREFIX, MAP_PREFIX, MARKER_PREFIX, RELATION_PREFIX, SCHEDULE_PREFIX, mapLetter, pad } from "./ids"; +import { MARKER_COLORS, parseKind, parseTemplateKind } from "./kinds"; +import { validateFields, type FieldDef } from "./templates"; + +// Export och import av en hel bok. Filen är en zip med book.json och bilderna i files/, +// eller bara JSON utan bilder. Import skapar alltid en ny bok med nya id och skriver aldrig över något. + +export const EXPORT_FORMAT = "bookmanager-export"; +export const EXPORT_VERSION = 1; +export const MAX_IMPORT_BYTES = 300 * 1024 * 1024; +const MAX_FILE_BYTES = 16 * 1024 * 1024; +const MAX_ITEMS = 20000; + +export type ImportErrorCode = "not-export" | "version" | "too-large" | "empty" | "invalid"; +export const IMPORT_ERROR_CODES: ImportErrorCode[] = ["not-export", "version", "too-large", "empty", "invalid"]; + +export class ImportError extends Error { + constructor( + public code: ImportErrorCode, + public detail = "", + ) { + super(`${code}${detail ? `: ${detail}` : ""}`); + } +} + +type Json = Prisma.InputJsonValue; + +export type ExportData = { + format: typeof EXPORT_FORMAT; + version: number; + exportedAt: string; + includesImages: boolean; + book: { title: string; subtitle: string | null; genre: string | null; description: string | null }; + entities: { + id: string; + kind: EntityKind; + name: string; + code: string | null; + summary: string | null; + tags: string[]; + data: Record; + image: string | null; + }[]; + relations: { + id: string; + code: string | null; + fromId: string; + toId: string; + type: string; + description: string | null; + data: Record; + members: string[]; + }[]; + shifts: { + personId: string; + workplaceId: string | null; + weekday: number; + startTime: string; + endTime: string; + weeks: WeekPattern; + label: string | null; + scheduleCode: string | null; + }[]; + maps: { + id: string; + code: string | null; + name: string; + kind: "GEO" | "IMAGE"; + image: string | null; + centerLat: number | null; + centerLng: number | null; + zoom: number | null; + markers: { + code: string | null; + entityId: string | null; + label: string; + note: string | null; + color: string; + lat: number; + lng: number; + }[]; + }[]; + templates: { kind: string; fields: FieldDef[] }[]; +}; + +// ─── Export ───────────────────────────────────────────────────────────────── + +export async function buildExport(bookId: string, withImages: boolean) { + const book = await prisma.book.findUniqueOrThrow({ + where: { id: bookId }, + include: { + entities: { orderBy: { createdAt: "asc" } }, + relations: { orderBy: { createdAt: "asc" }, include: { members: true } }, + shifts: { orderBy: { createdAt: "asc" } }, + maps: { orderBy: { createdAt: "asc" }, include: { markers: { orderBy: { createdAt: "asc" } } } }, + templates: true, + }, + }); + + const files = new Map(); + const image = async (name: string | null): Promise => { + if (!withImages || !name) return null; + const bytes = await readImageBytes(name); + if (!bytes) return null; + files.set(name, bytes); + return name; + }; + + const entities: ExportData["entities"] = []; + for (const e of book.entities) { + entities.push({ + id: e.id, + kind: e.kind, + name: e.name, + code: e.code, + summary: e.summary, + tags: e.tags, + data: (e.data ?? {}) as Record, + image: await image(e.imageFile), + }); + } + + const maps: ExportData["maps"] = []; + for (const m of book.maps) { + maps.push({ + id: m.id, + code: m.code, + name: m.name, + kind: m.kind, + image: await image(m.imageFile), + centerLat: m.centerLat, + centerLng: m.centerLng, + zoom: m.zoom, + markers: m.markers.map((k) => ({ + code: k.code, + entityId: k.entityId, + label: k.label, + note: k.note, + color: k.color, + lat: k.lat, + lng: k.lng, + })), + }); + } + + const data: ExportData = { + format: EXPORT_FORMAT, + version: EXPORT_VERSION, + exportedAt: new Date().toISOString(), + includesImages: withImages, + book: { title: book.title, subtitle: book.subtitle, genre: book.genre, description: book.description }, + entities, + relations: book.relations.map((r) => ({ + id: r.id, + code: r.code, + fromId: r.fromId, + toId: r.toId, + type: r.type, + description: r.description, + data: (r.data ?? {}) as Record, + members: r.members.map((m) => m.entityId), + })), + shifts: book.shifts.map((s) => ({ + personId: s.personId, + workplaceId: s.workplaceId, + weekday: s.weekday, + startTime: s.startTime, + endTime: s.endTime, + weeks: s.weeks, + label: s.label, + scheduleCode: s.scheduleCode, + })), + maps, + templates: book.templates.map((t) => ({ kind: t.kind, fields: t.fields as unknown as FieldDef[] })), + }; + return { data, files }; +} + +// ─── Läsa och kontrollera en importfil ────────────────────────────────────── + +const isObj = (v: unknown): v is Record => typeof v === "object" && v !== null && !Array.isArray(v); + +function text(v: unknown, max: number): string | null { + if (typeof v !== "string") return null; + const t = v.trim().slice(0, max); + return t || null; +} + +function required(v: unknown, max: number, what: string): string { + const t = text(v, max); + if (!t) throw new ImportError("invalid", what); + return t; +} + +function finite(v: unknown): number | null { + return typeof v === "number" && Number.isFinite(v) ? v : null; +} + +function list(v: unknown, what: string): unknown[] { + if (v === undefined || v === null) return []; + if (!Array.isArray(v)) throw new ImportError("invalid", what); + if (v.length > MAX_ITEMS) throw new ImportError("too-large", what); + return v; +} + +function object(v: unknown): Record { + return isObj(v) ? (v as Record) : {}; +} + +export function parseExport(raw: unknown): ExportData { + if (!isObj(raw) || raw.format !== EXPORT_FORMAT) throw new ImportError("not-export"); + if (raw.version !== EXPORT_VERSION) throw new ImportError("version", String(raw.version)); + const book = isObj(raw.book) ? raw.book : {}; + + const entities = list(raw.entities, "entities").map((item, i): ExportData["entities"][number] => { + if (!isObj(item)) throw new ImportError("invalid", `entities[${i}]`); + const kind = parseKind(item.kind); + if (!kind) throw new ImportError("invalid", `entities[${i}].kind`); + return { + id: required(item.id, 100, `entities[${i}].id`), + kind, + name: required(item.name, 200, `entities[${i}].name`), + code: text(item.code, 50), + summary: text(item.summary, 20000), + tags: Array.isArray(item.tags) ? item.tags.map((t) => text(t, 40)).filter((t): t is string => !!t).slice(0, 30) : [], + data: object(item.data), + image: text(item.image, 120), + }; + }); + + const relations = list(raw.relations, "relations").map((item, i): ExportData["relations"][number] => { + if (!isObj(item)) throw new ImportError("invalid", `relations[${i}]`); + return { + id: text(item.id, 100) ?? randomUUID(), + code: text(item.code, 50), + fromId: required(item.fromId, 100, `relations[${i}].fromId`), + toId: required(item.toId, 100, `relations[${i}].toId`), + type: required(item.type, 100, `relations[${i}].type`), + description: text(item.description, 2000), + data: object(item.data), + members: Array.isArray(item.members) ? item.members.map((m) => text(m, 100)).filter((m): m is string => !!m) : [], + }; + }); + + const shifts = list(raw.shifts, "shifts").map((item, i): ExportData["shifts"][number] => { + if (!isObj(item)) throw new ImportError("invalid", `shifts[${i}]`); + const weekday = finite(item.weekday); + const time = /^([01]\d|2[0-3]):[0-5]\d$/; + const start = text(item.startTime, 5); + const end = text(item.endTime, 5); + if (weekday === null || !Number.isInteger(weekday) || weekday < 0 || weekday > 6 || !start || !end || !time.test(start) || !time.test(end)) { + throw new ImportError("invalid", `shifts[${i}]`); + } + return { + personId: required(item.personId, 100, `shifts[${i}].personId`), + workplaceId: text(item.workplaceId, 100), + weekday, + startTime: start, + endTime: end, + weeks: item.weeks === "ODD" || item.weeks === "EVEN" ? item.weeks : "ALL", + label: text(item.label, 100), + scheduleCode: text(item.scheduleCode, 50), + }; + }); + + const maps = list(raw.maps, "maps").map((item, i): ExportData["maps"][number] => { + if (!isObj(item)) throw new ImportError("invalid", `maps[${i}]`); + const markers = list(item.markers, `maps[${i}].markers`).map((m, j) => { + if (!isObj(m)) throw new ImportError("invalid", `maps[${i}].markers[${j}]`); + const lat = finite(m.lat); + const lng = finite(m.lng); + if (lat === null || lng === null) throw new ImportError("invalid", `maps[${i}].markers[${j}] position`); + return { + code: text(m.code, 50), + entityId: text(m.entityId, 100), + label: required(m.label, 200, `maps[${i}].markers[${j}].label`), + note: text(m.note, 5000), + color: typeof m.color === "string" && m.color in MARKER_COLORS ? m.color : "blue", + lat, + lng, + }; + }); + return { + id: text(item.id, 100) ?? randomUUID(), + code: text(item.code, 50), + name: required(item.name, 200, `maps[${i}].name`), + kind: item.kind === "IMAGE" ? "IMAGE" : "GEO", + image: text(item.image, 120), + centerLat: finite(item.centerLat), + centerLng: finite(item.centerLng), + zoom: finite(item.zoom) === null ? null : Math.round(item.zoom as number), + markers, + }; + }); + + const templates = list(raw.templates, "templates").flatMap((item): ExportData["templates"] => { + if (!isObj(item)) return []; + const kind = parseTemplateKind(item.kind); + if (!kind) return []; + try { + return [{ kind, fields: validateFields(item.fields) }]; + } catch { + return []; // trasig mall: boken får standardmallen + } + }); + + return { + format: EXPORT_FORMAT, + version: EXPORT_VERSION, + exportedAt: text(raw.exportedAt, 40) ?? "", + includesImages: raw.includesImages === true, + book: { + title: required(book.title, 200, "book.title"), + subtitle: text(book.subtitle, 200), + genre: text(book.genre, 100), + description: text(book.description, 20000), + }, + entities, + relations, + shifts, + maps, + templates, + }; +} + +/** Läser en uppladdad fil: zip (book.json + files/) eller ren JSON. */ +export function readImportFile(bytes: Uint8Array): { data: ExportData; files: Map } { + if (bytes.length === 0) throw new ImportError("empty"); + if (bytes.length > MAX_IMPORT_BYTES) throw new ImportError("too-large"); + + const isZip = bytes[0] === 0x50 && bytes[1] === 0x4b; + const files = new Map(); + let json: string; + + if (isZip) { + let total = 0; + let count = 0; + let entries: Record; + try { + entries = unzipSync(bytes, { + // Bara book.json och bilder under files/ packas upp, med gränser mot zip-bomber + filter: (file) => { + const wanted = file.name === "book.json" || (file.name.startsWith("files/") && !file.name.slice(6).includes("/")); + if (!wanted || file.originalSize > MAX_FILE_BYTES || ++count > MAX_ITEMS) return false; + total += file.originalSize; + return total <= MAX_IMPORT_BYTES; + }, + }); + } catch { + throw new ImportError("invalid", "zip"); + } + const book = entries["book.json"]; + if (!book) throw new ImportError("not-export"); + json = strFromU8(book); + for (const [name, data] of Object.entries(entries)) if (name.startsWith("files/")) files.set(name.slice(6), data); + } else { + json = new TextDecoder().decode(bytes); + } + + let raw: unknown; + try { + raw = JSON.parse(json.replace(/^/, "")); + } catch { + throw new ImportError("invalid", "json"); + } + return { data: parseExport(raw), files }; +} + +// ─── Import ───────────────────────────────────────────────────────────────── + +export type ImportResult = { + bookId: string; + entities: number; + relations: number; + maps: number; + markers: number; + shifts: number; + skippedImages: number; + skippedLinks: number; +}; + +type Coder = { key: string; parse: (code: string) => number | null; format: (n: number) => string }; + +const escape = (s: string) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); + +function simpleCoder(prefix: string): Coder { + const re = new RegExp(`^${escape(prefix)}-(\\d+)$`); + return { key: prefix, parse: (c) => (re.test(c) ? Number(c.match(re)![1]) : null), format: (n) => `${prefix}-${pad(n)}` }; +} + +function mapCoder(kind: "GEO" | "IMAGE"): Coder { + const key = `${MAP_PREFIX}-${mapLetter(kind)}`; + const re = new RegExp(`^${escape(key)}_(\\d+)$`); + return { key, parse: (c) => (re.test(c) ? Number(c.match(re)![1]) : null), format: (n) => `${key}_${pad(n)}` }; +} + +/** + * Behåller giltiga och unika ID från filen och ger nya löpande ID åt resten. + * Räknarna (counters) hamnar efter det högsta numret, så nya poster fortsätter därifrån. + */ +function assignCodes(items: T[], coderOf: (item: T) => Coder | null, counters: Map) { + const used = new Set(); + const missing: { item: T; coder: Coder }[] = []; + for (const item of items) { + const coder = coderOf(item); + if (!coder) { + item.code = null; + continue; + } + const n = item.code ? coder.parse(item.code) : null; + if (n !== null && item.code && !used.has(item.code)) { + used.add(item.code); + counters.set(coder.key, Math.max(counters.get(coder.key) ?? 0, n)); + } else { + missing.push({ item, coder }); + } + } + for (const { item, coder } of missing) { + const n = (counters.get(coder.key) ?? 0) + 1; + counters.set(coder.key, n); + item.code = coder.format(n); + } +} + +function remapIds(value: Json, ids: Map): Json { + if (typeof value === "string") return ids.get(value) ?? value; + if (Array.isArray(value)) return value.map((v) => remapIds(v as Json, ids)); + if (isObj(value)) return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, remapIds(v as Json, ids)])); + return value; +} + +export async function importBook(ownerId: string, data: ExportData, files: Map): Promise { + const bookId = randomUUID(); + const entityIds = new Map(data.entities.map((e) => [e.id, randomUUID()])); + let skippedImages = 0; + let skippedLinks = 0; + const savedImages: string[] = []; + + const saveImage = async (name: string | null): Promise => { + if (!name) return null; + const bytes = files.get(name); + const saved = bytes ? await saveImageBytes(bytes) : null; + if (!saved) { + skippedImages++; + return null; + } + savedImages.push(saved); + return saved; + }; + + try { + // Samma namn som en befintlig bok får ett tillägg så att de går att skilja åt + const existingTitles = new Set((await prisma.book.findMany({ where: { ownerId }, select: { title: true } })).map((b) => b.title)); + const title = existingTitles.has(data.book.title) ? `${data.book.title} (import)` : data.book.title; + + const counters = new Map(); + + const entities = data.entities.map((e) => ({ ...e })); + assignCodes(entities, (e) => (ENTITY_PREFIX[e.kind] ? simpleCoder(ENTITY_PREFIX[e.kind]!) : null), counters); + + const relationsIn = data.relations.filter((r) => { + const ok = entityIds.has(r.fromId) && entityIds.has(r.toId) && r.fromId !== r.toId; + if (!ok) skippedLinks++; + return ok; + }); + const relations = relationsIn.map((r) => ({ ...r })); + assignCodes(relations, () => simpleCoder(RELATION_PREFIX), counters); + + const maps = data.maps.map((m) => ({ ...m, markers: m.markers.map((k) => ({ ...k })) })); + assignCodes(maps, (m) => mapCoder(m.kind), counters); + const markers = maps.flatMap((m) => m.markers); + assignCodes(markers, () => simpleCoder(MARKER_PREFIX), counters); + + // Ett schema-ID per person och arbetsplats + const shiftsIn = data.shifts.filter((s) => { + const ok = entityIds.has(s.personId); + if (!ok) skippedLinks++; + return ok; + }); + const groups = new Map(); + for (const s of shiftsIn) { + const key = `${s.personId}|${s.workplaceId && entityIds.has(s.workplaceId) ? s.workplaceId : ""}`; + const group = groups.get(key) ?? { code: s.scheduleCode, shifts: [] }; + group.shifts.push(s); + groups.set(key, group); + } + const groupList = [...groups.values()]; + assignCodes(groupList, () => simpleCoder(SCHEDULE_PREFIX), counters); + + // Bilderna sparas före databasen, och tas bort igen om importen misslyckas + const entityImages = new Map(); + for (const e of entities) entityImages.set(e.id, await saveImage(e.image)); + const mapImages = new Map(); + for (const m of maps) mapImages.set(m.id, await saveImage(m.image)); + + const sid = (id: string) => entityIds.get(id)!; + + await prisma.$transaction( + async (tx) => { + await tx.book.create({ + data: { id: bookId, ownerId, title, subtitle: data.book.subtitle, genre: data.book.genre, description: data.book.description }, + }); + await tx.entity.createMany({ + data: entities.map((e) => ({ + id: sid(e.id), + bookId, + kind: e.kind, + name: e.name, + code: e.code, + summary: e.summary, + tags: e.tags, + data: remapIds(e.data, entityIds) as Prisma.InputJsonObject, + imageFile: entityImages.get(e.id) ?? null, + })), + }); + + const relationRows = relations.map((r) => ({ ...r, newId: randomUUID() })); + await tx.relation.createMany({ + data: relationRows.map((r) => ({ + id: r.newId, + bookId, + code: r.code, + fromId: sid(r.fromId), + toId: sid(r.toId), + type: r.type, + description: r.description, + data: remapIds(r.data, entityIds) as Prisma.InputJsonObject, + })), + }); + await tx.relationMember.createMany({ + data: relationRows.flatMap((r) => + [...new Set(r.members)] + .filter((m) => entityIds.has(m) && m !== r.fromId && m !== r.toId) + .map((m) => ({ relationId: r.newId, entityId: sid(m) })), + ), + }); + + await tx.shift.createMany({ + data: groupList.flatMap((g) => + g.shifts.map((s) => ({ + bookId, + personId: sid(s.personId), + workplaceId: s.workplaceId && entityIds.has(s.workplaceId) ? sid(s.workplaceId) : null, + weekday: s.weekday, + startTime: s.startTime, + endTime: s.endTime, + weeks: s.weeks, + label: s.label, + scheduleCode: g.code, + })), + ), + }); + + const mapRows = maps.map((m) => ({ ...m, newId: randomUUID() })); + await tx.bookMap.createMany({ + data: mapRows.map((m) => ({ + id: m.newId, + bookId, + code: m.code, + name: m.name, + kind: m.kind, + imageFile: mapImages.get(m.id) ?? null, + centerLat: m.centerLat, + centerLng: m.centerLng, + zoom: m.zoom, + })), + }); + await tx.marker.createMany({ + data: mapRows.flatMap((m) => + m.markers.map((k) => ({ + mapId: m.newId, + code: k.code, + entityId: k.entityId && entityIds.has(k.entityId) ? sid(k.entityId) : null, + label: k.label, + note: k.note, + color: k.color, + lat: k.lat, + lng: k.lng, + })), + ), + }); + + const seenTemplates = new Set(); + await tx.template.createMany({ + data: data.templates + .filter((t) => !seenTemplates.has(t.kind) && seenTemplates.add(t.kind)) + .map((t) => ({ bookId, kind: t.kind, fields: t.fields as unknown as Prisma.InputJsonArray })), + }); + + await tx.idCounter.createMany({ data: [...counters.entries()].map(([key, next]) => ({ bookId, key, next })) }); + }, + { timeout: 120_000, maxWait: 20_000 }, + ); + + return { + bookId, + entities: entities.length, + relations: relations.length, + maps: maps.length, + markers: markers.length, + shifts: shiftsIn.length, + skippedImages, + skippedLinks, + }; + } catch (error) { + await Promise.all(savedImages.map((name) => deleteImage(name))); + throw error; + } +} diff --git a/src/lib/files.ts b/src/lib/files.ts index 7dc9f38..b991a55 100644 --- a/src/lib/files.ts +++ b/src/lib/files.ts @@ -1,5 +1,5 @@ import { randomUUID } from "node:crypto"; -import { mkdir, unlink, writeFile } from "node:fs/promises"; +import { mkdir, readFile, unlink, writeFile } from "node:fs/promises"; import path from "node:path"; export const UPLOAD_DIR = path.resolve(process.env.UPLOAD_DIR ?? "./data/uploads"); @@ -48,3 +48,32 @@ export async function deleteImage(name: string | null | undefined) { export function fileUrl(name: string | null | undefined): string | null { return name ? `/api/files/${name}` : null; } + +/** Bildtyp utifrån filens första bytes (inte filnamnet), eller null om det inte är en tillåten bild. */ +export function detectImageExt(bytes: Uint8Array): "png" | "jpg" | "webp" | "gif" | null { + const at = (i: number) => bytes[i]; + if (bytes.length > 8 && at(0) === 0x89 && at(1) === 0x50 && at(2) === 0x4e && at(3) === 0x47) return "png"; + if (bytes.length > 3 && at(0) === 0xff && at(1) === 0xd8 && at(2) === 0xff) return "jpg"; + if (bytes.length > 6 && at(0) === 0x47 && at(1) === 0x49 && at(2) === 0x46 && at(3) === 0x38) return "gif"; + if (bytes.length > 12 && at(0) === 0x52 && at(1) === 0x49 && at(2) === 0x46 && at(3) === 0x46 && at(8) === 0x57 && at(9) === 0x45 && at(10) === 0x42 && at(11) === 0x50) return "webp"; + return null; +} + +/** Sparar en bild som redan ligger i minnet (t.ex. från en importfil). Returnerar filnamnet, eller null om bilden inte duger. */ +export async function saveImageBytes(bytes: Uint8Array): Promise { + const ext = detectImageExt(bytes); + if (!ext || bytes.length > MAX_UPLOAD_BYTES) return null; + await mkdir(UPLOAD_DIR, { recursive: true }); + const name = `${randomUUID()}.${ext}`; + await writeFile(path.join(UPLOAD_DIR, name), bytes); + return name; +} + +export async function readImageBytes(name: string | null | undefined): Promise { + if (!name || !FILE_NAME_PATTERN.test(name)) return null; + try { + return await readFile(path.join(UPLOAD_DIR, name)); + } catch { + return null; + } +} diff --git a/src/lib/ids.ts b/src/lib/ids.ts index 383166a..b248178 100644 --- a/src/lib/ids.ts +++ b/src/lib/ids.ts @@ -30,7 +30,7 @@ export function mapLetter(kind: "GEO" | "IMAGE"): "R" | "E" { return kind === "GEO" ? "R" : "E"; } -const pad = (n: number) => String(n).padStart(4, "0"); +export const pad = (n: number) => String(n).padStart(4, "0"); /** Nästa löpande nummer för en räknare (atomärt). */ async function nextNumber(db: Db, bookId: string, key: string): Promise {