Skip to content
Closed
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
1 change: 1 addition & 0 deletions server/src/db.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export interface Assets {
isFavorite: Generated<boolean>;
isOffline: Generated<boolean>;
isVisible: Generated<boolean>;
isDirty: Generated<boolean>;
libraryId: string | null;
livePhotoVideoId: string | null;
localDateTime: Timestamp | null;
Expand Down
3 changes: 3 additions & 0 deletions server/src/entities/asset.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ export class AssetEntity {
@Column({ type: 'boolean', default: false })
isOffline!: boolean;

@Column({ type: 'boolean', default: false })
isDirty!: boolean;

@Column({ type: 'bytea' })
@Index()
checksum!: Buffer; // sha1 checksum
Expand Down
11 changes: 11 additions & 0 deletions server/src/migrations/1742127949957-AddAssetIsDirty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class AddAssetIsDirty1742127949957 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "assets" ADD "isDirty" boolean NOT NULL DEFAULT false`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "assets" DROP COLUMN "isDirty"`);
}
}
6 changes: 4 additions & 2 deletions server/src/services/asset.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ describe(AssetService.name, () => {

await sut.update(authStub.admin, 'asset-1', { isFavorite: true });

expect(mocks.asset.update).toHaveBeenCalledWith({ id: 'asset-1', isFavorite: true });
expect(mocks.asset.update).toHaveBeenCalledWith({ id: 'asset-1', isFavorite: true, isDirty: true });
});

