From a7bcdf5243ef3d64c64c771c96130611e571d97e Mon Sep 17 00:00:00 2001 From: Marcin Zelent Date: Wed, 21 Dec 2022 23:39:38 +0100 Subject: Fixed broken special characters in captions --- src/generate-data.ts | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'src/generate-data.ts') diff --git a/src/generate-data.ts b/src/generate-data.ts index a67c620..3d04bfb 100644 --- a/src/generate-data.ts +++ b/src/generate-data.ts @@ -93,6 +93,26 @@ function convertCoordinate(coordinate: number[], coordinateRef: string): number } } +function decodeExifUserComment(buffer: Buffer): string { + const encoding = buffer.toString('ascii', 0, 7); + + let index = 8; + for (let i = index; i < buffer.length; i++) { + if (buffer[i] !== 0) { + index = i; + break; + } + } + + if (encoding === 'ASCII\0\0') { + return buffer.toString('utf8', index); + } else if (encoding === 'UNICODE') { + return buffer.toString('utf16le', index); + } else { + return buffer.toString(); + } +} + async function checkIfFileExists(filepath: string): Promise { try { await access(filepath, fsConstants.F_OK); @@ -232,7 +252,7 @@ async function getPhotos( longitude, time: exifData.exif?.DateTimeOriginal, thumbnail: `${serverPath}/${relativePath}/thumb-${file.name}`, - caption: exifData.image?.ImageDescription, + caption: decodeExifUserComment(exifData.exif?.UserComment), }); } catch (err) { console.error(`Could not process the file ${inputFile}.`); -- cgit v1.2.3