chore(server) harden EXIF extraction (#1347)

* chore(server) Harden EXIF extraction

* Remove unused function in timeutil

* Remove deadcode
This commit is contained in:
Alex 2023-01-17 13:41:00 -06:00 committed by GitHub
parent dff10e89fe
commit 3e4a14b299
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 75 deletions

View file

@ -1,12 +1,4 @@
// This is needed as resolving for the vendored
// exiftool fails in tests otherwise but as it's not meant to be a requirement
// of a project directly I had to include the line below the comment.
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import { exiftool } from 'exiftool-vendored.pl';
function createTimeUtils() {
const floatRegex = /[+-]?([0-9]*[.])?[0-9]+/;
const checkValidTimestamp = (timestamp: string): boolean => {
const parsedTimestamp = Date.parse(timestamp);
@ -23,32 +15,7 @@ function createTimeUtils() {
return date.getFullYear() > 0;
};
const getTimestampFromExif = async (originalPath: string): Promise<string> => {
try {
const exifData = await exiftool.read(originalPath);
if (exifData && exifData['DateTimeOriginal']) {
await exiftool.end();
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return exifData['DateTimeOriginal'].toString()!;
} else {
return new Date().toISOString();
}
} catch (error) {
return new Date().toISOString();
}
};
const parseStringToNumber = async (original: string | undefined): Promise<number | null> => {
const match = original?.match(floatRegex)?.[0];
if (match) {
return parseFloat(match);
} else {
return null;
}
};
return { checkValidTimestamp, getTimestampFromExif, parseStringToNumber };
return { checkValidTimestamp };
}
export const timeUtils = createTimeUtils();