Fix repeated dictionary image importing (#1685)

This commit is contained in:
toasted-nutbread 2021-05-16 20:11:32 -04:00 committed by GitHub
parent 76c29134b3
commit 12451eaf61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -306,49 +306,8 @@ class DictionaryImporter {
} }
async _formatDictionaryTermGlossaryImage(data, context, entry) { async _formatDictionaryTermGlossaryImage(data, context, entry) {
const dictionary = entry.dictionary;
const {path, width: preferredWidth, height: preferredHeight, title, description, pixelated} = data; const {path, width: preferredWidth, height: preferredHeight, title, description, pixelated} = data;
if (context.media.has(path)) { const {width, height} = await this._getImageMedia(path, context, entry);
// 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 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}`);
}
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}`);
}
const width = image.naturalWidth;
const height = image.naturalHeight;
// Create image data
const mediaData = {
dictionary,
path,
mediaType,
width,
height,
content
};
context.media.set(path, mediaData);
// Create new data // Create new data
const newData = { const newData = {
@ -366,6 +325,62 @@ class DictionaryImporter {
return newData; return newData;
} }
async _getImageMedia(path, context, entry) {
const {media} = context;
const {dictionary, reading} = entry;
let errorSource = entry.expression;
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 createError('Could not find image');
}
// Load file content
const content = await file.async('base64');
const mediaType = MediaUtil.getImageMediaTypeFromFileName(path);
if (mediaType === null) {
throw createError('Could not determine media type for image');
}
// Load image data
let image;
try {
image = await this._loadImageBase64(mediaType, content);
} catch (e) {
throw createError('Could not load image');
}
// Create image data
mediaData = {
dictionary,
path,
mediaType,
width: image.naturalWidth,
height: image.naturalHeight,
content
};
media.set(path, mediaData);
return mediaData;
}
async _fetchJsonAsset(url) { async _fetchJsonAsset(url) {
const response = await fetch(chrome.runtime.getURL(url), { const response = await fetch(chrome.runtime.getURL(url), {
method: 'GET', method: 'GET',