aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ext/js/language/dictionary-importer.js77
1 files changed, 46 insertions, 31 deletions
diff --git a/ext/js/language/dictionary-importer.js b/ext/js/language/dictionary-importer.js
index 888d19b0..e060b3b1 100644
--- a/ext/js/language/dictionary-importer.js
+++ b/ext/js/language/dictionary-importer.js
@@ -306,64 +306,79 @@ class DictionaryImporter {
}
async _formatDictionaryTermGlossaryImage(data, context, entry) {
- const dictionary = entry.dictionary;
const {path, width: preferredWidth, height: preferredHeight, title, description, pixelated} = data;
- if (context.media.has(path)) {
- // Already exists
- return data;
- }
+ const {width, height} = await this._getImageMedia(path, context, entry);
+
+ // Create new data
+ const newData = {
+ type: 'image',
+ path,
+ width,
+ height
+ };
+ if (typeof preferredWidth === 'number') { newData.preferredWidth = preferredWidth; }
+ if (typeof preferredHeight === 'number') { newData.preferredHeight = preferredHeight; }
+ if (typeof title === 'string') { newData.title = title; }
+ if (typeof description === 'string') { newData.description = description; }
+ if (typeof pixelated === 'boolean') { newData.pixelated = pixelated; }
+
+ return newData;
+ }
+
+ async _getImageMedia(path, context, entry) {
+ const {media} = context;
+ const {dictionary, reading} = entry;
let errorSource = entry.expression;
- if (entry.reading.length > 0) {
- errorSource += ` (${entry.reading});`;
+ if (reading.length > 0) {
+ errorSource += ` (${reading})`;
+ }
+ errorSource += dictionary;
+
+ const createError = (message) => new Error(`${message} at path ${JSON.stringify(path)} for ${errorSource}`);
+
+ // Check if already added
+ let mediaData = media.get(path);
+ if (typeof mediaData !== 'undefined') {
+ if (MediaUtil.getFileExtensionFromImageMediaType(mediaData.mediaType) === null) {
+ throw createError('Media file is not a valid image');
+ }
+ return mediaData;
}
+ // Find file in archive
const file = context.archive.file(path);
if (file === null) {
- throw new Error(`Could not find image at path ${JSON.stringify(path)} for ${errorSource}`);
+ throw createError('Could not find image');
}
+ // Load file content
const content = await file.async('base64');
const mediaType = MediaUtil.getImageMediaTypeFromFileName(path);
if (mediaType === null) {
- throw new Error(`Could not determine media type for image at path ${JSON.stringify(path)} for ${errorSource}`);
+ throw createError('Could not determine media type for image');
}
+ // Load image data
let image;
try {
image = await this._loadImageBase64(mediaType, content);
} catch (e) {
- throw new Error(`Could not load image at path ${JSON.stringify(path)} for ${errorSource}`);
+ throw createError('Could not load image');
}
- const width = image.naturalWidth;
- const height = image.naturalHeight;
-
// Create image data
- const mediaData = {
+ mediaData = {
dictionary,
path,
mediaType,
- width,
- height,
+ width: image.naturalWidth,
+ height: image.naturalHeight,
content
};
- context.media.set(path, mediaData);
+ media.set(path, mediaData);
- // Create new data
- const newData = {
- type: 'image',
- path,
- width,
- height
- };
- if (typeof preferredWidth === 'number') { newData.preferredWidth = preferredWidth; }
- if (typeof preferredHeight === 'number') { newData.preferredHeight = preferredHeight; }
- if (typeof title === 'string') { newData.title = title; }
- if (typeof description === 'string') { newData.description = description; }
- if (typeof pixelated === 'boolean') { newData.pixelated = pixelated; }
-
- return newData;
+ return mediaData;
}
async _fetchJsonAsset(url) {