it('should update the exif description', async () => {
Expand Down Expand Up @@ -371,6 +371,7 @@ describe(AssetService.name, () => {
expect(mocks.asset.update).toHaveBeenCalledWith({
id: assetStub.livePhotoStillAsset.id,
livePhotoVideoId: assetStub.livePhotoMotionAsset.id,
isDirty: true,
});
});

Expand All @@ -392,6 +393,7 @@ describe(AssetService.name, () => {
expect(mocks.asset.update).toHaveBeenCalledWith({
id: assetStub.livePhotoStillAsset.id,
livePhotoVideoId: null,
isDirty: true,
});
expect(mocks.asset.update).toHaveBeenCalledWith({ id: assetStub.livePhotoMotionAsset.id, isVisible: true });
expect(mocks.event.emit).toHaveBeenCalledWith('asset.show', {
Expand Down Expand Up @@ -429,7 +431,7 @@ describe(AssetService.name, () => {

await sut.updateAll(authStub.admin, { ids: ['asset-1', 'asset-2'], isArchived: true });

expect(mocks.asset.updateAll).toHaveBeenCalledWith(['asset-1', 'asset-2'], { isArchived: true });
expect(mocks.asset.updateAll).toHaveBeenCalledWith(['asset-1', 'asset-2'], { isArchived: true, isDirty: true });
});

it('should not update Assets table if no relevant fields are provided', async () => {
Expand Down
4 changes: 2 additions & 2 deletions server/src/services/asset.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export class AssetService extends BaseService {

await this.updateMetadata({ id, description, dateTimeOriginal, latitude, longitude, rating });

const asset = await this.assetRepository.update({ id, ...rest });
const asset = await this.assetRepository.update({ id, isDirty: true, ...rest });

if (previousMotion) {
await onAfterUnlink(repos, { userId: auth.user.id, livePhotoVideoId: previousMotion.id });
Expand All @@ -144,7 +144,7 @@ export class AssetService extends BaseService {
options.duplicateId != undefined ||
options.rating != undefined
) {
await this.assetRepository.updateAll(ids, options);
await this.assetRepository.updateAll(ids, { isDirty: true, ...options });
}
}

Expand Down
204 changes: 101 additions & 103 deletions server/src/services/metadata.service.spec.ts

Large diffs are not rendered by default.

27 changes: 21 additions & 6 deletions server/src/services/metadata.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { ReverseGeocodeResult } from 'src/repositories/map.repository';
import { ImmichTags } from 'src/repositories/metadata.repository';
import { BaseService } from 'src/services/base.service';
import { JobOf } from 'src/types';
import { getSidecarPath } from 'src/utils/asset.util';
import { isFaceImportEnabled } from 'src/utils/misc';
import { usePagination } from 'src/utils/pagination';
import { upsertTags } from 'src/utils/tag';
Expand Down Expand Up @@ -162,15 +163,29 @@ export class MetadataService extends BaseService {

@OnJob({ name: JobName.METADATA_EXTRACTION, queue: QueueName.METADATA_EXTRACTION })
async handleMetadataExtraction(data: JobOf<JobName.METADATA_EXTRACTION>): Promise<JobStatus> {
const [{ metadata, reverseGeocoding }, [asset]] = await Promise.all([
const [{ metadata, reverseGeocoding }, asset] = await Promise.all([
this.getConfig({ withCache: true }),
this.assetRepository.getByIds([data.id], { faces: { person: false } }),
this.assetRepository.getById(data.id, { faces: { person: false } }),
]);

if (!asset) {
return JobStatus.FAILED;
}

if (asset.isDirty) {
const { exifInfo } = (await this.assetRepository.getById(asset.id, { exifInfo: true })) || {};
await this.handleSidecarWrite({
id: asset.id,
description: exifInfo?.description,
dateTimeOriginal: exifInfo?.dateTimeOriginal?.toISOString(),
latitude: exifInfo?.latitude ?? undefined,
longitude: exifInfo?.longitude ?? undefined,
rating: exifInfo?.rating ?? undefined,
tags: true,
});
asset.sidecarPath = asset.sidecarPath || getSidecarPath(asset);
}

const exifTags = await this.getExifTags(asset);
if (!exifTags.FileCreateDate || !exifTags.FileModifyDate || exifTags.FileSize === undefined) {
this.logger.warn(`Missing file creation or modification date for asset ${asset.id}: ${asset.originalPath}`);
Expand Down Expand Up @@ -314,14 +329,14 @@ export class MetadataService extends BaseService {
@OnJob({ name: JobName.SIDECAR_WRITE, queue: QueueName.SIDECAR })
async handleSidecarWrite(job: JobOf<JobName.SIDECAR_WRITE>): Promise<JobStatus> {
const { id, description, dateTimeOriginal, latitude, longitude, rating, tags } = job;
const [asset] = await this.assetRepository.getByIds([id], { tags: true });
const asset = await this.assetRepository.getById(id, { tags: true });
if (!asset) {
return JobStatus.FAILED;
}

const tagsList = (asset.tags || []).map((tag) => tag.value);

const sidecarPath = asset.sidecarPath || `${asset.originalPath}.xmp`;
const sidecarPath = asset.sidecarPath || getSidecarPath(asset);
const exif = _.omitBy(
<Tags>{
Description: description,
Expand All @@ -342,7 +357,7 @@ export class MetadataService extends BaseService {
await this.metadataRepository.writeTags(sidecarPath, exif);

if (!asset.sidecarPath) {
await this.assetRepository.update({ id, sidecarPath });
await this.assetRepository.update({ id, sidecarPath, isDirty: false });
}

return JobStatus.SUCCESS;
Expand Down Expand Up @@ -754,7 +769,7 @@ export class MetadataService extends BaseService {
}

private async processSidecar(id: string, isSync: boolean): Promise<JobStatus> {
const [asset] = await this.assetRepository.getByIds([id]);
const asset = await this.assetRepository.getById(id);

if (!asset) {
return JobStatus.FAILED;
Expand Down
3 changes: 3 additions & 0 deletions server/src/services/tag.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export class TagService extends BaseService {

const results = await this.tagRepository.upsertAssetIds(items);
for (const assetId of new Set(results.map((item) => item.assetsId))) {
await this.assetRepository.update({ id: assetId, isDirty: true });
await this.eventRepository.emit('asset.tag', { assetId });
}

Expand All @@ -107,6 +108,7 @@ export class TagService extends BaseService {

for (const { id: assetId, success } of results) {
if (success) {
await this.assetRepository.update({ id: assetId, isDirty: true });
await this.eventRepository.emit('asset.tag', { assetId });
}
}
Expand All @@ -125,6 +127,7 @@ export class TagService extends BaseService {

for (const { id: assetId, success } of results) {
if (success) {
await this.assetRepository.update({ id: assetId, isDirty: true });
await this.eventRepository.emit('asset.untag', { assetId });
}
}
Expand Down
3 changes: 3 additions & 0 deletions server/src/utils/asset.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { BulkIdErrorReason, BulkIdResponseDto } from 'src/dtos/asset-ids.respons
import { UploadFieldName } from 'src/dtos/asset-media.dto';
import { AuthDto } from 'src/dtos/auth.dto';
import { AssetFileEntity } from 'src/entities/asset-files.entity';
import { AssetEntity } from 'src/entities/asset.entity';
import { AssetFileType, AssetType, Permission } from 'src/enum';
import { AuthRequest } from 'src/middleware/auth.guard';
import { AccessRepository } from 'src/repositories/access.repository';
Expand All @@ -17,6 +18,8 @@ const getFileByType = (files: AssetFileEntity[] | undefined, type: AssetFileType
return (files || []).find((file) => file.type === type);
};

export const getSidecarPath = (asset: AssetEntity) => `${asset.originalPath}.xmp`;

export const getAssetFiles = (files?: AssetFileEntity[]) => ({
previewFile: getFileByType(files, AssetFileType.PREVIEW),
thumbnailFile: getFileByType(files, AssetFileType.THUMBNAIL),
Expand Down
20 changes: 20 additions & 0 deletions server/test/fixtures/asset.stub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export const assetStub = {
isExternal: false,
duplicateId: null,
isOffline: false,
isDirty: false,
}),

noWebpPath: Object.freeze<AssetEntity>({
Expand Down Expand Up @@ -126,6 +127,7 @@ export const assetStub = {
deletedAt: null,
duplicateId: null,
isOffline: false,
isDirty: false,
}),

noThumbhash: Object.freeze<AssetEntity>({
Expand Down Expand Up @@ -161,6 +163,7 @@ export const assetStub = {
deletedAt: null,
duplicateId: null,
isOffline: false,
isDirty: false,
}),

primaryImage: Object.freeze<AssetEntity>({
Expand Down Expand Up @@ -207,6 +210,7 @@ export const assetStub = {
]),
duplicateId: null,
isOffline: false,
isDirty: false,
}),

image: Object.freeze<AssetEntity>({
Expand Down Expand Up @@ -247,6 +251,7 @@ export const assetStub = {
} as ExifEntity,
duplicateId: null,
isOffline: false,
isDirty: false,
}),

trashed: Object.freeze<AssetEntity>({
Expand Down Expand Up @@ -287,6 +292,7 @@ export const assetStub = {
duplicateId: null,
isOffline: false,
status: AssetStatus.TRASHED,
isDirty: false,
}),

trashedOffline: Object.freeze<AssetEntity>({
Expand Down Expand Up @@ -328,6 +334,7 @@ export const assetStub = {
} as ExifEntity,
duplicateId: null,
isOffline: true,
isDirty: false,
}),
archived: Object.freeze<AssetEntity>({
id: 'asset-id',
Expand Down Expand Up @@ -367,6 +374,7 @@ export const assetStub = {
} as ExifEntity,
duplicateId: null,
isOffline: false,
isDirty: false,
}),

external: Object.freeze<AssetEntity>({
Expand Down Expand Up @@ -406,6 +414,7 @@ export const assetStub = {
} as ExifEntity,
duplicateId: null,
isOffline: false,
isDirty: false,
}),

image1: Object.freeze<AssetEntity>({
Expand Down Expand Up @@ -444,6 +453,7 @@ export const assetStub = {
} as ExifEntity,
duplicateId: null,
isOffline: false,
isDirty: false,
}),

imageFrom2015: Object.freeze<AssetEntity>({
Expand Down Expand Up @@ -482,6 +492,7 @@ export const assetStub = {
deletedAt: null,
duplicateId: null,
isOffline: false,
isDirty: false,
}),

video: Object.freeze<AssetEntity>({
Expand Down Expand Up @@ -522,6 +533,7 @@ export const assetStub = {
deletedAt: null,
duplicateId: null,
isOffline: false,
isDirty: false,
}),

livePhotoMotionAsset: Object.freeze({
Expand Down Expand Up @@ -613,6 +625,7 @@ export const assetStub = {
deletedAt: null,
duplicateId: null,
isOffline: false,
isDirty: false,
}),

sidecar: Object.freeze<AssetEntity>({
Expand Down Expand Up @@ -648,6 +661,7 @@ export const assetStub = {
deletedAt: null,
duplicateId: null,
isOffline: false,
isDirty: false,
}),

sidecarWithoutExt: Object.freeze<AssetEntity>({
Expand Down Expand Up @@ -683,6 +697,7 @@ export const assetStub = {
deletedAt: null,
duplicateId: null,
isOffline: false,
isDirty: false,
}),

hasEncodedVideo: Object.freeze<AssetEntity>({
Expand Down Expand Up @@ -721,6 +736,7 @@ export const assetStub = {
deletedAt: null,
duplicateId: null,
isOffline: false,
isDirty: false,
}),

hasFileExtension: Object.freeze<AssetEntity>({
Expand Down Expand Up @@ -760,6 +776,7 @@ export const assetStub = {
} as ExifEntity,
duplicateId: null,
isOffline: false,
isDirty: false,
}),

imageDng: Object.freeze<AssetEntity>({
Expand Down Expand Up @@ -800,6 +817,7 @@ export const assetStub = {
} as ExifEntity,
duplicateId: null,
isOffline: false,
isDirty: false,
}),

hasEmbedding: Object.freeze<AssetEntity>({
Expand Down Expand Up @@ -842,6 +860,7 @@ export const assetStub = {
embedding: '[1, 2, 3, 4]',
},
isOffline: false,
isDirty: false,
}),

hasDupe: Object.freeze<AssetEntity>({
Expand Down Expand Up @@ -884,5 +903,6 @@ export const assetStub = {
embedding: '[1, 2, 3, 4]',
},
isOffline: false,
isDirty: false,
}),
};
1 change: 1 addition & 0 deletions server/test/fixtures/shared-link.stub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ export const sharedLinkStub = {
sidecarPath: null,
deletedAt: null,
duplicateId: null,
isDirty: false,
},
],
},
Expand Down
2 changes: 1 addition & 1 deletion server/test/medium/specs/metadata.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ describe(MetadataService.name, () => {
process.env.TZ = serverTimeZone ?? undefined;

const { filePath } = await createTestFile(exifData);
mocks.asset.getByIds.mockResolvedValue([{ id: 'asset-1', originalPath: filePath } as AssetEntity]);
mocks.asset.getById.mockResolvedValue({ id: 'asset-1', originalPath: filePath } as AssetEntity);

await sut.handleMetadataExtraction({ id: 'asset-1' });

Expand Down
1 change: 1 addition & 0 deletions server/test/small.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ const assetFactory = (asset: Partial<Asset> = {}) => ({
isFavorite: false,
isOffline: false,
isVisible: true,
isDirty: false,
libraryId: null,
livePhotoVideoId: null,
localDateTime: newDate(),
Expand Down
Loading