Skip to content
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

refactor(server): use exiftool for file date metadata #16453

Open
wants to merge 4 commits into
base: refactor/server-metadata-extraction-group-async-calls
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion server/src/repositories/metadata.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class MetadataRepository {
inferTimezoneFromDatestamps: true,
inferTimezoneFromTimeStamp: true,
useMWG: true,
numericTags: [...DefaultReadTaskOptions.numericTags, 'FocalLength'],
numericTags: [...DefaultReadTaskOptions.numericTags, 'FocalLength', 'FileSize'],
/* eslint unicorn/no-array-callback-reference: off, unicorn/no-array-method-this-argument: off */
geoTz: (lat, lon) => geotz.find(lat, lon)[0],
// Enable exiftool LFS to parse metadata for files larger than 2GB.
Expand Down
79 changes: 53 additions & 26 deletions server/src/services/metadata.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { BinaryField, ExifDateTime } from 'exiftool-vendored';
import { randomBytes } from 'node:crypto';
import { Stats } from 'node:fs';
import { constants } from 'node:fs/promises';
import { defaults } from 'src/config';
import { AssetEntity } from 'src/entities/asset.entity';
Expand All @@ -22,8 +21,14 @@ describe(MetadataService.name, () => {
let mocks: ServiceMocks;

const mockReadTags = (exifData?: Partial<ImmichTags>, sidecarData?: Partial<ImmichTags>) => {
exifData = {
FileSize: '123456',
FileCreateDate: '2024-01-01T00:00:00.000Z',
FileModifyDate: '2024-01-01T00:00:00.000Z',
...exifData,
};
mocks.metadata.readTags.mockReset();
mocks.metadata.readTags.mockResolvedValueOnce(exifData ?? {});
mocks.metadata.readTags.mockResolvedValueOnce(exifData);
mocks.metadata.readTags.mockResolvedValueOnce(sidecarData ?? {});
};

Expand Down Expand Up @@ -105,10 +110,6 @@ describe(MetadataService.name, () => {
});

describe('handleMetadataExtraction', () => {
beforeEach(() => {
mocks.storage.stat.mockResolvedValue({ size: 123_456 } as Stats);
});

it('should handle an asset that could not be found', async () => {
await expect(sut.handleMetadataExtraction({ id: assetStub.image.id })).resolves.toBe(JobStatus.FAILED);

Expand All @@ -126,19 +127,24 @@ describe(MetadataService.name, () => {
await sut.handleMetadataExtraction({ id: assetStub.image.id });
expect(mocks.asset.getByIds).toHaveBeenCalledWith([assetStub.sidecar.id], { faces: { person: false } });
expect(mocks.asset.upsertExif).toHaveBeenCalledWith(expect.objectContaining({ dateTimeOriginal: sidecarDate }));
expect(mocks.asset.update).toHaveBeenCalledWith({
id: assetStub.image.id,
duration: null,
fileCreatedAt: sidecarDate,
localDateTime: sidecarDate,
});
expect(mocks.asset.update).toHaveBeenCalledWith(
expect.objectContaining({
id: assetStub.image.id,
duration: null,
fileCreatedAt: sidecarDate,
localDateTime: sidecarDate,
}),
);
});

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

await sut.handleMetadataExtraction({ id: assetStub.image.id });
expect(mocks.asset.getByIds).toHaveBeenCalledWith([assetStub.image.id], { faces: { person: false } });
Expand All @@ -149,15 +155,19 @@ describe(MetadataService.name, () => {
id: assetStub.image.id,
duration: null,
fileCreatedAt: fileModifiedAt,
fileModifiedAt,
localDateTime: fileModifiedAt,
});
});

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

await sut.handleMetadataExtraction({ id: assetStub.image.id });
expect(mocks.asset.getByIds).toHaveBeenCalledWith([assetStub.image.id], { faces: { person: false } });
Expand All @@ -166,6 +176,7 @@ describe(MetadataService.name, () => {
id: assetStub.image.id,
duration: null,
fileCreatedAt,
fileModifiedAt,
localDateTime: fileCreatedAt,
});
});
Expand All @@ -191,7 +202,11 @@ describe(MetadataService.name, () => {

it('should handle lists of numbers', async () => {
mocks.asset.getByIds.mockResolvedValue([assetStub.image]);
mockReadTags({ ISO: [160] });
mockReadTags({
ISO: [160],
FileCreateDate: assetStub.image.fileCreatedAt.toISOString(),
FileModifyDate: assetStub.image.fileModifiedAt.toISOString(),
});

await sut.handleMetadataExtraction({ id: assetStub.image.id });
expect(mocks.asset.getByIds).toHaveBeenCalledWith([assetStub.image.id], { faces: { person: false } });
Expand All @@ -200,6 +215,7 @@ describe(MetadataService.name, () => {
id: assetStub.image.id,
duration: null,
fileCreatedAt: assetStub.image.fileCreatedAt,
fileModifiedAt: assetStub.image.fileCreatedAt,
localDateTime: assetStub.image.fileCreatedAt,
});
});
Expand All @@ -211,6 +227,8 @@ describe(MetadataService.name, () => {
mockReadTags({
GPSLatitude: assetStub.withLocation.exifInfo!.latitude!,
GPSLongitude: assetStub.withLocation.exifInfo!.longitude!,
FileCreateDate: assetStub.withLocation.fileCreatedAt.toISOString(),
FileModifyDate: assetStub.withLocation.fileModifiedAt.toISOString(),
});

await sut.handleMetadataExtraction({ id: assetStub.image.id });
Expand All @@ -221,7 +239,8 @@ describe(MetadataService.name, () => {
expect(mocks.asset.update).toHaveBeenCalledWith({
id: assetStub.withLocation.id,
duration: null,
fileCreatedAt: assetStub.withLocation.createdAt,
fileCreatedAt: assetStub.withLocation.fileCreatedAt,
fileModifiedAt: assetStub.withLocation.fileModifiedAt,
localDateTime: new Date('2023-02-22T05:06:29.716Z'),
});
});
Expand Down Expand Up @@ -460,6 +479,8 @@ describe(MetadataService.name, () => {
// instead of the EmbeddedVideoFile, since HEIC MotionPhotos include both
EmbeddedVideoFile: new BinaryField(0, ''),
EmbeddedVideoType: 'MotionPhoto_Data',
FileCreateDate: assetStub.livePhotoWithOriginalFileName.fileCreatedAt.toISOString(),
FileModifyDate: assetStub.livePhotoWithOriginalFileName.fileModifiedAt.toISOString(),
});
mocks.crypto.hashSha1.mockReturnValue(randomBytes(512));
mocks.asset.create.mockResolvedValue(assetStub.livePhotoMotionAsset);
Expand Down Expand Up @@ -506,6 +527,8 @@ describe(MetadataService.name, () => {
EmbeddedVideoFile: new BinaryField(0, ''),
EmbeddedVideoType: 'MotionPhoto_Data',
MotionPhoto: 1,
FileCreateDate: assetStub.livePhotoWithOriginalFileName.fileCreatedAt.toISOString(),
FileModifyDate: assetStub.livePhotoWithOriginalFileName.fileModifiedAt.toISOString(),
});
mocks.crypto.hashSha1.mockReturnValue(randomBytes(512));
mocks.asset.create.mockResolvedValue(assetStub.livePhotoMotionAsset);
Expand Down Expand Up @@ -552,6 +575,8 @@ describe(MetadataService.name, () => {
MotionPhoto: 1,
MicroVideo: 1,
MicroVideoOffset: 1,
FileCreateDate: assetStub.livePhotoWithOriginalFileName.fileCreatedAt.toISOString(),
FileModifyDate: assetStub.livePhotoWithOriginalFileName.fileModifiedAt.toISOString(),
});
mocks.crypto.hashSha1.mockReturnValue(randomBytes(512));
mocks.asset.create.mockResolvedValue(assetStub.livePhotoMotionAsset);
Expand Down Expand Up @@ -745,12 +770,14 @@ describe(MetadataService.name, () => {
state: null,
city: null,
});
expect(mocks.asset.update).toHaveBeenCalledWith({
id: assetStub.image.id,
duration: null,
fileCreatedAt: dateForTest,
localDateTime: dateForTest,
});
expect(mocks.asset.update).toHaveBeenCalledWith(
expect.objectContaining({
id: assetStub.image.id,
duration: null,
fileCreatedAt: dateForTest,
localDateTime: dateForTest,
}),
);
});

it('should extract +00:00 timezone from raw value', async () => {
Expand Down
44 changes: 19 additions & 25 deletions server/src/services/metadata.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,20 +171,14 @@ export class MetadataService extends BaseService {
return JobStatus.FAILED;
}

const [stats, exifTags] = await Promise.all([
this.storageRepository.stat(asset.originalPath),
this.getExifTags(asset),
]);

this.logger.verbose('Exif Tags', exifTags);

if (!asset.fileCreatedAt) {
asset.fileCreatedAt = stats.mtime;
const exifTags = await this.getExifTags(asset);
if (!exifTags.FileCreateDate || !exifTags.FileModifyDate) {
const stat = await this.storageRepository.stat(asset.originalPath);
exifTags.FileCreateDate = stat.ctime.toISOString();
exifTags.FileModifyDate = stat.mtime.toISOString();
}

if (!asset.fileModifiedAt) {
asset.fileModifiedAt = stats.mtime;
}
this.logger.verbose('Exif Tags', exifTags);

const { dateTimeOriginal, localDateTime, timeZone, modifyDate } = this.getDates(asset, exifTags);

Expand Down Expand Up @@ -216,7 +210,7 @@ export class MetadataService extends BaseService {
city: geo.city,

// image/file
fileSizeInByte: stats.size,
fileSizeInByte: Number.parseInt(exifTags.FileSize!),
exifImageHeight: validate(height),
exifImageWidth: validate(width),
orientation: validate(exifTags.Orientation)?.toString() ?? null,
Expand Down Expand Up @@ -251,13 +245,13 @@ export class MetadataService extends BaseService {
duration: exifTags.Duration?.toString() ?? null,
localDateTime,
fileCreatedAt: exifData.dateTimeOriginal ?? undefined,
fileModifiedAt: stats.mtime,
fileModifiedAt: exifData.modifyDate ?? undefined,
}),
this.applyTagList(asset, exifTags),
];

if (this.isMotionPhoto(asset, exifTags)) {
promises.push(this.applyMotionPhotos(asset, exifTags));
promises.push(this.applyMotionPhotos(asset, exifTags, exifData.fileSizeInByte!));
}

if (isFaceImportEnabled(metadata) && this.hasTaggedFaces(exifTags)) {
Expand Down Expand Up @@ -436,7 +430,7 @@ export class MetadataService extends BaseService {
return asset.type === AssetType.IMAGE && !!(tags.MotionPhoto || tags.MicroVideo);
}

private async applyMotionPhotos(asset: AssetEntity, tags: ImmichTags) {
private async applyMotionPhotos(asset: AssetEntity, tags: ImmichTags, fileSize: number) {
const isMotionPhoto = tags.MotionPhoto;
const isMicroVideo = tags.MicroVideo;
const videoOffset = tags.MicroVideoOffset;
Expand Down Expand Up @@ -470,8 +464,7 @@ export class MetadataService extends BaseService {
this.logger.debug(`Starting motion photo video extraction for asset ${asset.id}: ${asset.originalPath}`);

try {
const stat = await this.storageRepository.stat(asset.originalPath);
const position = stat.size - length - padding;
const position = fileSize - length - padding;
let video: Buffer;
// Samsung MotionPhoto video extraction
// HEIC-encoded
Expand Down Expand Up @@ -659,13 +652,15 @@ export class MetadataService extends BaseService {
this.logger.debug(`No timezone information found for asset ${asset.id}: ${asset.originalPath}`);
}

const modifyDate = this.toDate(exifTags.FileModifyDate!);
let dateTimeOriginal = dateTime?.toDate();
let localDateTime = dateTime?.toDateTime().setZone('UTC', { keepLocalTime: true }).toJSDate();
if (!localDateTime || !dateTimeOriginal) {
this.logger.debug(
`No exif date time found, falling back on earliest of file creation and modification for assset ${asset.id}: ${asset.originalPath}`,
`No exif date time found, falling back on earliest of file creation and modification for asset ${asset.id}: ${asset.originalPath}`,
);
const earliestDate = this.earliestDate(asset.fileModifiedAt, asset.fileCreatedAt);
const fileCreatedAt = this.toDate(exifTags.FileCreateDate!);
const earliestDate = this.earliestDate(fileCreatedAt, modifyDate);
dateTimeOriginal = earliestDate;
localDateTime = earliestDate;
}
Expand All @@ -674,11 +669,6 @@ export class MetadataService extends BaseService {
`Found local date time ${localDateTime.toISOString()} for asset ${asset.id}: ${asset.originalPath}`,
);

let modifyDate = asset.fileModifiedAt;
try {
modifyDate = (exifTags.ModifyDate as ExifDateTime)?.toDate() ?? modifyDate;
} catch {}

return {
dateTimeOriginal,
timeZone,
Expand All @@ -687,6 +677,10 @@ export class MetadataService extends BaseService {
};
}

private toDate(date: string | ExifDateTime): Date {
return typeof date === 'string' ? new Date(date) : date.toDate();
}

private earliestDate(a: Date, b: Date) {
return new Date(Math.min(a.valueOf(), b.valueOf()));
}
Expand Down
12 changes: 11 additions & 1 deletion server/test/medium/specs/metadata.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe(MetadataService.name, () => {
beforeEach(() => {
({ sut, mocks } = newTestService(MetadataService, { metadataRepository }));

mocks.storage.stat.mockResolvedValue({ size: 123_456 } as Stats);
mocks.storage.stat.mockResolvedValue({ size: 123_456, ctime: new Date(), mtime: new Date() } as Stats);

delete process.env.TZ;
});
Expand All @@ -51,6 +51,8 @@ describe(MetadataService.name, () => {
description: 'should handle no time zone information',
exifData: {
DateTimeOriginal: '2022:01:01 00:00:00',
FileCreateDate: '2022:01:01 00:00:00',
FileModifyDate: '2022:01:01 00:00:00',
},
expected: {
localDateTime: '2022-01-01T00:00:00.000Z',
Expand All @@ -63,6 +65,8 @@ describe(MetadataService.name, () => {
serverTimeZone: 'America/Los_Angeles',
exifData: {
DateTimeOriginal: '2022:01:01 00:00:00',
FileCreateDate: '2022:01:01 00:00:00',
FileModifyDate: '2022:01:01 00:00:00',
},
expected: {
localDateTime: '2022-01-01T00:00:00.000Z',
Expand All @@ -75,6 +79,8 @@ describe(MetadataService.name, () => {
serverTimeZone: 'Europe/Brussels',
exifData: {
DateTimeOriginal: '2022:01:01 00:00:00',
FileCreateDate: '2022:01:01 00:00:00',
FileModifyDate: '2022:01:01 00:00:00',
},
expected: {
localDateTime: '2022-01-01T00:00:00.000Z',
Expand All @@ -87,6 +93,8 @@ describe(MetadataService.name, () => {
serverTimeZone: 'Europe/Brussels',
exifData: {
DateTimeOriginal: '2022:06:01 00:00:00',
FileCreateDate: '2022:06:01 00:00:00',
FileModifyDate: '2022:06:01 00:00:00',
},
expected: {
localDateTime: '2022-06-01T00:00:00.000Z',
Expand All @@ -98,6 +106,8 @@ describe(MetadataService.name, () => {
description: 'should handle a +13:00 time zone',
exifData: {
DateTimeOriginal: '2022:01:01 00:00:00+13:00',
FileCreateDate: '2022:01:01 00:00:00+13:00',
FileModifyDate: '2022:01:01 00:00:00+13:00',
},
expected: {
localDateTime: '2022-01-01T00:00:00.000Z',
Expand Down
Loading