Skip to content

Commit

Permalink
fix: handle sub pages regardless of location
Browse files Browse the repository at this point in the history
  • Loading branch information
aalemayhu committed Nov 12, 2022
1 parent 60b2103 commit 96ac326
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/lib/anki/zip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions src/lib/misc/ErrorHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
29 changes: 29 additions & 0 deletions src/routes/upload/helpers/getPackagesFromZip.ts
Original file line number Diff line number Diff line change
@@ -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];
};
24 changes: 11 additions & 13 deletions src/routes/upload/helpers/handleUpload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand All @@ -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;
}
}
}
Expand Down

0 comments on commit 96ac326

Please sign in to comment.