2023-01-17 17:29:49 +02:00
|
|
|
// 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';
|
2022-09-12 23:35:44 -05:00
|
|
|
|
|
|
|
|
function createTimeUtils() {
|
2023-01-17 17:29:49 +02:00
|
|
|
const floatRegex = /[+-]?([0-9]*[.])?[0-9]+/;
|
2022-09-12 23:35:44 -05:00
|
|
|
const checkValidTimestamp = (timestamp: string): boolean => {
|
|
|
|
|
const parsedTimestamp = Date.parse(timestamp);
|
|
|
|
|
|
|
|
|
|
if (isNaN(parsedTimestamp)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const date = new Date(parsedTimestamp);
|
|
|
|
|
|
|
|
|
|
if (date.getFullYear() < 1583 || date.getFullYear() > 9999) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return date.getFullYear() > 0;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getTimestampFromExif = async (originalPath: string): Promise<string> => {
|
|
|
|
|
try {
|
2023-01-17 17:29:49 +02:00
|
|
|
const exifData = await exiftool.read(originalPath);
|
2022-09-12 23:35:44 -05:00
|
|
|
|
|
|
|
|
if (exifData && exifData['DateTimeOriginal']) {
|
2023-01-17 17:29:49 +02:00
|
|
|
await exiftool.end();
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
|
|
|
return exifData['DateTimeOriginal'].toString()!;
|
2022-09-12 23:35:44 -05:00
|
|
|
} else {
|
|
|
|
|
return new Date().toISOString();
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return new Date().toISOString();
|
|
|
|
|
}
|
|
|
|
|
};
|
2023-01-17 17:29:49 +02:00
|
|
|
|
|
|
|
|
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 };
|
2022-09-12 23:35:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const timeUtils = createTimeUtils();
|