From 96ac326dbe73a76e4c88fcecdf6f2587484113fb Mon Sep 17 00:00:00 2001 From: Alexander Alemayhu Date: Sat, 12 Nov 2022 11:31:36 +0100 Subject: [PATCH] fix: handle sub pages regardless of location --- src/lib/anki/zip.tsx | 2 +- src/lib/misc/ErrorHandler.tsx | 1 + .../upload/helpers/getPackagesFromZip.ts | 29 +++++++++++++++++++ src/routes/upload/helpers/handleUpload.ts | 24 +++++++-------- 4 files changed, 42 insertions(+), 14 deletions(-) create mode 100644 src/routes/upload/helpers/getPackagesFromZip.ts diff --git a/src/lib/anki/zip.tsx b/src/lib/anki/zip.tsx index 5fb9d8be9..411052518 100644 --- a/src/lib/anki/zip.tsx +++ b/src/lib/anki/zip.tsx @@ -19,7 +19,7 @@ class ZipHandler { this.files = []; } - async build(zipData: Buffer, isPatron: boolean) { + async build(zipData: string, isPatron: boolean) { const size = Buffer.byteLength(zipData); const limits = getUploadLimits(isPatron); diff --git a/src/lib/misc/ErrorHandler.tsx b/src/lib/misc/ErrorHandler.tsx index 3a854fb62..70f43c9e8 100644 --- a/src/lib/misc/ErrorHandler.tsx +++ b/src/lib/misc/ErrorHandler.tsx @@ -28,6 +28,7 @@ export const UNSUPPORTED_FORMAT_MD = new Error( ); export default function ErrorHandler(res: express.Response, err: Error) { + console.error(err); if (process.env.NODE_ENV === 'production') { Sentry.captureException(err); } diff --git a/src/routes/upload/helpers/getPackagesFromZip.ts b/src/routes/upload/helpers/getPackagesFromZip.ts new file mode 100644 index 000000000..61d87c9b5 --- /dev/null +++ b/src/routes/upload/helpers/getPackagesFromZip.ts @@ -0,0 +1,29 @@ +import { ZipHandler } from '../../../lib/anki/zip'; +import { PrepareDeck } from '../../../lib/parser/DeckParser'; +import Package from '../../../lib/parser/Package'; +import Settings from '../../../lib/parser/Settings'; + +export const getPackagesFromZip = async ( + fileContents: string, + isPatreon: boolean, + settings: Settings +) => { + const zipHandler = new ZipHandler(); + let hasMarkdown = false; + const packages = []; + + await zipHandler.build(fileContents, isPatreon); + + for (const fileName of zipHandler.getFileNames()) { + if (fileName.match(/.html$/)) { + const d = await PrepareDeck(fileName, zipHandler.files, settings); + if (d) { + packages.push(new Package(d.name, d.apkg)); + } + } else if (fileName.match(/.md$/)) { + hasMarkdown = true; + } + } + + return [packages, hasMarkdown]; +}; diff --git a/src/routes/upload/helpers/handleUpload.ts b/src/routes/upload/helpers/handleUpload.ts index d346b75df..4b5d6c1f3 100644 --- a/src/routes/upload/helpers/handleUpload.ts +++ b/src/routes/upload/helpers/handleUpload.ts @@ -13,6 +13,7 @@ import cleanDeckName from './cleanDeckname'; import { registerUploadSize } from './registerUploadSize'; import { sendBundle } from './sendBundle'; import { captureException } from '@sentry/node'; +import { getPackagesFromZip } from './getPackagesFromZip'; export default async function handleUpload( storage: StorageHandler, @@ -29,6 +30,7 @@ export default async function handleUpload( registerUploadSize(file, res); /* @ts-ignore */ const fileContents = await storage.getFileContents(file.key); + console.log('reading', filename); if (filename.match(/.html$/)) { const d = await PrepareDeck( @@ -42,19 +44,15 @@ export default async function handleUpload( } } else if (filename.match(/.md$/)) { hasMarkdown = true; - } else { - const zipHandler = new ZipHandler(); - /* @ts-ignore */ - await zipHandler.build(fileContents, res.locals.patreon); - for (const fileName of zipHandler.getFileNames()) { - if (fileName.match(/.html$/) && !fileName.includes('/')) { - const d = await PrepareDeck(fileName, zipHandler.files, settings); - if (d) { - packages.push(new Package(d.name, d.apkg)); - } - } else if (fileName.match(/.md$/)) { - hasMarkdown = true; - } + } else if (filename.match(/.zip$/)) { + const [extraPackages, md] = await getPackagesFromZip( + fileContents, + res.locals.patreon, + settings + ); + packages = packages.concat(extraPackages as Package[]); + if (md) { + hasMarkdown = true; } } }