diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2020-09-26 13:42:31 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-26 13:42:31 -0400 |
commit | 079307899f3b57f2aec121631b56fb8780430e8e (patch) | |
tree | 919ac88504af5895f9dbcc778ff2dd3c56a5ec26 /ext/bg/js/backend.js | |
parent | 0b51488f1f639885b518fabf683b70db577afa67 (diff) |
Media utility refactor (#859)
* Move loadImageBase64 into DictionaryImporter
* Convert mediaUtility to a class
* Add getFileExtensionFromImageMediaType to MediaUtility
* Use MediaUtility instead of _getImageExtensionFromMediaType
* Use MediaUtility in ClipboardReader to validate images before reading
Diffstat (limited to 'ext/bg/js/backend.js')
-rw-r--r-- | ext/bg/js/backend.js | 19 |
1 files changed, 8 insertions, 11 deletions
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index b9d85b84..2a90e8e1 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -24,6 +24,7 @@ * Environment * JsonSchemaValidator * Mecab + * MediaUtility * ObjectPropertyAccessor * OptionsUtil * ProfileConditions @@ -39,10 +40,12 @@ class Backend { this._translator = new Translator(this._dictionaryDatabase); this._anki = new AnkiConnect(); this._mecab = new Mecab(); + this._mediaUtility = new MediaUtility(); this._clipboardReader = new ClipboardReader({ document: (typeof document === 'object' && document !== null ? document : null), pasteTargetSelector: '#clipboard-paste-target', - imagePasteTargetSelector: '#clipboard-image-paste-target' + imagePasteTargetSelector: '#clipboard-image-paste-target', + mediaUtility: this._mediaUtility }); this._clipboardMonitor = new ClipboardMonitor({ clipboardReader: this._clipboardReader @@ -1570,7 +1573,8 @@ class Backend { const dataUrl = await this._getScreenshot(windowId, tabId, ownerFrameId, format, quality); const {mediaType, data} = this._getDataUrlInfo(dataUrl); - const extension = this._getImageExtensionFromMediaType(mediaType); + const extension = this._mediaUtility.getFileExtensionFromImageMediaType(mediaType); + if (extension === null) { throw new Error('Unknown image media type'); } let fileName = `yomichan_browser_screenshot_${reading}_${this._ankNoteDateToString(now)}.${extension}`; fileName = this._replaceInvalidFileNameCharacters(fileName); @@ -1593,7 +1597,8 @@ class Backend { } const {mediaType, data} = this._getDataUrlInfo(dataUrl); - const extension = this._getImageExtensionFromMediaType(mediaType); + const extension = this._mediaUtility.getFileExtensionFromImageMediaType(mediaType); + if (extension === null) { throw new Error('Unknown image media type'); } let fileName = `yomichan_clipboard_image_${reading}_${this._ankNoteDateToString(now)}.${extension}`; fileName = this._replaceInvalidFileNameCharacters(fileName); @@ -1636,14 +1641,6 @@ class Backend { return {mediaType, data}; } - _getImageExtensionFromMediaType(mediaType) { - switch (mediaType.toLowerCase()) { - case 'image/png': return 'png'; - case 'image/jpeg': return 'jpeg'; - default: throw new Error('Unknown image media type'); - } - } - _triggerDatabaseUpdated(type, cause) { this._translator.clearDatabaseCaches(); this._sendMessageAllTabs('databaseUpdated', {type, cause}); |