diff options
| author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2021-05-16 20:11:32 -0400 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-05-16 20:11:32 -0400 | 
| commit | 12451eaf61281e588fb8ef5f911d604ba34aca92 (patch) | |
| tree | ebfa70be32a92ce470f154f431833d9e74d1c304 | |
| parent | 76c29134b374efb6aed56839b66088ceedc415d3 (diff) | |
Fix repeated dictionary image importing (#1685)
| -rw-r--r-- | ext/js/language/dictionary-importer.js | 77 | 
1 files changed, 46 insertions, 31 deletions
| diff --git a/ext/js/language/dictionary-importer.js b/ext/js/language/dictionary-importer.js index 888d19b0..e060b3b1 100644 --- a/ext/js/language/dictionary-importer.js +++ b/ext/js/language/dictionary-importer.js @@ -306,64 +306,79 @@ class DictionaryImporter {      }      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; -        } +        const {width, height} = await this._getImageMedia(path, context, entry); + +        // 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; +    } + +    async _getImageMedia(path, context, entry) { +        const {media} = context; +        const {dictionary, reading} = entry;          let errorSource = entry.expression; -        if (entry.reading.length > 0) { -            errorSource += ` (${entry.reading});`; +        if (reading.length > 0) { +            errorSource += ` (${reading})`; +        } +        errorSource += dictionary; + +        const createError = (message) => new Error(`${message} at path ${JSON.stringify(path)} for ${errorSource}`); + +        // Check if already added +        let mediaData = media.get(path); +        if (typeof mediaData !== 'undefined') { +            if (MediaUtil.getFileExtensionFromImageMediaType(mediaData.mediaType) === null) { +                throw createError('Media file is not a valid image'); +            } +            return mediaData;          } +        // Find file in archive          const file = context.archive.file(path);          if (file === null) { -            throw new Error(`Could not find image at path ${JSON.stringify(path)} for ${errorSource}`); +            throw createError('Could not find image');          } +        // Load file content          const content = await file.async('base64');          const mediaType = MediaUtil.getImageMediaTypeFromFileName(path);          if (mediaType === null) { -            throw new Error(`Could not determine media type for image at path ${JSON.stringify(path)} for ${errorSource}`); +            throw createError('Could not determine media type for image');          } +        // Load image data          let image;          try {              image = await this._loadImageBase64(mediaType, content);          } catch (e) { -            throw new Error(`Could not load image at path ${JSON.stringify(path)} for ${errorSource}`); +            throw createError('Could not load image');          } -        const width = image.naturalWidth; -        const height = image.naturalHeight; -          // Create image data -        const mediaData = { +        mediaData = {              dictionary,              path,              mediaType, -            width, -            height, +            width: image.naturalWidth, +            height: image.naturalHeight,              content          }; -        context.media.set(path, mediaData); +        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; +        return mediaData;      }      async _fetchJsonAsset(url) { |