diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2021-07-29 21:16:55 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-29 21:16:55 -0400 |
commit | b99850ed54ecaa903a4bd1c2bcf3ae1cd405a3ed (patch) | |
tree | 4e50d45250211d485dd882ca8bc5995d43f2242c | |
parent | a0fa67d57c6055dd967f43112bfe9535ec46bb5c (diff) |
Call URL.revokeObjectURL for created URLs (#1858)
* Call URL.revokeObjectURL for created URLs
* Refactor image usage
-rw-r--r-- | ext/js/language/dictionary-importer.js | 30 |
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); |