Skip to content

Commit

Permalink
feat: allow nested zip files to be processed
Browse files Browse the repository at this point in the history
This fixes a bug where some Zip files can contain nested data.
  • Loading branch information
aalemayhu committed Oct 26, 2024
1 parent 2d4a781 commit 1a8c8bf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
30 changes: 21 additions & 9 deletions src/lib/anki/zip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ interface File {

class ZipHandler {
fileNames: string[];

files: File[];
zipFileCount: number;
maxZipFiles: number;

constructor() {
constructor(maxNestedZipFiles: number) {
this.fileNames = [];
this.files = [];
this.zipFileCount = 0;
this.maxZipFiles = maxNestedZipFiles;
}

build(zipData: Uint8Array, paying: boolean) {
Expand All @@ -35,31 +38,40 @@ class ZipHandler {
)
);
}

this.processZip(zipData);
}

private processZip(zipData: Uint8Array) {
if (this.zipFileCount >= this.maxZipFiles) {
throw new Error('Too many zip files in the upload.');
}

const loadedZip = unzipSync(zipData, {
filter(file) {
return !file.name.endsWith('/');
},
});
this.fileNames = Object.keys(loadedZip);
this.files = [];

for (const name of this.fileNames) {
for (const name in loadedZip) {
const file = loadedZip[name];
let contents = file;

/**
* For now disable batch processing of PDF files. We only want single uploads to avoid creating too many requests.
*/
if (name.includes('__MACOSX/') || isPDFFile(name)) {
continue;
}

if ((isHTMLFile(name) || isMarkdownFile(name)) && contents) {
if (name.endsWith('.zip')) {
this.zipFileCount++;
this.processZip(file);
} else if ((isHTMLFile(name) || isMarkdownFile(name)) && contents) {
this.files.push({ name, contents: strFromU8(file) });
} else if (contents) {
this.files.push({ name, contents });
}
}

this.fileNames = this.files.map((file) => file.name);
}

getFileNames() {
Expand Down
3 changes: 2 additions & 1 deletion src/usecases/uploads/getPackagesFromZip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from '../../lib/storage/checks';
import Workspace from '../../lib/parser/WorkSpace';
import { allowPDFUpload } from './allowPDFUpload';
import { getMaxUploadCount } from '../../lib/misc/getMaxUploadCount';

export const isFileSupported = (filename: string) =>
isHTMLFile(filename) ??
Expand All @@ -26,7 +27,7 @@ export const getPackagesFromZip = async (
settings: Settings,
workspace: Workspace
): Promise<PackageResult> => {
const zipHandler = new ZipHandler();
const zipHandler = new ZipHandler(getMaxUploadCount(paying));
const packages = [];

if (!fileContents) {
Expand Down

0 comments on commit 1a8c8bf

Please sign in to comment.