Skip to content

feat(server): use the earliest date between file creation and modification timestamps when missing exif tags #14874

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jan 2, 2025
34 changes: 34 additions & 0 deletions server/src/services/metadata.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,40 @@ describe(MetadataService.name, () => {
});
});

it('should take the file modification date when missing exif and earliest than creation date', async () => {
const fileCreatedAt = new Date('2022-01-01T00:00:00.000Z');
const fileModifiedAt = new Date('2021-01-01T00:00:00.000Z');
assetMock.getByIds.mockResolvedValue([{ ...assetStub.image, fileCreatedAt, fileModifiedAt }]);
mockReadTags();

await sut.handleMetadataExtraction({ id: assetStub.image.id });
expect(assetMock.getByIds).toHaveBeenCalledWith([assetStub.image.id], { faces: { person: false } });
expect(assetMock.upsertExif).toHaveBeenCalledWith(expect.objectContaining({ dateTimeOriginal: fileModifiedAt }));
expect(assetMock.update).toHaveBeenCalledWith({
id: assetStub.image.id,
duration: null,
fileCreatedAt: fileModifiedAt,
localDateTime: fileModifiedAt,
});
});

it('should take the file creation date when missing exif and earliest than modification date', async () => {
const fileCreatedAt = new Date('2021-01-01T00:00:00.000Z');
const fileModifiedAt = new Date('2022-01-01T00:00:00.000Z');
assetMock.getByIds.mockResolvedValue([{ ...assetStub.image, fileCreatedAt, fileModifiedAt }]);
mockReadTags();

await sut.handleMetadataExtraction({ id: assetStub.image.id });
expect(assetMock.getByIds).toHaveBeenCalledWith([assetStub.image.id], { faces: { person: false } });
expect(assetMock.upsertExif).toHaveBeenCalledWith(expect.objectContaining({ dateTimeOriginal: fileCreatedAt }));
expect(assetMock.update).toHaveBeenCalledWith({
id: assetStub.image.id,
duration: null,
fileCreatedAt,
localDateTime: fileCreatedAt,
});
});

it('should account for the server being in a non-UTC timezone', async () => {
process.env.TZ = 'America/Los_Angeles';
assetMock.getByIds.mockResolvedValue([assetStub.sidecar]);
Expand Down
21 changes: 14 additions & 7 deletions server/src/services/metadata.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,14 +450,14 @@ export class MetadataService extends BaseService {
}
} else {
const motionAssetId = this.cryptoRepository.randomUUID();
const createdAt = asset.fileCreatedAt ?? asset.createdAt;
const dates = this.getDates(asset, tags);
motionAsset = await this.assetRepository.create({
id: motionAssetId,
libraryId: asset.libraryId,
type: AssetType.VIDEO,
fileCreatedAt: createdAt,
fileModifiedAt: asset.fileModifiedAt,
localDateTime: createdAt,
fileCreatedAt: dates.dateTimeOriginal,
fileModifiedAt: dates.modifyDate,
localDateTime: dates.localDateTime,
checksum,
ownerId: asset.ownerId,
originalPath: StorageCore.getAndroidMotionPath(asset, motionAssetId),
Expand Down Expand Up @@ -589,9 +589,12 @@ export class MetadataService extends BaseService {
let dateTimeOriginal = dateTime?.toDate();
let localDateTime = dateTime?.toDateTime().setZone('UTC', { keepLocalTime: true }).toJSDate();
if (!localDateTime || !dateTimeOriginal) {
this.logger.warn(`Asset ${asset.id} has no valid date, falling back to asset.fileCreatedAt`);
dateTimeOriginal = asset.fileCreatedAt;
localDateTime = asset.fileCreatedAt;
this.logger.debug(
`No valid date found in exif tags from asset ${asset.id}, falling back to earliest timestamp between file creation and file modification`,
);
const earliestDate = this.earliestDate(asset.fileModifiedAt, asset.fileCreatedAt);
dateTimeOriginal = earliestDate;
localDateTime = earliestDate;
}

this.logger.verbose(`Asset ${asset.id} has a local time of ${localDateTime.toISOString()}`);
Expand All @@ -609,6 +612,10 @@ export class MetadataService extends BaseService {
};
}

private earliestDate(a: Date, b: Date) {
return new Date(Math.min(a.valueOf(), b.valueOf()));
}

private async getGeo(tags: ImmichTags, reverseGeocoding: SystemConfig['reverseGeocoding']) {
let latitude = validate(tags.GPSLatitude);
let longitude = validate(tags.GPSLongitude);
Expand Down
Loading