1
0
Fork 0

fix(gui): attempt to decode unicode EXIF tags

This commit is contained in:
Sean Sube 2023-07-21 15:31:14 -05:00
parent 063acc6f20
commit 27a21dfa62
Signed by: ssube
GPG Key ID: 3EED7B957D362AF1
2 changed files with 29 additions and 7 deletions

View File

@ -1,4 +1,4 @@
import { doesExist, mustExist } from '@apextoaster/js-utils'; import { InvalidArgumentError, Maybe, doesExist, mustExist } from '@apextoaster/js-utils';
import { Delete as DeleteIcon, ImageSearch, Save as SaveIcon } from '@mui/icons-material'; import { Delete as DeleteIcon, ImageSearch, Save as SaveIcon } from '@mui/icons-material';
import { import {
Autocomplete, Autocomplete,
@ -172,21 +172,42 @@ export async function parseImageParams(file: File): Promise<Partial<Txt2ImgParam
const tags = await ExifReader.load(file); const tags = await ExifReader.load(file);
// handle lowercase variation from my earlier mistakes // handle lowercase variation from my earlier mistakes
const makerNote = tags.MakerNote || tags['maker note']; const makerNote = decodeTag(defaultTo(tags.MakerNote, tags['maker note']));
// eslint-disable-next-line dot-notation, @typescript-eslint/strict-boolean-expressions // eslint-disable-next-line dot-notation, @typescript-eslint/strict-boolean-expressions
const userComment = tags.UserComment || tags['Parameters'] || tags['parameters']; const userComment = decodeTag(defaultTo(defaultTo(tags.UserComment, tags['Parameters']), tags['parameters']));
if (doesExist(makerNote) && isString(makerNote.value) && isProbablyJSON(makerNote.value)) { if (doesExist(makerNote) && isProbablyJSON(makerNote)) {
return parseJSONParams(makerNote.value); return parseJSONParams(makerNote);
} }
if (doesExist(userComment) && isString(userComment.value)) { if (doesExist(userComment)) {
return parseAutoComment(userComment.value); return parseAutoComment(userComment);
} }
return {}; return {};
} }
export function isNumberArray(it: unknown): it is Array<number> {
return Array.isArray(it) && typeof it[0] === 'number';
}
export function decodeTag(tag: Maybe<ExifReader.XmpTag | (ExifReader.NumberTag & ExifReader.NumberArrayTag)>): Maybe<string> {
// eslint-disable-next-line no-restricted-syntax
if (!doesExist(tag)) {
return undefined;
}
if (isString(tag.value)) {
return tag.value;
}
if (tag.description === '[Unicode encoded text]' && isNumberArray(tag.value)) {
return Buffer.from(tag.value).toString('utf-8');
}
throw new InvalidArgumentError('tag value cannot be decoded');
}
export async function parseJSONParams(json: string): Promise<Partial<Txt2ImgParams>> { export async function parseJSONParams(json: string): Promise<Partial<Txt2ImgParams>> {
const data = JSON.parse(json); const data = JSON.parse(json);
const params: Partial<Txt2ImgParams> = { const params: Partial<Txt2ImgParams> = {

View File

@ -102,6 +102,7 @@
"venv", "venv",
"virtualenv", "virtualenv",
"VRAM", "VRAM",
"webp",
"zustand" "zustand"
] ]
} }