fix: stream large project texts and scope API capabilities
Analyze UTF-8 text files as bounded batches of complete lines and reassemble only approved spans without loading entire files. Preserve line endings, BOM, Unicode offsets and source-change checks. Scope capability checks to selected file types, report missing features and HTTP failures, and retain the required Office image-policy handling. Cover large files, chunk boundaries, mappings and failure cleanup with regression tests.
This commit is contained in:
57
src/lib/image-policy.ts
Normal file
57
src/lib/image-policy.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
export type ImagePolicy = "placeholder" | "remove" | "keep";
|
||||
export type DocumentImageReport = {
|
||||
image_policy: ImagePolicy;
|
||||
images_replaced: number;
|
||||
images_removed: number;
|
||||
media_parts_removed: number;
|
||||
warnings: string[];
|
||||
};
|
||||
export const DEFAULT_IMAGE_POLICY: ImagePolicy = "placeholder";
|
||||
export const IMAGE_POLICY_LABELS: Record<ImagePolicy, string> = {
|
||||
placeholder: "Datenschutz-Platzhalter (Standard)",
|
||||
remove: "Bilder vollständig entfernen",
|
||||
keep: "Bilder behalten — Datenschutzrisiko",
|
||||
};
|
||||
export const KEEP_IMAGE_WARNING = "Achtung: Bilder können sensible Informationen enthalten. Sie werden unverändert übernommen und nicht auf personenbezogene Daten geprüft.";
|
||||
|
||||
export function checkedImagePolicy(policy: string): ImagePolicy {
|
||||
if (policy !== "placeholder" && policy !== "remove" && policy !== "keep") {
|
||||
throw new Error("Ungültige Bildbehandlung.");
|
||||
}
|
||||
return policy;
|
||||
}
|
||||
|
||||
export function sameImagePolicyCoverage(previous: { imagePolicy?: ImagePolicy } | undefined, policy: ImagePolicy): boolean {
|
||||
return previous?.imagePolicy === policy;
|
||||
}
|
||||
|
||||
export function checkedImageReport(value: unknown, policy: ImagePolicy): DocumentImageReport {
|
||||
const report = value as Partial<DocumentImageReport> | null;
|
||||
if (!report || report.image_policy !== policy) {
|
||||
throw new Error("Die Velum-API bestätigt die gewählte Bildbehandlung nicht. Bitte das Backend aktualisieren.");
|
||||
}
|
||||
for (const count of [report.images_replaced, report.images_removed, report.media_parts_removed]) {
|
||||
if (typeof count !== "number" || !Number.isSafeInteger(count) || count < 0) {
|
||||
throw new Error("Die Velum-API liefert keine gültigen Bildzähler.");
|
||||
}
|
||||
}
|
||||
if (!Array.isArray(report.warnings) || report.warnings.some((warning) => typeof warning !== "string")) {
|
||||
throw new Error("Die Velum-API liefert keine gültigen Bildwarnungen.");
|
||||
}
|
||||
return {
|
||||
image_policy: policy,
|
||||
images_replaced: report.images_replaced!, images_removed: report.images_removed!,
|
||||
media_parts_removed: report.media_parts_removed!,
|
||||
warnings: policy === "keep" && !report.warnings.includes(KEEP_IMAGE_WARNING) ? [KEEP_IMAGE_WARNING, ...report.warnings] : [...report.warnings],
|
||||
};
|
||||
}
|
||||
|
||||
export function summarizeImages(files: readonly { imageReport?: DocumentImageReport }[]): {
|
||||
replaced: number; removed: number; warnings: string[];
|
||||
} {
|
||||
return {
|
||||
replaced: files.reduce((total, file) => total + (file.imageReport?.images_replaced ?? 0), 0),
|
||||
removed: files.reduce((total, file) => total + (file.imageReport?.images_removed ?? 0), 0),
|
||||
warnings: [...new Set(files.flatMap((file) => file.imageReport?.warnings ?? []))],
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user