zips are getting extracted

This commit is contained in:
2026-06-12 11:58:22 +02:00
parent 2f1b9f8f79
commit 187f7b3a6a
5 changed files with 167 additions and 13 deletions
+99
View File
@@ -0,0 +1,99 @@
import { unzip } from "fflate";
export type ZipAttachmentKind = "pdf" | "image";
export type ZipAttachment = {
name: string;
path: string;
kind: ZipAttachmentKind;
data: Uint8Array;
};
export type ProcessedZipArchive = {
name: string;
attachments: ZipAttachment[];
xjustizNachrichtXml: Uint8Array | null;
};
const IMAGE_EXTENSIONS = new Set([
".avif",
".bmp",
".gif",
".heic",
".jpeg",
".jpg",
".jp2",
".png",
".tif",
".tiff",
".webp",
]);
const ZIP_META_FILE_NAME = "xjustiz_nachricht.xml";
const unzipArchive = (data: Uint8Array) =>
new Promise<Record<string, Uint8Array>>((resolve, reject) => {
unzip(data, (error, files) => {
if (error) {
reject(error);
return;
}
resolve(files);
});
});
const getBaseName = (path: string) => path.split("/").pop() ?? path;
const getExtension = (path: string) => {
const baseName = getBaseName(path).toLowerCase();
const lastDotIndex = baseName.lastIndexOf(".");
return lastDotIndex === -1 ? "" : baseName.slice(lastDotIndex);
};
const isPdf = (path: string) => getExtension(path) === ".pdf";
const isImage = (path: string) => IMAGE_EXTENSIONS.has(getExtension(path));
export const extractZipArchive = async (file: File): Promise<ProcessedZipArchive> => {
const archiveBytes = new Uint8Array(await file.arrayBuffer());
const files = await unzipArchive(archiveBytes);
const attachments: ZipAttachment[] = [];
let xjustizNachrichtXml: Uint8Array | null = null;
for (const [path, data] of Object.entries(files)) {
const baseName = getBaseName(path);
if (baseName.toLowerCase() === ZIP_META_FILE_NAME) {
xjustizNachrichtXml = data;
continue;
}
if (isPdf(path)) {
attachments.push({
name: baseName,
path,
kind: "pdf",
data,
});
continue;
}
if (isImage(path)) {
attachments.push({
name: baseName,
path,
kind: "image",
data,
});
}
}
return {
name: file.name,
attachments,
xjustizNachrichtXml,
};
};