From 8106f4744b07833526d16acf656eda11d29b99ad Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 1 Mar 2020 22:36:42 -0500 Subject: Add support for importing and storing media files --- ext/bg/js/dictionary-importer.js | 90 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) (limited to 'ext/bg/js/dictionary-importer.js') diff --git a/ext/bg/js/dictionary-importer.js b/ext/bg/js/dictionary-importer.js index bf6809ec..8a4497a3 100644 --- a/ext/bg/js/dictionary-importer.js +++ b/ext/bg/js/dictionary-importer.js @@ -18,6 +18,7 @@ /* global * JSZip * JsonSchema + * mediaUtility * requestJson */ @@ -148,6 +149,22 @@ class DictionaryImporter { } } + // Extended data support + const extendedDataContext = { + archive, + media: new Map() + }; + for (const entry of termList) { + const glossaryList = entry.glossary; + for (let i = 0, ii = glossaryList.length; i < ii; ++i) { + const glossary = glossaryList[i]; + if (typeof glossary !== 'object' || glossary === null) { continue; } + glossaryList[i] = await this._formatDictionaryTermGlossaryObject(glossary, extendedDataContext, entry); + } + } + + const media = [...extendedDataContext.media.values()]; + // Add dictionary const summary = this._createSummary(dictionaryTitle, version, index, {prefixWildcardsSupported}); @@ -188,6 +205,7 @@ class DictionaryImporter { await bulkAdd('kanji', kanjiList); await bulkAdd('kanjiMeta', kanjiMetaList); await bulkAdd('tagMeta', tagList); + await bulkAdd('media', media); return {result: summary, errors}; } @@ -275,4 +293,76 @@ class DictionaryImporter { return [termBank, termMetaBank, kanjiBank, kanjiMetaBank, tagBank]; } + + async _formatDictionaryTermGlossaryObject(data, context, entry) { + switch (data.type) { + case 'text': + return data.text; + case 'image': + return await this._formatDictionaryTermGlossaryImage(data, context, entry); + default: + throw new Error(`Unhandled data type: ${data.type}`); + } + } + + async _formatDictionaryTermGlossaryImage(data, context, entry) { + const dictionary = entry.dictionary; + const {path, width: preferredWidth, height: preferredHeight, title, description, pixelated} = data; + if (context.media.has(path)) { + // Already exists + return data; + } + + let errorSource = entry.expression; + if (entry.reading.length > 0) { + errorSource += ` (${entry.reading});`; + } + + const file = context.archive.file(path); + if (file === null) { + throw new Error(`Could not find image at path ${JSON.stringify(path)} for ${errorSource}`); + } + + const source = await file.async('base64'); + const mediaType = mediaUtility.getImageMediaTypeFromFileName(path); + if (mediaType === null) { + throw new Error(`Could not determine media type for image at path ${JSON.stringify(path)} for ${errorSource}`); + } + + let image; + try { + image = await mediaUtility.loadImage(mediaType, source); + } catch (e) { + throw new Error(`Could not load image at path ${JSON.stringify(path)} for ${errorSource}`); + } + + const width = image.naturalWidth; + const height = image.naturalHeight; + + // Create image data + const mediaData = { + dictionary, + path, + mediaType, + width, + height, + source + }; + context.media.set(path, mediaData); + + // Create new data + const newData = { + type: 'image', + path, + width, + height + }; + if (typeof preferredWidth === 'number') { newData.preferredWidth = preferredWidth; } + if (typeof preferredHeight === 'number') { newData.preferredHeight = preferredHeight; } + if (typeof title === 'string') { newData.title = title; } + if (typeof description === 'string') { newData.description = description; } + if (typeof pixelated === 'boolean') { newData.pixelated = pixelated; } + + return newData; + } } -- cgit v1.2.3 From 7faaf4e45737dd06ab3fdf189bd9c1d26ad1349d Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 19 Apr 2020 10:16:59 -0400 Subject: Use 'content' instead of 'source' to contain media file data --- ext/bg/js/dictionary-importer.js | 6 +++--- ext/bg/js/media-utility.js | 4 ++-- ext/mixed/js/media-loader.js | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) (limited to 'ext/bg/js/dictionary-importer.js') diff --git a/ext/bg/js/dictionary-importer.js b/ext/bg/js/dictionary-importer.js index 8a4497a3..b5c535bb 100644 --- a/ext/bg/js/dictionary-importer.js +++ b/ext/bg/js/dictionary-importer.js @@ -323,7 +323,7 @@ class DictionaryImporter { throw new Error(`Could not find image at path ${JSON.stringify(path)} for ${errorSource}`); } - const source = await file.async('base64'); + const content = await file.async('base64'); const mediaType = mediaUtility.getImageMediaTypeFromFileName(path); if (mediaType === null) { throw new Error(`Could not determine media type for image at path ${JSON.stringify(path)} for ${errorSource}`); @@ -331,7 +331,7 @@ class DictionaryImporter { let image; try { - image = await mediaUtility.loadImage(mediaType, source); + image = await mediaUtility.loadImage(mediaType, content); } catch (e) { throw new Error(`Could not load image at path ${JSON.stringify(path)} for ${errorSource}`); } @@ -346,7 +346,7 @@ class DictionaryImporter { mediaType, width, height, - source + content }; context.media.set(path, mediaData); diff --git a/ext/bg/js/media-utility.js b/ext/bg/js/media-utility.js index 24686838..febc509a 100644 --- a/ext/bg/js/media-utility.js +++ b/ext/bg/js/media-utility.js @@ -52,7 +52,7 @@ const mediaUtility = (() => { } } - function loadImage(mediaType, base64Source) { + function loadImage(mediaType, content) { return new Promise((resolve, reject) => { const image = new Image(); const eventListeners = new EventListenerCollection(); @@ -64,7 +64,7 @@ const mediaUtility = (() => { eventListeners.removeAllEventListeners(); reject(new Error('Image failed to load')); }, false); - image.src = `data:${mediaType};base64,${base64Source}`; + image.src = `data:${mediaType};base64,${content}`; }); } diff --git a/ext/mixed/js/media-loader.js b/ext/mixed/js/media-loader.js index c89c4f12..5e3177fc 100644 --- a/ext/mixed/js/media-loader.js +++ b/ext/mixed/js/media-loader.js @@ -86,7 +86,7 @@ class MediaLoader { const token = this._token; const data = (await apiGetMedia([{path, dictionaryName}]))[0]; if (token === this._token && data !== null) { - const sourceArrayBuffer = this._base64ToArrayBuffer(data.source); + const sourceArrayBuffer = this._base64ToArrayBuffer(data.content); const blob = new Blob([sourceArrayBuffer], {type: data.mediaType}); const url = URL.createObjectURL(blob); cachedData.data = data; @@ -95,8 +95,8 @@ class MediaLoader { return cachedData; } - _base64ToArrayBuffer(source) { - const binarySource = window.atob(source); + _base64ToArrayBuffer(content) { + const binarySource = window.atob(content); const length = binarySource.length; const array = new Uint8Array(length); for (let i = 0; i < length; ++i) { -- cgit v1.2.3 From 0e80c0d5d03b51c56e72657a46c868c9a2c1137d Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 19 Apr 2020 10:24:36 -0400 Subject: Rename loadImage to loadImageBase64 for clarity --- ext/bg/js/dictionary-importer.js | 2 +- ext/bg/js/media-utility.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'ext/bg/js/dictionary-importer.js') diff --git a/ext/bg/js/dictionary-importer.js b/ext/bg/js/dictionary-importer.js index b5c535bb..3727f7ee 100644 --- a/ext/bg/js/dictionary-importer.js +++ b/ext/bg/js/dictionary-importer.js @@ -331,7 +331,7 @@ class DictionaryImporter { let image; try { - image = await mediaUtility.loadImage(mediaType, content); + image = await mediaUtility.loadImageBase64(mediaType, content); } catch (e) { throw new Error(`Could not load image at path ${JSON.stringify(path)} for ${errorSource}`); } diff --git a/ext/bg/js/media-utility.js b/ext/bg/js/media-utility.js index 8a46cb49..fcbf9d37 100644 --- a/ext/bg/js/media-utility.js +++ b/ext/bg/js/media-utility.js @@ -52,7 +52,7 @@ const mediaUtility = (() => { } } - function loadImage(mediaType, content) { + function loadImageBase64(mediaType, content) { return new Promise((resolve, reject) => { const image = new Image(); const eventListeners = new EventListenerCollection(); @@ -70,6 +70,6 @@ const mediaUtility = (() => { return { getImageMediaTypeFromFileName, - loadImage + loadImageBase64 }; })(); -- cgit v1.2.3