handles zips in zip correctly
This commit is contained in:
@@ -56,6 +56,8 @@ const isPdf = (path: string) => getExtension(path) === ".pdf";
|
||||
|
||||
const isImage = (path: string) => IMAGE_EXTENSIONS.has(getExtension(path));
|
||||
|
||||
const isZipArchive = (path: string) => getExtension(path) === ".zip";
|
||||
|
||||
const normalizeKey = (value: string) => value.replaceAll("\\", "/").toLowerCase().trim();
|
||||
|
||||
const isMacMetadataEntry = (path: string) => {
|
||||
@@ -188,11 +190,10 @@ const orderAttachmentsByDocumentNames = (attachments: ZipAttachment[], documentN
|
||||
return orderedAttachments;
|
||||
};
|
||||
|
||||
export const extractZipArchive = async (file: File): Promise<ProcessedZipArchive> => {
|
||||
const archiveBytes = new Uint8Array(await file.arrayBuffer());
|
||||
const extractZipEntries = async (archiveBytes: Uint8Array, fallbackName: string): Promise<ProcessedZipArchive[]> => {
|
||||
const files = await unzipArchive(archiveBytes);
|
||||
|
||||
const attachments: ZipAttachment[] = [];
|
||||
const nestedArchives: ProcessedZipArchive[] = [];
|
||||
let xjustizNachrichtXml: Uint8Array | null = null;
|
||||
|
||||
for (const [path, data] of Object.entries(files)) {
|
||||
@@ -207,6 +208,12 @@ export const extractZipArchive = async (file: File): Promise<ProcessedZipArchive
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isZipArchive(path)) {
|
||||
const childArchives = await extractZipEntries(data, baseName);
|
||||
nestedArchives.push(...childArchives);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isPdf(path)) {
|
||||
attachments.push({
|
||||
name: baseName,
|
||||
@@ -229,10 +236,21 @@ export const extractZipArchive = async (file: File): Promise<ProcessedZipArchive
|
||||
|
||||
const documentNames = xjustizNachrichtXml ? getXJustizDocumentNames(xjustizNachrichtXml) : [];
|
||||
const orderedAttachments = orderAttachmentsByDocumentNames(attachments, documentNames);
|
||||
const archiveName = documentNames[0] ? getBaseName(documentNames[0]) : file.name;
|
||||
const archiveName = documentNames[0] ? getBaseName(documentNames[0]) : fallbackName;
|
||||
const archives: ProcessedZipArchive[] = [];
|
||||
|
||||
return {
|
||||
name: archiveName,
|
||||
attachments: orderedAttachments
|
||||
};
|
||||
if (orderedAttachments.length > 0) {
|
||||
archives.push({
|
||||
name: archiveName,
|
||||
attachments: orderedAttachments,
|
||||
});
|
||||
}
|
||||
|
||||
return [...archives, ...nestedArchives];
|
||||
};
|
||||
|
||||
export const extractZipArchives = async (file: File): Promise<ProcessedZipArchive[]> => {
|
||||
const archiveBytes = new Uint8Array(await file.arrayBuffer());
|
||||
|
||||
return extractZipEntries(archiveBytes, file.name);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user