-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix embedding of file in exporter
- Loading branch information
Showing
1 changed file
with
24 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |