All files / lib exif.js

85.26% Statements 81/95
72.3% Branches 47/65
91.3% Functions 21/23
86.07% Lines 68/79

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271                                                  2x 2x       2x 2x     2x 6x   2x           6x             4x   2x   6x                     2x   2x   2x 2x 3x                                                 5x   5x 5x   5x                 4x                           9x               5x     13x       13x                                 26x   5x           8x 7x 6x     1x     2x 1x       10x                         19x   15x 10x 9x 8x 1x 4x     7x                     2x               2x 2x   10x 10x 10x 10x     6x 6x 4x 6x   4x             2x 2x 2x           2x 2x 2x         2x         2x   2x 2x 6x 6x   2x 154140x    
import { match, type } from 'arktype';
import * as dates from 'date-fns';
import * as exifParser from 'exif-parser';
import piexif from 'piexifjs';
 
import { Schemas } from './database.js';
import { EXIF_GPS_FIELDS } from './exiffields.js';
import * as db from './idb.svelte.js';
import { storeMetadataValue } from './metadata.js';
import { ensureNamespacedMetadataId } from './schemas/metadata.js';
import { toasts } from './toasts.svelte.js';
 
/**
 * @import { MetadataInferOptions, MetadataType } from './database.js';
 * @import { RuntimeValue } from './metadata.js';
 */
 
/**
 *
 * @param {string} sessionId
 * @param {string} imageFileId
 * @param {ArrayBuffer|Buffer} imageBytes
 * @param {{ type: string; name: string }} file
 */
export async function processExifData(sessionId, imageFileId, imageBytes, file) {
	const session = await db.tables.Session.get(sessionId);
	Iif (!session) {
		throw new Error(`Session ${sessionId} introuvable`);
	}
 
	const protocol = await db.tables.Protocol.get(session.protocol);
	Iif (!protocol) {
		throw new Error(`Protocole ${session.protocol} introuvable`);
	}
	const metadataOfProtocol = await db.tables.Metadata.list().then((defs) =>
		defs.filter((def) => protocol.metadata.includes(def.id))
	);
	const metadataFromExif = await extractMetadata(
		// 2^16 + 100 of margin
		// see https://www.npmjs.com/package/exif-parser#creating-a-parser
		imageBytes.slice(0, 65_635),
		(metadataOfProtocol ?? [])
			.map(({ infer, type, id }) =>
				match
					.case(
						[
							{ exif: 'string' },
							'|',
							{ latitude: { exif: 'string' }, longitude: { exif: 'string' } }
						],
						(infer) => /** @type {ExifExtractionPlanItem} */ ({ key: id, infer, type })
					)
					.default(() => undefined)(infer)
			)
			.filter((entry) => entry !== undefined)
	).catch((e) => {
		console.warn(e);
		if (file.type === 'image/jpeg') {
			toasts.warn(
				`Impossible d'extraire les métadonnées EXIF de ${file.name}: ${e?.toString() ?? 'Erreur inattendue'}`
			);
		}
		return {};
	});
 
	const images = await db
		.list('Image')
		.then((imgs) => imgs.filter((img) => img.fileId === imageFileId));
 
	for (const { id: subjectId } of images) {
		for (const [key, { value, confidence }] of Object.entries(metadataFromExif)) {
			await storeMetadataValue({
				db: db.databaseHandle(),
				subjectId,
				sessionId: session.id,
				metadataId: ensureNamespacedMetadataId(key, protocol.id),
				value,
				confidence
			});
		}
	}
}
 
/**
 * @typedef {object} ExifExtractionPlanItem
 * @property {string} key key in the output object
 * @property {MetadataInferOptions | { latitude: MetadataInferOptions, longitude: MetadataInferOptions }} infer how to extract the value
 * @property {MetadataType} type type to coerce the extracted value to
 */
 
/**
 * @param {ArrayBuffer | Buffer} buffer buffer of the image to extract EXIF data from
 * @param {ExifExtractionPlanItem[]} extractionPlan
 * @returns {Promise<Record<string, { value: unknown; confidence: number; alternatives: Record<string, unknown> }>>}
 */
export async function extractMetadata(buffer, extractionPlan) {
	const exif = exifParser.create(buffer).enableImageSize(false).parse();
 
	Iif (!exif) return {};
	console.debug('Starting EXIF Extraction', { extractionPlan, exif });
 
	const extract = match
		.case(
			{
				type: '"location"',
				infer: {
					latitude: { exif: 'string' },
					longitude: { exif: 'string' }
				}
			},
			({ infer }) => ({
				confidence: 1,
				alternatives: {},
				value: {
					longitude: coerceExifValue(exif.tags[infer.longitude.exif], 'float'),
					latitude: coerceExifValue(exif.tags[infer.latitude.exif], 'float')
				}
			})
		)
		.case(
			{
				type: Schemas.MetadataTypeSchema,
				infer: { exif: 'string' }
			},
			({ infer, type }) => ({
				confidence: 1,
				alternatives: {},
				value: coerceExifValue(exif.tags[infer.exif], type)
			})
		)
		.default(() => undefined);
 
	return Object.fromEntries(
		extractionPlan
			.map(({ key: id, ...option }) => {
				return /** @type {const} */ ([id, extract(option)]);
			})
			.filter(
				([, extracted]) =>
					extracted !== undefined &&
					!type({ latitude: 'number.NaN', longitude: 'number.NaN' }).allows(
						extracted.value
					) &&
					!Number.isNaN(extracted.value)
			)
	);
}
 
