From ee2466eb22754c7f4a3296d23f8002bb97c4dfb9 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Fri, 3 Sep 2021 22:33:58 -0400 Subject: Dictionary media import improvements (#1926) * Add base64ToArrayBuffer to StringUtil * Remove unnecessary media-util.js import * Run async requirements in serial rather than parallel * Update API.getMedia handler to convert ArrayBuffer content to base64 * Rename getImageResolution to getImageDetails * Change parameter order of getImageDetails * Pre-process and store media as an ArrayBuffer * Remove MediaUtil.createBlobFromBase64Content * Fix Anki media injection --- ext/js/language/dictionary-importer-media-loader.js | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) (limited to 'ext/js/language/dictionary-importer-media-loader.js') diff --git a/ext/js/language/dictionary-importer-media-loader.js b/ext/js/language/dictionary-importer-media-loader.js index bbcc5476..27ddde34 100644 --- a/ext/js/language/dictionary-importer-media-loader.js +++ b/ext/js/language/dictionary-importer-media-loader.js @@ -15,23 +15,17 @@ * along with this program. If not, see . */ -/* global - * MediaUtil - */ - /** * Class used for loading and validating media during the dictionary import process. */ class DictionaryImporterMediaLoader { /** - * Attempts to load an image using a base64 encoded content and a media type - * and returns its resolution. + * Attempts to load an image using an ArrayBuffer and a media type to return details about it. + * @param content The binary content for the image, encoded as an ArrayBuffer. * @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 {width, height} on success, - * otherwise an error is thrown. + * @returns A Promise which resolves with {content, width, height} on success, otherwise an error is thrown. */ - getImageResolution(mediaType, content) { + getImageDetails(content, mediaType, transfer) { return new Promise((resolve, reject) => { const image = new Image(); const eventListeners = new EventListenerCollection(); @@ -42,14 +36,15 @@ class DictionaryImporterMediaLoader { }; eventListeners.addEventListener(image, 'load', () => { const {naturalWidth: width, naturalHeight: height} = image; + if (Array.isArray(transfer)) { transfer.push(content); } cleanup(); - resolve({width, height}); + resolve({content, width, height}); }, false); eventListeners.addEventListener(image, 'error', () => { cleanup(); reject(new Error('Image failed to load')); }, false); - const blob = MediaUtil.createBlobFromBase64Content(content, mediaType); + const blob = new Blob([content], {type: mediaType}); const url = URL.createObjectURL(blob); image.src = url; }); -- cgit v1.2.3