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,
|
2021-11-10 09:23:23 +00:00
|
|
|
getFileFromURL,
|
2021-11-03 10:31:47 +00:00
|
|
|
updateExistingFilePubMetadata,
|
|
|
|
} from 'utils/file';
|
|
|
|
import { logError } from 'utils/sentry';
|
|
|
|
import downloadManager from './downloadManager';
|
2022-04-09 09:09:11 +00:00
|
|
|
import { updateFilePublicMagicMetadata } from './fileService';
|
2022-01-05 06:48:46 +00:00
|
|
|
import { EnteFile } from 'types/file';
|
2022-01-04 09:50:14 +00:00
|
|
|
|
2022-02-28 08:18:52 +00:00
|
|
|
import { getRawExif } from './upload/exifService';
|
2022-03-07 11:55:07 +00:00
|
|
|
import { getFileType } from 'services/typeDetectionService';
|
2022-01-05 06:48:46 +00:00
|
|
|
import { FILE_TYPE } from 'constants/file';
|
2022-02-28 12:14:23 +00:00
|
|
|
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 {
|
2021-11-17 08:21:21 +00:00
|
|
|
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) {
|
2022-02-28 12:14:23 +00:00
|
|
|
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);
|
2022-05-23 09:11:07 +00:00
|
|
|
const fileTypeInfo = await getFileType(fileObject);
|
2021-11-18 17:14:50 +00:00
|
|
|
const exifData = await getRawExif(fileObject, fileTypeInfo);
|
|
|
|
if (fixOption === FIX_OPTIONS.DATE_TIME_ORIGINAL) {
|
2022-02-28 12:14:23 +00:00
|
|
|
correctCreationTime = getUnixTimeInMicroSeconds(
|
2021-11-18 18:36:51 +00:00
|
|
|
exifData?.DateTimeOriginal
|
2021-11-18 17:14:50 +00:00
|
|
|
);
|
|
|
|
} else {
|
2022-02-28 12:14:23 +00:00
|
|
|
correctCreationTime = getUnixTimeInMicroSeconds(
|
|
|
|
exifData?.CreateDate
|
|
|
|
);
|
2021-11-18 17:14:50 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-10 08:17:21 +00:00
|
|
|
if (
|
2021-11-18 17:14:50 +00:00
|
|
|
correctCreationTime &&
|
|
|
|
correctCreationTime !== file.metadata.creationTime
|
2021-11-10 08:17:21 +00:00
|
|
|
) {
|
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 = (
|
2022-04-09 09:09:11 +00:00
|
|
|
await updateFilePublicMagicMetadata([updatedFile])
|
2021-11-03 10:31:47 +00:00
|
|
|
)[0];
|
|
|
|
updateExistingFilePubMetadata(file, updatedFile);
|
|
|
|
}
|
2021-11-17 07:46:29 +00:00
|
|
|
} catch (e) {
|
|
|
|
logError(e, 'failed to updated a CreationTime With Exif');
|
|
|
|
completedWithError = true;
|
|
|
|
} finally {
|
2021-11-03 10:31:47 +00:00
|
|
|
setProgressTracker({
|
2021-11-16 06:57:23 +00:00
|
|
|
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;
|
|
|
|
}
|