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
This commit is contained in:
toasted-nutbread 2021-09-03 22:33:58 -04:00 committed by GitHub
parent 764d59df13
commit 0331374241
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 61 additions and 58 deletions

View File

@ -69,9 +69,9 @@ class DatabaseVM extends VM {
} }
class DatabaseVMDictionaryImporterMediaLoader { class DatabaseVMDictionaryImporterMediaLoader {
async getImageResolution() { async getImageDetails(content) {
// Placeholder values // Placeholder values
return {width: 100, height: 100}; return {content, width: 100, height: 100};
} }
} }

View File

@ -31,6 +31,7 @@
* PermissionsUtil * PermissionsUtil
* ProfileConditionsUtil * ProfileConditionsUtil
* RequestBuilder * RequestBuilder
* StringUtil
* Translator * Translator
* wanakana * wanakana
*/ */
@ -621,7 +622,7 @@ class Backend {
} }
async _onApiGetMedia({targets}) { async _onApiGetMedia({targets}) {
return await this._dictionaryDatabase.getMedia(targets); return await this._getNormalizedDictionaryDatabaseMedia(targets);
} }
_onApiLog({error, level, context}) { _onApiLog({error, level, context}) {
@ -1847,7 +1848,7 @@ class Backend {
detailsList.push(details); detailsList.push(details);
detailsMap.set(key, details); detailsMap.set(key, details);
} }
const mediaList = await this._dictionaryDatabase.getMedia(targets); const mediaList = await this._getNormalizedDictionaryDatabaseMedia(targets);
for (const media of mediaList) { for (const media of mediaList) {
const {dictionary, path} = media; const {dictionary, path} = media;
@ -2283,4 +2284,15 @@ class Backend {
return await this._injectScript(file, tab.id, frameId); return await this._injectScript(file, tab.id, frameId);
} }
async _getNormalizedDictionaryDatabaseMedia(targets) {
const results = await this._dictionaryDatabase.getMedia(targets);
for (const item of results) {
const {content} = item;
if (content instanceof ArrayBuffer) {
item.content = StringUtil.arrayBufferToBase64(content);
}
}
return results;
}
} }

View File

@ -58,4 +58,19 @@ class StringUtil {
return binary; return binary;
} }
} }
/**
* Converts a base64 string to an ArrayBuffer.
* @param content The binary content string encoded in base64.
* @returns A new `ArrayBuffer` object corresponding to the specified content.
*/
static base64ToArrayBuffer(content) {
const binaryContent = atob(content);
const length = binaryContent.length;
const array = new Uint8Array(length);
for (let i = 0; i < length; ++i) {
array[i] = binaryContent.charCodeAt(i);
}
return array.buffer;
}
} }

View File

@ -15,23 +15,17 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
/* global
* MediaUtil
*/
/** /**
* Class used for loading and validating media during the dictionary import process. * Class used for loading and validating media during the dictionary import process.
*/ */
class DictionaryImporterMediaLoader { class DictionaryImporterMediaLoader {
/** /**
* Attempts to load an image using a base64 encoded content and a media type * Attempts to load an image using an ArrayBuffer and a media type to return details about it.
* and returns its resolution. * @param content The binary content for the image, encoded as an ArrayBuffer.
* @param mediaType The media type for the image content. * @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 {content, width, height} on success, otherwise an error is thrown.
* @returns A Promise which resolves with {width, height} on success,
* otherwise an error is thrown.
*/ */
getImageResolution(mediaType, content) { getImageDetails(content, mediaType, transfer) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const image = new Image(); const image = new Image();
const eventListeners = new EventListenerCollection(); const eventListeners = new EventListenerCollection();
@ -42,14 +36,15 @@ class DictionaryImporterMediaLoader {
}; };
eventListeners.addEventListener(image, 'load', () => { eventListeners.addEventListener(image, 'load', () => {
const {naturalWidth: width, naturalHeight: height} = image; const {naturalWidth: width, naturalHeight: height} = image;
if (Array.isArray(transfer)) { transfer.push(content); }
cleanup(); cleanup();
resolve({width, height}); resolve({content, width, height});
}, false); }, false);
eventListeners.addEventListener(image, 'error', () => { eventListeners.addEventListener(image, 'error', () => {
cleanup(); cleanup();
reject(new Error('Image failed to load')); reject(new Error('Image failed to load'));
}, false); }, false);
const blob = MediaUtil.createBlobFromBase64Content(content, mediaType); const blob = new Blob([content], {type: mediaType});
const url = URL.createObjectURL(blob); const url = URL.createObjectURL(blob);
image.src = url; image.src = url;
}); });

