Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "chore: remove debug option" #1590

Merged
merged 5 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 8 additions & 32 deletions src/lib/parser/DeckParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ import { isFileNameEqual } from '../storage/types';
import { isImageFileEmbedable, isMarkdownFile } from '../storage/checks';
import { getFileContents } from './getFileContents';
import { handleNestedBulletPointsInMarkdown } from './handleNestedBulletPointsInMarkdown';
import { checkFlashcardsLimits } from '../User/checkFlashcardsLimits';

export interface DeckParserInput {
name: string;
settings: Settings;
files: File[];
noLimits: boolean;
}

export class DeckParser {
globalTags: cheerio.Cheerio | null;
Expand All @@ -43,27 +35,22 @@ export class DeckParser {

files: File[];

noLimits: boolean;

public get name() {
return this.payload[0].name;
}

constructor(input: DeckParserInput) {
this.settings = input.settings;
this.files = input.files || [];
this.firstDeckName = input.name;
this.noLimits = input.noLimits;
constructor(name: string, settings: Settings, files: File[]) {
this.settings = settings;
this.files = files || [];
this.firstDeckName = name;
this.globalTags = null;

const firstFile = this.files.find((file) =>
isFileNameEqual(file, input.name)
);
const firstFile = this.files.find((file) => isFileNameEqual(file, name));

if (this.settings.nestedBulletPoints && isMarkdownFile(input.name)) {
if (this.settings.nestedBulletPoints && isMarkdownFile(name)) {
const contents = getFileContents(firstFile, false);
this.payload = handleNestedBulletPointsInMarkdown(
input.name,
name,
contents?.toString(),
this.settings.deckName,
[],
Expand All @@ -73,7 +60,7 @@ export class DeckParser {
const contents = getFileContents(firstFile, true);
this.payload = contents
? this.handleHTML(
input.name,
name,
contents.toString(),
this.settings.deckName || '',
[]
Expand Down Expand Up @@ -584,8 +571,6 @@ export class DeckParser {
const parentUL = p;
const parentClass = p.attr('class') || '';

this.checkLimits(cards.length, []);

if (this.settings.toggleMode === 'open_toggle') {
dom('details').attr('open', '');
} else if (this.settings.toggleMode === 'close_toggle') {
Expand Down Expand Up @@ -666,19 +651,10 @@ export class DeckParser {

lists.forEach((list) => {
for (const child of dom(list).find('li')) {
this.checkLimits(cards.length, []);
cards.push(new Note(dom(child).html() ?? '', ''));
}
});

return cards;
}

private checkLimits(cards: number, decks: Deck[]) {
checkFlashcardsLimits({
cards: cards,
decks: decks,
paying: this.noLimits,
});
}
}
12 changes: 8 additions & 4 deletions src/lib/parser/PrepareDeck.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { File } from '../anki/zip';
import Settings from './Settings';
import getDeckFilename from '../anki/getDeckFilename';
import { DeckParser, DeckParserInput } from './DeckParser';
import { DeckParser } from './DeckParser';
import Deck from './Deck';

interface PrepareDeckResult {
Expand All @@ -9,14 +11,16 @@ interface PrepareDeckResult {
}

export async function PrepareDeck(
input: DeckParserInput
fileName: string,
files: File[],
settings: Settings
): Promise<PrepareDeckResult> {
const parser = new DeckParser(input);
const parser = new DeckParser(fileName, settings, files);

if (parser.totalCardCount() === 0) {
const apkg = await parser.tryExperimental();
return {
name: getDeckFilename(parser.name ?? input.name),
name: getDeckFilename(parser.name ?? fileName),
apkg,
deck: parser.payload,
};
Expand Down
7 changes: 1 addition & 6 deletions src/test/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@ function loadFixture(fileName: string) {

function configureParser(fileName: string, opts: Settings) {
const info = loadFixture(fileName);
return new DeckParser({
name: fileName,
settings: opts,
files: info,
noLimits: true,
});
return new DeckParser(fileName, opts, info);
}

export async function getDeck(fileName: string, opts: Settings) {
Expand Down
101 changes: 91 additions & 10 deletions src/usecases/uploads/GeneratePackagesUseCase.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,108 @@
import fs from 'fs';

import { ZipHandler } from '../../lib/anki/zip';
import Package from '../../lib/parser/Package';
import Settings from '../../lib/parser/Settings';
import {
isCSVFile,
isHTMLFile,
isMarkdownFile,
isPlainText,
isZIPFile,
} from '../../lib/storage/checks';
import { UploadedFile } from '../../lib/storage/types';
import { Worker } from 'worker_threads';
import path from 'path';

import { Body } from 'aws-sdk/clients/s3';
import { PrepareDeck } from '../../lib/parser/PrepareDeck';
import { checkFlashcardsLimits } from '../../lib/User/checkFlashcardsLimits';

export interface PackageResult {
packages: Package[];
}

export const isFileSupported = (filename: string) =>
isHTMLFile(filename) ??
isMarkdownFile(filename) ??
isPlainText(filename) ??
isCSVFile(filename);

const getPackagesFromZip = async (
fileContents: Body | undefined,
paying: boolean,
settings: Settings
): Promise<PackageResult> => {
const zipHandler = new ZipHandler();
const packages = [];

if (!fileContents) {
return { packages: [] };
}

zipHandler.build(fileContents as Uint8Array, paying);

const fileNames = zipHandler.getFileNames();

let cardCount = 0;
for (const fileName of fileNames) {
if (isFileSupported(fileName)) {
const deck = await PrepareDeck(fileName, zipHandler.files, settings);

if (deck) {
packages.push(new Package(deck.name, deck.apkg));
cardCount += deck.deck.reduce((acc, d) => acc + d.cards.length, 0);

// Checking the limit in place while iterating through the decks
checkFlashcardsLimits({
cards: 0,
decks: deck.deck,
paying,
});
}
}

// Checking the limit in place while iterating through the files
checkFlashcardsLimits({
cards: cardCount,
paying: paying,
});
}

return { packages };
};

class GeneratePackagesUseCase {
execute(
async execute(
paying: boolean,
files: UploadedFile[],
settings: Settings
): Promise<PackageResult> {
return new Promise((resolve, reject) => {
const data = { paying, files, settings };
const workerPath = path.resolve(__dirname, './worker.js');
const worker = new Worker(workerPath, { workerData: { data } });
let packages: Package[] = [];

worker.on('message', (result: PackageResult) => resolve(result));
worker.on('error', (error) => reject(error));
});
for (const file of files) {
const fileContents = file.path ? fs.readFileSync(file.path) : file.buffer;
const filename = file.originalname;
const key = file.key;

if (isFileSupported(filename)) {
const d = await PrepareDeck(
filename,
[{ name: filename, contents: fileContents }],
settings
);
if (d) {
const pkg = new Package(d.name, d.apkg);
packages = packages.concat(pkg);
}
} else if (isZIPFile(filename) || isZIPFile(key)) {
const { packages: extraPackages } = await getPackagesFromZip(
fileContents,
paying,
settings
);
packages = packages.concat(extraPackages);
}
}
return { packages };
}
}

Expand Down
68 changes: 0 additions & 68 deletions src/usecases/uploads/getPackagesFromZip.ts

This file was deleted.

56 changes: 0 additions & 56 deletions src/usecases/uploads/worker.ts

This file was deleted.

Loading