From b99850ed54ecaa903a4bd1c2bcf3ae1cd405a3ed Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 29 Jul 2021 21:16:55 -0400 Subject: Call URL.revokeObjectURL for created URLs (#1858) * Call URL.revokeObjectURL for created URLs * Refactor image usage --- ext/js/language/dictionary-importer.js | 30 +++++++++++++++++++----------- 1 file 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); -- cgit v1.2.3