Skip to content

Commit

Permalink
fix: fix embedding of file in exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
aalemayhu committed Jul 21, 2023
1 parent 46ae39e commit c3bf3d9
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions src/lib/parser/exporters/embedFile.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
import Bugsnag from '@bugsnag/js';
import { File } from '../../anki/zip';
import { SuffixFrom } from '../../misc/file';
import getUniqueFileName from '../../misc/getUniqueFileName';
import CustomExporter from './CustomExporter';
import { File } from '../../anki/zip';
import { isFileNameEqual } from '../../storage/types';
import Bugsnag from '@bugsnag/js';

export const embedFile = (
exporter: CustomExporter,
files: File[],
filePath: string
): string | null => {
const newName = getUniqueFileName(filePath) + SuffixFrom(filePath);
const file = files.find((f) => isFileNameEqual(f, filePath));
const contents = file?.contents;

if (typeof contents === 'string') {
const suffix = SuffixFrom(filePath);
let file = files.find((f) => f.name === filePath);
if (!file) {
const lookup = `${exporter.firstDeckName}/${filePath}`.replace(
/\.\.\//g,
''
);
file = files.find((f) => {
if (f.name === lookup || f.name.endsWith(filePath)) {
return f;
}
});
if (!file) {
Bugsnag.notify(
`Missing relative path to ${filePath} used ${exporter.firstDeckName}`
);
return null;
}
}
const newName = getUniqueFileName(filePath) + suffix;
const contents = file.contents as string;
if (contents) {
exporter.addMedia(newName, contents);
} else {
Bugsnag.notify(new Error(`Failed to embed file ${filePath}`));
return null;
}

return newName;
};

0 comments on commit c3bf3d9

Please sign in to comment.