Skip to content

Commit

Permalink
fix(server): untracked thumbnail and preview images (#9126)
Browse files Browse the repository at this point in the history
* delete old thumbnails

* add tests

* revert invisible asset handling
  • Loading branch information
mertalev committed Apr 27, 2024
1 parent 19aa97d commit 2648032
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
22 changes: 22 additions & 0 deletions server/src/services/media.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,17 @@ describe(MediaService.name, () => {
expect(assetMock.update).toHaveBeenCalledWith({ id: 'asset-id', previewPath });
});

it('should delete previous preview if different path', async () => {
const previousPreviewPath = assetStub.image.previewPath;

configMock.load.mockResolvedValue([{ key: SystemConfigKey.IMAGE_THUMBNAIL_FORMAT, value: ImageFormat.WEBP }]);
assetMock.getByIds.mockResolvedValue([assetStub.image]);

await sut.handleGeneratePreview({ id: assetStub.image.id });

expect(storageMock.unlink).toHaveBeenCalledWith(previousPreviewPath);
});

it('should generate a P3 thumbnail for a wide gamut image', async () => {
assetMock.getByIds.mockResolvedValue([
{ ...assetStub.image, exifInfo: { profileDescription: 'Adobe RGB', bitsPerSample: 14 } as ExifEntity },
Expand Down Expand Up @@ -390,6 +401,17 @@ describe(MediaService.name, () => {
expect(assetMock.update).toHaveBeenCalledWith({ id: 'asset-id', thumbnailPath });
},
);

it('should delete previous thumbnail if different path', async () => {
const previousThumbnailPath = assetStub.image.thumbnailPath;

configMock.load.mockResolvedValue([{ key: SystemConfigKey.IMAGE_THUMBNAIL_FORMAT, value: ImageFormat.WEBP }]);
assetMock.getByIds.mockResolvedValue([assetStub.image]);

await sut.handleGenerateThumbnail({ id: assetStub.image.id });

expect(storageMock.unlink).toHaveBeenCalledWith(previousThumbnailPath);
});
});

it('should generate a P3 thumbnail for a wide gamut image', async () => {
Expand Down
8 changes: 8 additions & 0 deletions server/src/services/media.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ export class MediaService {
}

const previewPath = await this.generateThumbnail(asset, AssetPathType.PREVIEW, image.previewFormat);
if (asset.previewPath && asset.previewPath !== previewPath) {
this.logger.debug(`Deleting old preview for asset ${asset.id}`);
await this.storageRepository.unlink(asset.previewPath);
}
await this.assetRepository.update({ id: asset.id, previewPath });
return JobStatus.SUCCESS;
}
Expand Down Expand Up @@ -253,6 +257,10 @@ export class MediaService {
}

const thumbnailPath = await this.generateThumbnail(asset, AssetPathType.THUMBNAIL, image.thumbnailFormat);
if (asset.thumbnailPath && asset.thumbnailPath !== thumbnailPath) {
this.logger.debug(`Deleting old thumbnail for asset ${asset.id}`);
await this.storageRepository.unlink(asset.thumbnailPath);
}
await this.assetRepository.update({ id: asset.id, thumbnailPath });
return JobStatus.SUCCESS;
}
Expand Down
18 changes: 18 additions & 0 deletions server/test/fixtures/asset.stub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,24 @@ export const assetStub = {
},
} as AssetEntity),

liveMotionWithThumb: Object.freeze({
id: fileStub.livePhotoMotion.uuid,
originalPath: fileStub.livePhotoMotion.originalPath,
ownerId: authStub.user1.user.id,
type: AssetType.VIDEO,
isVisible: false,
fileModifiedAt: new Date('2022-06-19T23:41:36.910Z'),
fileCreatedAt: new Date('2022-06-19T23:41:36.910Z'),
libraryId: 'library-id',
library: libraryStub.uploadLibrary1,
previewPath: '/uploads/user-id/thumbs/path.ext',
thumbnailPath: '/uploads/user-id/webp/path.ext',
exifInfo: {
fileSizeInByte: 100_000,
timeZone: `America/New_York`,
},
} as AssetEntity),

livePhotoStillAsset: Object.freeze({
id: 'live-photo-still-asset',
originalPath: fileStub.livePhotoStill.originalPath,
Expand Down

0 comments on commit 2648032

Please sign in to comment.