ente/src/services/updateCreationTimeWithExif.ts

83 lines
3.2 KiB
TypeScript
Raw Normal View History

2021-11-18 17:14:50 +00:00
import { FIX_OPTIONS } from 'components/FixCreationTime';
2021-11-03 10:31:47 +00:00
import { SetProgressTracker } from 'components/FixLargeThumbnail';
import {
changeFileCreationTime,
getFileFromURL,
2021-11-03 10:31:47 +00:00
updateExistingFilePubMetadata,
} from 'utils/file';
import { logError } from 'utils/sentry';
import downloadManager from './downloadManager';
2022-01-04 09:50:14 +00:00
import { updatePublicMagicMetadata } from './fileService';
2022-01-05 06:48:46 +00:00
import { EnteFile } from 'types/file';
2022-01-04 09:50:14 +00:00
import { getRawExif } from './upload/exifService';
import { getFileType } from 'services/typeDetectionService';
2022-01-05 06:48:46 +00:00
import { FILE_TYPE } from 'constants/file';
import { getUnixTimeInMicroSeconds } from 'utils/time';
2021-11-03 10:31:47 +00:00
export async function updateCreationTimeWithExif(
2022-01-04 09:50:14 +00:00
filesToBeUpdated: EnteFile[],
2021-11-18 17:14:50 +00:00
fixOption: FIX_OPTIONS,
customTime: Date,
2021-11-03 10:31:47 +00:00
setProgressTracker: SetProgressTracker
) {
let completedWithError = false;
try {
if (filesToBeUpdated.length === 0) {
return completedWithError;
}
setProgressTracker({ current: 0, total: filesToBeUpdated.length });
for (const [index, file] of filesToBeUpdated.entries()) {
try {
if (file.metadata.fileType !== FILE_TYPE.IMAGE) {
continue;
}
2021-11-18 17:14:50 +00:00
let correctCreationTime: number;
if (fixOption === FIX_OPTIONS.CUSTOM_TIME) {
correctCreationTime = getUnixTimeInMicroSeconds(customTime);
2021-11-18 17:14:50 +00:00
} else {
2022-03-01 12:44:19 +00:00
const fileURL = await downloadManager.getFile(file)[0];
2021-11-18 17:14:50 +00:00
const fileObject = await getFileFromURL(fileURL);
const reader = new FileReader();
const fileTypeInfo = await getFileType(reader, fileObject);
2021-11-18 17:14:50 +00:00
const exifData = await getRawExif(fileObject, fileTypeInfo);
if (fixOption === FIX_OPTIONS.DATE_TIME_ORIGINAL) {
correctCreationTime = getUnixTimeInMicroSeconds(
2021-11-18 18:36:51 +00:00
exifData?.DateTimeOriginal
2021-11-18 17:14:50 +00:00
);
} else {
correctCreationTime = getUnixTimeInMicroSeconds(
exifData?.CreateDate
);
2021-11-18 17:14:50 +00:00
}
}
if (
2021-11-18 17:14:50 +00:00
correctCreationTime &&
correctCreationTime !== file.metadata.creationTime
) {
2021-11-03 10:31:47 +00:00
let updatedFile = await changeFileCreationTime(
file,
2021-11-18 17:14:50 +00:00
correctCreationTime
2021-11-03 10:31:47 +00:00
);
updatedFile = (
await updatePublicMagicMetadata([updatedFile])
)[0];
updateExistingFilePubMetadata(file, updatedFile);
}
} catch (e) {
logError(e, 'failed to updated a CreationTime With Exif');
completedWithError = true;
} finally {
2021-11-03 10:31:47 +00:00
setProgressTracker({
current: index + 1,
2021-11-03 10:31:47 +00:00
total: filesToBeUpdated.length,
});
}
}
} catch (e) {
logError(e, 'update CreationTime With Exif failed');
completedWithError = true;
}
return completedWithError;
}