Delning av böcker, skydd mot kolliderande ändringar och import från Word
Böcker kan delas med roller (läsare, redaktör, administratör), och behörigheten kontrolleras på servern. Formulär för poster, relationer och händelser skickar med version, och en krock visar vad som skiljer och låter användaren spara ändå eller ladda om. Ifyllda Word-mallar kan importeras med förhandsgranskning. Importerade ID flyttar nu räknaren. Migrering 0007. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
bd269d70a5
commit
86fd435438
64 files changed
+4796
-235
No files matched your search
@@ -0,0 +1,38 @@
|
||||
"use server";
|
||||
|
||||
import { revalidateBook, str } from "@/lib/form";
|
||||
import { requireBook } from "@/lib/session";
|
||||
import { MAX_DOCX_BYTES } from "@/lib/word-docx";
|
||||
import { IMPORT_KINDS, type ImportKind } from "@/lib/word-import";
|
||||
import { MAX_WORD_FILES, MAX_WORD_ITEMS, commitWordImport, previewWordFiles, type CommitItem, type CommitResult, type PreviewDocument } from "@/lib/word-import-db";
|
||||
|
||||
export type PreviewResponse = { docs: PreviewDocument[] } | { error: "no-files" | "limit" };
|
||||
|
||||
/** Läser uppladdade Word-filer och visar vad som hittades. Ingenting sparas. */
|
||||
export async function previewWordImport(formData: FormData): Promise<PreviewResponse> {
|
||||
const book = await requireBook(str(formData, "bookId"), "EDITOR");
|
||||
const uploads = formData.getAll("files").filter((f): f is File => typeof f !== "string" && f.size > 0);
|
||||
if (uploads.length === 0) return { error: "no-files" };
|
||||
if (uploads.length > MAX_WORD_FILES) return { error: "limit" };
|
||||
|
||||
const kinds = formData.getAll("kinds").map((k) => (IMPORT_KINDS as string[]).includes(String(k)) ? (String(k) as ImportKind) : null);
|
||||
const files = await Promise.all(
|
||||
uploads.map(async (file) => ({
|
||||
name: file.name.slice(0, 200),
|
||||
// Stora filer läses inte alls, de rapporteras som för stora
|
||||
bytes: file.size > MAX_DOCX_BYTES ? new Uint8Array(4) : new Uint8Array(await file.arrayBuffer()),
|
||||
tooLarge: file.size > MAX_DOCX_BYTES,
|
||||
})),
|
||||
);
|
||||
const docs = await previewWordFiles(book.id, files.map(({ name, bytes }) => ({ name, bytes })), kinds);
|
||||
return { docs: docs.map((doc, i) => (files[i].tooLarge ? { ...doc, error: "too-large" as const } : doc)) };
|
||||
}
|
||||
|
||||
/** Sparar det användaren valt i förhandsgranskningen. Allt kontrolleras igen mot boken innan det sparas. */
|
||||
export async function saveWordImport(input: { bookId: string; items: CommitItem[]; appendUnmatched: boolean }): Promise<CommitResult> {
|
||||
const book = await requireBook(String(input.bookId), "EDITOR");
|
||||
const items = Array.isArray(input.items) ? input.items.slice(0, MAX_WORD_ITEMS) : [];
|
||||
const result = await commitWordImport(book.id, book.userId, items, { appendUnmatched: input.appendUnmatched === true });
|
||||
revalidateBook(book.id);
|
||||
return result;
|
||||
}
|
||||
Reference in new issue
Block a user