Skip to content

Commit

Permalink
fix: prevent crashes in nested uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
aalemayhu committed Feb 19, 2024
1 parent 48709f9 commit 859dff0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
5 changes: 5 additions & 0 deletions src/lib/getSafeFilename.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { getSafeFilename } from './getSafeFilename';

test("returns filename without slashes", () => {
expect(getSafeFilename("x/y/z")).toBe('x-y-z')
})
3 changes: 3 additions & 0 deletions src/lib/getSafeFilename.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function getSafeFilename(name: string) {
return name.replace(/\//g, '-');
}
21 changes: 11 additions & 10 deletions src/lib/parser/DeckParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,14 @@ export class DeckParser {
const firstFile = this.files.find((file) => isFileNameEqual(file, name));
const contents = getHTMLContents(firstFile);

if (contents) {
this.payload = this.handleHTML(
name,
contents.toString(),
this.settings.deckName || '',
[]
);
} else {
throw new Error(`Error Unknown file ${name}`);
}
this.payload = contents
? this.handleHTML(
name,
contents.toString(),
this.settings.deckName || '',
[]
)
: [];
}

findNextPage(href: string | undefined): string | Uint8Array | undefined {
Expand Down Expand Up @@ -473,6 +471,9 @@ export class DeckParser {
}

totalCardCount() {
if (this.payload.length === 0) {
return 0;
}
return this.payload.map((p) => p.cardCount).reduce((a, b) => a + b);
}

Expand Down
3 changes: 2 additions & 1 deletion src/services/UploadService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import StorageHandler from '../lib/storage/StorageHandler';
import { UploadedFile } from '../lib/storage/types';
import GeneratePackagesUseCase from '../usecases/uploads/GeneratePackagesUseCase';
import { toText } from './NotionService/BlockHandler/helpers/deckNameToText';
import { getSafeFilename } from '../lib/getSafeFilename';

class UploadService {
getUploadsByOwner(owner: number) {
Expand Down Expand Up @@ -63,7 +64,7 @@ class UploadService {
const workspace = new Workspace(true, 'fs');

for (const pkg of packages) {
const p = path.join(workspace.location, pkg.name);
const p = path.join(workspace.location, getSafeFilename(pkg.name));
fs.writeFileSync(p, pkg.apkg);
}

Expand Down

0 comments on commit 859dff0

Please sign in to comment.