Skip to content

Commit 859dff0

Browse files
committed
fix: prevent crashes in nested uploads
1 parent 48709f9 commit 859dff0

File tree

4 files changed

+21
-11
lines changed

4 files changed

+21
-11
lines changed

src/lib/getSafeFilename.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { getSafeFilename } from './getSafeFilename';
2+
3+
test("returns filename without slashes", () => {
4+
expect(getSafeFilename("x/y/z")).toBe('x-y-z')
5+
})

src/lib/getSafeFilename.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function getSafeFilename(name: string) {
2+
return name.replace(/\//g, '-');
3+
}

src/lib/parser/DeckParser.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,14 @@ export class DeckParser {
4747
const firstFile = this.files.find((file) => isFileNameEqual(file, name));
4848
const contents = getHTMLContents(firstFile);
4949

50-
if (contents) {
51-
this.payload = this.handleHTML(
52-
name,
53-
contents.toString(),
54-
this.settings.deckName || '',
55-
[]
56-
);
57-
} else {
58-
throw new Error(`Error Unknown file ${name}`);
59-
}
50+
this.payload = contents
51+
? this.handleHTML(
52+
name,
53+
contents.toString(),
54+
this.settings.deckName || '',
55+
[]
56+
)
57+
: [];
6058
}
6159

6260
findNextPage(href: string | undefined): string | Uint8Array | undefined {
@@ -473,6 +471,9 @@ export class DeckParser {
473471
}
474472

475473
totalCardCount() {
474+
if (this.payload.length === 0) {
475+
return 0;
476+
}
476477
return this.payload.map((p) => p.cardCount).reduce((a, b) => a + b);
477478
}
478479

src/services/UploadService.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import StorageHandler from '../lib/storage/StorageHandler';
1212
import { UploadedFile } from '../lib/storage/types';
1313
import GeneratePackagesUseCase from '../usecases/uploads/GeneratePackagesUseCase';
1414
import { toText } from './NotionService/BlockHandler/helpers/deckNameToText';
15+
import { getSafeFilename } from '../lib/getSafeFilename';
1516

1617
class UploadService {
1718
getUploadsByOwner(owner: number) {
@@ -63,7 +64,7 @@ class UploadService {
6364
const workspace = new Workspace(true, 'fs');
6465

6566
for (const pkg of packages) {
66-
const p = path.join(workspace.location, pkg.name);
67+
const p = path.join(workspace.location, getSafeFilename(pkg.name));
6768
fs.writeFileSync(p, pkg.apkg);
6869
}
6970

0 commit comments

Comments
 (0)