View File

@ -328,13 +328,10 @@ class DictionaryImporter {
const media = new Map(); const media = new Map();
const context = {archive, media}; const context = {archive, media};
const promises = [];
for (const requirement of requirements) { for (const requirement of requirements) {
promises.push(this._resolveAsyncRequirement(context, requirement)); await this._resolveAsyncRequirement(context, requirement);
} }
await Promise.all(promises);
return { return {
media: [...media.values()] media: [...media.values()]
}; };
@ -425,7 +422,7 @@ class DictionaryImporter {
} }
// Load file content // Load file content
const content = await file.async('base64'); let content = await file.async('arraybuffer');
const mediaType = MediaUtil.getImageMediaTypeFromFileName(path); const mediaType = MediaUtil.getImageMediaTypeFromFileName(path);
if (mediaType === null) { if (mediaType === null) {
throw createError('Could not determine media type for image'); throw createError('Could not determine media type for image');
@ -435,7 +432,7 @@ class DictionaryImporter {
let width; let width;
let height; let height;
try { try {
({width, height} = await this._mediaLoader.getImageResolution(mediaType, content)); ({content, width, height} = await this._mediaLoader.getImageDetails(content, mediaType));
} catch (e) { } catch (e) {
throw createError('Could not load image'); throw createError('Could not load image');
} }

View File

@ -44,7 +44,7 @@ class DictionaryWorkerHandler {
case 'getDictionaryCounts': case 'getDictionaryCounts':
this._onMessageWithProgress(params, this._getDictionaryCounts.bind(this)); this._onMessageWithProgress(params, this._getDictionaryCounts.bind(this));
break; break;
case 'getImageResolution.response': case 'getImageDetails.response':
this._mediaLoader.handleMessage(params); this._mediaLoader.handleMessage(params);
break; break;
} }

View File

@ -45,21 +45,19 @@ class DictionaryWorkerMediaLoader {
} }
/** /**
* Attempts to load an image using a base64 encoded content and a media type * Attempts to load an image using an ArrayBuffer and a media type to return details about it.
* and returns its resolution. * @param content The binary content for the image, encoded as an ArrayBuffer.
* @param mediaType The media type for the image content. * @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 {content, width, height} on success, otherwise an error is thrown.
* @returns A Promise which resolves with {width, height} on success,
* otherwise an error is thrown.
*/ */
getImageResolution(mediaType, content) { getImageDetails(content, mediaType) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const id = generateId(16); const id = generateId(16);
this._requests.set(id, {resolve, reject}); this._requests.set(id, {resolve, reject});
self.postMessage({ self.postMessage({
action: 'getImageResolution', action: 'getImageDetails',
params: {id, mediaType, content} params: {id, content, mediaType}
}); }, [content]);
}); });
} }
} }

View File

