-
-
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(embedFile): improve the file retrieval from the payload
- Loading branch information
Showing
1 changed file
with
47 additions
and
22 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,37 +1,62 @@ | ||
import fs from 'fs'; | ||
|
||
import Bugsnag from '@bugsnag/js'; | ||
import { File } from '../../anki/zip'; | ||
import { SuffixFrom } from '../../misc/file'; | ||
import getUniqueFileName from '../../misc/getUniqueFileName'; | ||
import CustomExporter from './CustomExporter'; | ||
|
||
const getFile = ( | ||
exporter: CustomExporter, | ||
files: File[], | ||
filePath: string | ||
): File | undefined => { | ||
const asRootFile = files.find((f) => f.name === filePath); | ||
if (asRootFile) { | ||
return asRootFile; | ||
} | ||
const parent = exporter.firstDeckName.replace(/.html /, '/'); | ||
const asChildFile = files.find((f) => f.name === `${parent}/${filePath}`); | ||
if (asChildFile) { | ||
return asChildFile; | ||
} | ||
|
||
/* | ||
* Could not find file, try to find it by ending. | ||
* This happens in deeply nested directories. | ||
* Example: using a huge database | ||
*/ | ||
const normalized = filePath.replace(/\.\.\//g, ''); | ||
const usingSuffix = files.find( | ||
(f) => f.name.endsWith(filePath) || f.name.endsWith(normalized) | ||
); | ||
if (usingSuffix) { | ||
return usingSuffix; | ||
} | ||
|
||
return undefined; | ||
}; | ||
|
||
export const embedFile = ( | ||
exporter: CustomExporter, | ||
files: File[], | ||
filePath: string | ||
): string | null => { | ||
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 file = getFile(exporter, files, filePath); | ||
|
||
if (file) { | ||
const newName = getUniqueFileName(filePath) + suffix; | ||
const contents = file.contents as string; | ||
if (contents) { | ||
exporter.addMedia(newName, contents); | ||
} | ||
return newName; | ||
} else { | ||
Bugsnag.notify( | ||
`Missing relative path to ${filePath} used ${exporter.firstDeckName}` | ||
); | ||
} | ||
const newName = getUniqueFileName(filePath) + suffix; | ||
const contents = file.contents as string; | ||
if (contents) { | ||
exporter.addMedia(newName, contents); | ||
} | ||
return newName; | ||
|
||
return null; | ||
}; |