Skip to content

Commit 2a466b6

Browse files
committed
Remove old motion photo video assets when a new one is extracted
1 parent 8769506 commit 2a466b6

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

Diff for: server/src/domain/metadata/metadata.service.ts

+25-4
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ export class MetadataService {
354354
}
355355

356356
private async applyMotionPhotos(asset: AssetEntity, tags: ImmichTags) {
357-
if (asset.type !== AssetType.IMAGE || asset.livePhotoVideoId) {
357+
if (asset.type !== AssetType.IMAGE) {
358358
return;
359359
}
360360

@@ -381,7 +381,11 @@ export class MetadataService {
381381
length = videoOffset;
382382
}
383383

384-
if (!length) {
384+
if (
385+
length === 0 &&
386+
tags.MotionPhotoVideo === undefined &&
387+
(tags.EmbeddedVideoFile === undefined || tags.EmbeddedVideoType !== 'MotionPhoto_Data')
388+
) {
385389
return;
386390
}
387391

@@ -392,11 +396,11 @@ export class MetadataService {
392396
const position = stat.size - length - padding;
393397
let video: Buffer;
394398
// Samsung MotionPhoto video extraction
395-
// HEIC-encoded
399+
// HEIC-encoded
396400
if (tags.MotionPhotoVideo) {
397401
video = await this.repository.extractBinaryTag(asset.originalPath, 'MotionPhotoVideo');
398402
}
399-
// JPEG-encoded; HEIC also contains these tags, so this conditional must come second
403+
// JPEG-encoded; HEIC also contains these tags, so this conditional must come second
400404
else if (tags.EmbeddedVideoType === 'MotionPhoto_Data' && tags.EmbeddedVideoFile) {
401405
video = await this.repository.extractBinaryTag(asset.originalPath, 'EmbeddedVideoFile');
402406
}
@@ -436,6 +440,23 @@ export class MetadataService {
436440
await this.jobRepository.queue({ name: JobName.METADATA_EXTRACTION, data: { id: motionAsset.id } });
437441
}
438442

443+
if (asset.livePhotoVideoId) {
444+
const old_asset = await this.assetRepository.getById(asset.livePhotoVideoId);
445+
if (old_asset) {
446+
try {
447+
await this.assetRepository.remove(old_asset);
448+
await this.storageRepository.unlink(old_asset.originalPath);
449+
} catch (error: Error | any) {
450+
this.logger.error(`Failed to remove old asset ${old_asset.originalPath}: ${error}`, error?.stack);
451+
}
452+
this.logger.log(`Removed old motion photo video asset (${old_asset.id})`);
453+
} else {
454+
this.logger.warn(
455+
`Tried to remove old motion photo video but asset did not exist (${asset.livePhotoVideoId})`,
456+
);
457+
}
458+
}
459+
439460
await this.assetRepository.save({ id: asset.id, livePhotoVideoId: motionAsset.id });
440461

441462
this.logger.debug(`Finished motion photo video extraction (${asset.id})`);

0 commit comments

Comments
 (0)