/**
 *
 * @template {import('./database.js').MetadataType} T
 * @param {unknown} value
 * @param {T} coerceTo
 * @returns {import('./metadata.js').RuntimeValue<T>}
 */
export function coerceExifValue(value, coerceTo) {
	switch (coerceTo) {
		case 'string':
			return value?.toString() ?? '';
 
		case 'boolean':
			return Boolean(value);
 
		case 'date':
			if (typeof value !== 'number') throw new Error('Date value must be a number');
			if (Number.isNaN(value)) throw new Error('Date value is invalid');
			return new Date(value * 1e3);
 
		case 'boundingbox':
			throw new Error('Bounding box not supported in EXIF');
 
		case 'enum':
			if (typeof value !== 'string') throw new Error('Enum value must be a string');
			return value;
 
		case 'integer':
		case 'float':
			return Number(value);
 
		default:
			throw new Error(`Unknown type ${coerceTo}`);
	}
}
 
/**
 * Serialize a value to a string for EXIF writing
 * @param {unknown} value
 * @returns {string|any[]}
 */
export function serializeExifValue(value) {
	if (value instanceof Date) return dates.format(value, 'yyyy:MM:dd HH:mm:ss');
	// Let multivalued exif entries through
	if (Array.isArray(value)) return value;
	if (value === undefined) return 'undefined';
	if (value === null) return 'null';
	if (typeof value === 'object' && value !== null) {
		return Object.entries(value)
			.map(([key, val]) => `${key}=${val}`)
			.join(';');
	}
	return value?.toString() ?? '';
}
 
/**
 * Append EXIF metadata to the image's bytes
 * @param {ArrayBuffer|Buffer} bytes
 * @param {import('./database.js').Metadata[]} metadataDefs
 * @param {import('./database.js').MetadataValues} metadataValues
 * @returns {Uint8Array} the image with EXIF metadata added
 */
export function addExifMetadata(bytes, metadataDefs, metadataValues) {
	const ExifMetadata = Schemas.Metadata.and({
		infer: [
			{ exif: 'string' },
			'|',
			{ latitude: { exif: 'string' }, longitude: { exif: 'string' } }
		]
	});
 
	const exifDict = { GPS: {}, Exif: {} };
	const setExifKey = (key, value) => {
		// @wc-ignore
		const category = Object.keys(EXIF_GPS_FIELDS).includes(key) ? 'GPS' : 'Exif';
		const serialized = serializeExifValue(value);
		Iif (serialized === undefined) return;
		exifDict[category][piexif[`${category}IFD`][key]] = serialized;
	};
 
	for (const def of metadataDefs.map((m) => ExifMetadata(m))) {
		if (def instanceof type.errors) continue;
		const value = metadataValues[def.id]?.value;
		Iif (value === undefined) continue;
 
		if (
			type({ latitude: { exif: 'string' }, longitude: { exif: 'string' } }).allows(
				def.infer
			) &&
			type({ latitude: 'number', longitude: 'number' }).allows(value)
		) {
			// XXX harcoded BS :/
			if (def.infer.latitude.exif === 'GPSLatitude') {
				setExifKey('GPSLatitudeRef', value.latitude >= 0 ? 'N' : 'S');
				setExifKey('GPSLatitude', piexif.GPSHelper.degToDmsRational(value.latitude));
			} else E{
				setExifKey(def.infer.latitude.exif, value.latitude);
			}
 
			// XXX harcoded BS :/
			if (def.infer.longitude.exif === 'GPSLongitude') {
				setExifKey('GPSLongitudeRef', value.longitude >= 0 ? 'E' : 'W');
				setExifKey('GPSLongitude', piexif.GPSHelper.degToDmsRational(value.longitude));
			} else E{
				setExifKey(def.infer.longitude.exif, value.longitude);
			}
		} else {
			setExifKey(def.infer.exif, value);
		}
	}
 
	// Piexif wants bytes _as a string_. why??? idk. but it seems like npm has no decent EXIF libraries that both support browsers and writing exif data.
	let bytesstr = '';
	// Build bytesstr in chunks, since String.fromCharCode is limited in characters size (see https://stackoverflow.com/q/76857530 and https://stackoverflow.com/a/22747272)
	let chunksize = 32_000;
	for (let i = 0; i < bytes.byteLength; i += chunksize) {
		const chunk = bytes.slice(i, i + chunksize);
		bytesstr += String.fromCharCode(...new Uint8Array(chunk));
	}
	const outputstr = piexif.insert(piexif.dump(exifDict), bytesstr);
	return new Uint8Array(Array.from(outputstr).map((c) => c.charCodeAt(0)));
}