From fe65c44cbc592c5d07a1d1101a8ee3ac16aa1371 Mon Sep 17 00:00:00 2001 From: Eli Gao Date: Wed, 25 Dec 2024 04:49:56 +0800 Subject: [PATCH] fix(server): delete fullsize preview image on thumbnail regen after fullsize preview turned off --- server/src/interfaces/asset.interface.ts | 1 + server/src/repositories/asset.repository.ts | 5 +++++ server/src/services/media.service.ts | 4 ++++ server/test/repositories/asset.repository.mock.ts | 1 + 4 files changed, 11 insertions(+) diff --git a/server/src/interfaces/asset.interface.ts b/server/src/interfaces/asset.interface.ts index b25e42ba0e..4585631284 100644 --- a/server/src/interfaces/asset.interface.ts +++ b/server/src/interfaces/asset.interface.ts @@ -197,4 +197,5 @@ export interface IAssetRepository { getChangedDeltaSync(options: AssetDeltaSyncOptions): Promise; upsertFile(file: UpsertFileOptions): Promise; upsertFiles(files: UpsertFileOptions[]): Promise; + deleteFiles(assetIds: string | string[]): Promise; } diff --git a/server/src/repositories/asset.repository.ts b/server/src/repositories/asset.repository.ts index 33d1e2457e..b62df207a2 100644 --- a/server/src/repositories/asset.repository.ts +++ b/server/src/repositories/asset.repository.ts @@ -726,4 +726,9 @@ export class AssetRepository implements IAssetRepository { async upsertFiles(files: { assetId: string; type: AssetFileType; path: string }[]): Promise { await this.fileRepository.upsert(files, { conflictPaths: ['assetId', 'type'] }); } + + @GenerateSql({ params: [[DummyValue.UUID, DummyValue.UUID]] }) + async deleteFiles(assetIds: string | string[]): Promise { + await this.fileRepository.delete(assetIds); + } } diff --git a/server/src/services/media.service.ts b/server/src/services/media.service.ts index 60db5acdee..77a87511c4 100644 --- a/server/src/services/media.service.ts +++ b/server/src/services/media.service.ts @@ -209,6 +209,10 @@ export class MediaService extends BaseService { if (fullsizeFile && fullsizeFile.path !== generated.fullsizePath) { this.logger.debug(`Deleting old fullsize preview image for asset ${asset.id}`); pathsToDelete.push(fullsizeFile.path); + if (!generated.fullsizePath) { + // did not generate a new fullsize image, delete the existing record + await this.assetRepository.deleteFiles(fullsizeFile.id); + } } if (pathsToDelete.length > 0) { diff --git a/server/test/repositories/asset.repository.mock.ts b/server/test/repositories/asset.repository.mock.ts index 928a7956c5..3475a9914a 100644 --- a/server/test/repositories/asset.repository.mock.ts +++ b/server/test/repositories/asset.repository.mock.ts @@ -38,5 +38,6 @@ export const newAssetRepositoryMock = (): Mocked => { getDuplicates: vitest.fn(), upsertFile: vitest.fn(), upsertFiles: vitest.fn(), + deleteFiles: vitest.fn(), }; };