@ -85,8 +85,8 @@ class DictionaryWorker {
case 'progress': case 'progress':
this._onMessageProgress(params, details.onProgress); this._onMessageProgress(params, details.onProgress);
break; break;
case 'getImageResolution': case 'getImageDetails':
this._onMessageGetImageResolution(params, details.worker); this._onMessageGetImageDetails(params, details.worker);
break; break;
} }
} }
@ -115,16 +115,17 @@ class DictionaryWorker {
onProgress(...args); onProgress(...args);
} }
async _onMessageGetImageResolution(params, worker) { async _onMessageGetImageDetails(params, worker) {
const {id, mediaType, content} = params; const {id, content, mediaType} = params;
const transfer = [];
let response; let response;
try { try {
const result = await this._dictionaryImporterMediaLoader.getImageResolution(mediaType, content); const result = await this._dictionaryImporterMediaLoader.getImageDetails(content, mediaType, transfer);
response = {id, result}; response = {id, result};
} catch (e) { } catch (e) {
response = {id, error: serializeError(e)}; response = {id, error: serializeError(e)};
} }
worker.postMessage({action: 'getImageResolution.response', params: response}); worker.postMessage({action: 'getImageDetails.response', params: response}, transfer);
} }
_formatimportDictionaryResult(result) { _formatimportDictionaryResult(result) {

View File

@ -16,7 +16,7 @@
*/ */
/* global /* global
* MediaUtil * StringUtil
*/ */
class MediaLoader { class MediaLoader {
@ -86,7 +86,8 @@ class MediaLoader {
const token = this._token; const token = this._token;
const data = (await yomichan.api.getMedia([{path, dictionary}]))[0]; const data = (await yomichan.api.getMedia([{path, dictionary}]))[0];
if (token === this._token && data !== null) { if (token === this._token && data !== null) {
const blob = MediaUtil.createBlobFromBase64Content(data.content, data.mediaType); const buffer = StringUtil.base64ToArrayBuffer(data.content);
const blob = new Blob([buffer], {type: data.mediaType});
const url = URL.createObjectURL(blob); const url = URL.createObjectURL(blob);
cachedData.data = data; cachedData.data = data;
cachedData.url = url; cachedData.url = url;

View File

@ -129,20 +129,4 @@ class MediaUtil {
return null; return null;
} }
} }
/**
* Creates a new `Blob` object from a base64 string of content.
* @param content The binary content string encoded in base64.
* @param mediaType The type of the media.
* @returns A new `Blob` object corresponding to the specified content.
*/
static createBlobFromBase64Content(content, mediaType) {
const binaryContent = atob(content);
const length = binaryContent.length;
const array = new Uint8Array(length);
for (let i = 0; i < length; ++i) {
array[i] = binaryContent.charCodeAt(i);
}
return new Blob([array.buffer], {type: mediaType});
}
} }

View File

@ -100,6 +100,7 @@
<script src="/js/comm/frame-endpoint.js"></script> <script src="/js/comm/frame-endpoint.js"></script>
<script src="/js/data/anki-note-builder.js"></script> <script src="/js/data/anki-note-builder.js"></script>
<script src="/js/data/anki-util.js"></script> <script src="/js/data/anki-util.js"></script>
<script src="/js/data/sandbox/string-util.js"></script>
<script src="/js/display/display.js"></script> <script src="/js/display/display.js"></script>
<script src="/js/display/display-anki.js"></script> <script src="/js/display/display-anki.js"></script>
<script src="/js/display/display-audio.js"></script> <script src="/js/display/display-audio.js"></script>

View File

@ -86,6 +86,7 @@
<script src="/js/comm/cross-frame-api.js"></script> <script src="/js/comm/cross-frame-api.js"></script>
<script src="/js/data/anki-note-builder.js"></script> <script src="/js/data/anki-note-builder.js"></script>
<script src="/js/data/anki-util.js"></script> <script src="/js/data/anki-util.js"></script>
<script src="/js/data/sandbox/string-util.js"></script>
<script src="/js/display/display.js"></script> <script src="/js/display/display.js"></script>
<script src="/js/display/display-anki.js"></script> <script src="/js/display/display-anki.js"></script>
<script src="/js/display/display-audio.js"></script> <script src="/js/display/display-audio.js"></script>

View File

@ -3488,7 +3488,6 @@
<script src="/js/language/sandbox/dictionary-data-util.js"></script> <script src="/js/language/sandbox/dictionary-data-util.js"></script>
<script src="/js/language/sandbox/japanese-util.js"></script> <script src="/js/language/sandbox/japanese-util.js"></script>
<script src="/js/media/audio-system.js"></script> <script src="/js/media/audio-system.js"></script>
<script src="/js/media/media-util.js"></script>
<script src="/js/media/text-to-speech-audio.js"></script> <script src="/js/media/text-to-speech-audio.js"></script>
<script src="/js/pages/settings/anki-controller.js"></script> <script src="/js/pages/settings/anki-controller.js"></script>
<script src="/js/pages/settings/anki-templates-controller.js"></script> <script src="/js/pages/settings/anki-templates-controller.js"></script>

View File

@ -407,7 +407,6 @@
<script src="/js/input/hotkey-util.js"></script> <script src="/js/input/hotkey-util.js"></script>
<script src="/js/language/dictionary-importer-media-loader.js"></script> <script src="/js/language/dictionary-importer-media-loader.js"></script>
<script src="/js/language/dictionary-worker.js"></script> <script src="/js/language/dictionary-worker.js"></script>
<script src="/js/media/media-util.js"></script>
<script src="/js/pages/settings/dictionary-controller.js"></script> <script src="/js/pages/settings/dictionary-controller.js"></script>
<script src="/js/pages/settings/dictionary-import-controller.js"></script> <script src="/js/pages/settings/dictionary-import-controller.js"></script>
<script src="/js/pages/settings/generic-setting-controller.js"></script> <script src="/js/pages/settings/generic-setting-controller.js"></script>