aboutsummaryrefslogtreecommitdiff
path: root/ext/js/language/dictionary-importer.js
diff options
context:
space:
mode:
Diffstat (limited to 'ext/js/language/dictionary-importer.js')
-rw-r--r--ext/js/language/dictionary-importer.js30
1 files changed, 19 insertions, 11 deletions
diff --git a/ext/js/language/dictionary-importer.js b/ext/js/language/dictionary-importer.js
index b735953a..f89a33d3 100644
--- a/ext/js/language/dictionary-importer.js
+++ b/ext/js/language/dictionary-importer.js
@@ -424,9 +424,10 @@ class DictionaryImporter {
}
// Load image data
- let image;
+ let width;
+ let height;
try {
- image = await this._loadImageBase64(mediaType, content);
+ ({width, height} = await this._getImageResolution(mediaType, content));
} catch (e) {
throw createError('Could not load image');
}
@@ -436,8 +437,8 @@ class DictionaryImporter {
dictionary,
path,
mediaType,
- width: image.naturalWidth,
- height: image.naturalHeight,
+ width,
+ height,
content
};
media.set(path, mediaData);
@@ -461,22 +462,29 @@ class DictionaryImporter {
}
/**
- * Attempts to load an image using a base64 encoded content and a media type.
+ * Attempts to load an image using a base64 encoded content and a media type
+ * and returns its resolution.
* @param mediaType The media type for the image content.
* @param content The binary content for the image, encoded in base64.
- * @returns A Promise which resolves with an HTMLImageElement instance on
- * successful load, otherwise an error is thrown.
+ * @returns A Promise which resolves with {width, height} on success,
+ * otherwise an error is thrown.
*/
- _loadImageBase64(mediaType, content) {
+ _getImageResolution(mediaType, content) {
return new Promise((resolve, reject) => {
const image = new Image();
const eventListeners = new EventListenerCollection();
- eventListeners.addEventListener(image, 'load', () => {
+ const cleanup = () => {
+ image.removeAttribute('src');
+ URL.revokeObjectURL(url);
eventListeners.removeAllEventListeners();
- resolve(image);
+ };
+ eventListeners.addEventListener(image, 'load', () => {
+ const {naturalWidth: width, naturalHeight: height} = image;
+ cleanup();
+ resolve({width, height});
}, false);
eventListeners.addEventListener(image, 'error', () => {
- eventListeners.removeAllEventListeners();
+ cleanup();
reject(new Error('Image failed to load'));
}, false);
const blob = MediaUtil.createBlobFromBase64Content(content, mediaType);