From 247429c3e4db6e223f572bfeadb774cc1dc554aa Mon Sep 17 00:00:00 2001 From: Jonathan Jogenfors Date: Thu, 14 Mar 2024 00:47:25 +0100 Subject: [PATCH] only check for offline when using checkForOffline --- server/e2e/jobs/specs/library.e2e-spec.ts | 88 ++++++++++++++++++-- server/src/domain/library/library.service.ts | 13 +-- 2 files changed, 83 insertions(+), 18 deletions(-) diff --git a/server/e2e/jobs/specs/library.e2e-spec.ts b/server/e2e/jobs/specs/library.e2e-spec.ts index 6dca783c4..a4af5bda8 100644 --- a/server/e2e/jobs/specs/library.e2e-spec.ts +++ b/server/e2e/jobs/specs/library.e2e-spec.ts @@ -145,7 +145,7 @@ describe(`${LibraryController.name} (e2e)`, () => { ); }); - it('should offline missing files', async () => { + it('should offline a missing files when called with checkForOffline', async () => { await fs.promises.cp(`${IMMICH_TEST_ASSET_PATH}/albums/nature`, `${IMMICH_TEST_ASSET_TEMP_PATH}/albums/nature`, { recursive: true, }); @@ -160,7 +160,42 @@ describe(`${LibraryController.name} (e2e)`, () => { const onlineAssets = await api.assetApi.getAllAssets(server, admin.accessToken); expect(onlineAssets.length).toBeGreaterThan(1); - await restoreTempFolder(); + await fs.promises.rm(`${IMMICH_TEST_ASSET_TEMP_PATH}/albums/nature/silver_fir.jpg`); + + await api.libraryApi.scanLibrary(server, admin.accessToken, library.id, { checkForOffline: true }); + + const assets = await api.assetApi.getAllAssets(server, admin.accessToken); + + expect(assets).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + isOffline: true, + originalFileName: 'silver_fir', + }), + expect.objectContaining({ + isOffline: false, + originalFileName: 'tanners_ridge', + }), + ]), + ); + }); + + it('should not offline a missing files when performing regular scan', async () => { + await fs.promises.cp(`${IMMICH_TEST_ASSET_PATH}/albums/nature`, `${IMMICH_TEST_ASSET_TEMP_PATH}/albums/nature`, { + recursive: true, + }); + + const library = await api.libraryApi.create(server, admin.accessToken, { + type: LibraryType.EXTERNAL, + importPaths: [`${IMMICH_TEST_ASSET_TEMP_PATH}`], + }); + + await api.libraryApi.scanLibrary(server, admin.accessToken, library.id); + + const onlineAssets = await api.assetApi.getAllAssets(server, admin.accessToken); + expect(onlineAssets.length).toBeGreaterThan(1); + + await fs.promises.rm(`${IMMICH_TEST_ASSET_TEMP_PATH}/albums/nature/silver_fir.jpg`); await api.libraryApi.scanLibrary(server, admin.accessToken, library.id); @@ -169,11 +204,52 @@ describe(`${LibraryController.name} (e2e)`, () => { expect(assets).toEqual( expect.arrayContaining([ expect.objectContaining({ - isOffline: true, - originalFileName: 'el_torcal_rocks', + isOffline: false, + originalFileName: 'silver_fir', }), expect.objectContaining({ - isOffline: true, + isOffline: false, + originalFileName: 'tanners_ridge', + }), + ]), + ); + }); + + it('should mark a rediscovered file as back online', async () => { + await fs.promises.cp(`${IMMICH_TEST_ASSET_PATH}/albums/nature`, `${IMMICH_TEST_ASSET_TEMP_PATH}/albums/nature`, { + recursive: true, + }); + + const library = await api.libraryApi.create(server, admin.accessToken, { + type: LibraryType.EXTERNAL, + importPaths: [`${IMMICH_TEST_ASSET_TEMP_PATH}`], + }); + + await api.libraryApi.scanLibrary(server, admin.accessToken, library.id); + + const onlineAssets = await api.assetApi.getAllAssets(server, admin.accessToken); + expect(onlineAssets.length).toBeGreaterThan(1); + + await fs.promises.rm(`${IMMICH_TEST_ASSET_TEMP_PATH}/albums/nature/silver_fir.jpg`); + + await api.libraryApi.scanLibrary(server, admin.accessToken, library.id, { checkForOffline: true }); + + await fs.promises.cp(`${IMMICH_TEST_ASSET_PATH}/albums/nature`, `${IMMICH_TEST_ASSET_TEMP_PATH}/albums/nature`, { + recursive: true, + }); + + await api.libraryApi.scanLibrary(server, admin.accessToken, library.id); + + const assets = await api.assetApi.getAllAssets(server, admin.accessToken); + + expect(assets).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + isOffline: false, + originalFileName: 'silver_fir', + }), + expect.objectContaining({ + isOffline: false, originalFileName: 'tanners_ridge', }), ]), @@ -385,7 +461,7 @@ describe(`${LibraryController.name} (e2e)`, () => { await restoreTempFolder(); - await api.libraryApi.scanLibrary(server, admin.accessToken, library.id); + await api.libraryApi.scanLibrary(server, admin.accessToken, library.id, { checkForOffline: true }); const { status } = await request(server) .post(`/library/${library.id}/removeOffline`) diff --git a/server/src/domain/library/library.service.ts b/server/src/domain/library/library.service.ts index 7e42ebad9..628bde1bc 100644 --- a/server/src/domain/library/library.service.ts +++ b/server/src/domain/library/library.service.ts @@ -699,7 +699,6 @@ export class LibraryService extends EventEmitter { this.logger.debug(`Found ${crawledAssetPaths.size} asset(s) when crawling import paths ${library.importPaths}`); - const assetIdsToMarkOffline = []; const assetIdsToMarkOnline = []; const pagination = usePagination(5000, (pagination) => this.assetRepository.getLibraryAssetPaths(pagination, library.id), @@ -707,12 +706,7 @@ export class LibraryService extends EventEmitter { for await (const page of pagination) { for (const asset of page) { - const isOffline = !crawledAssetPaths.has(asset.originalPath); - if (isOffline && !asset.isOffline) { - assetIdsToMarkOffline.push(asset.id); - } - - if (!isOffline && asset.isOffline) { + if (asset.isOffline) { assetIdsToMarkOnline.push(asset.id); } @@ -720,11 +714,6 @@ export class LibraryService extends EventEmitter { } } - if (assetIdsToMarkOffline.length > 0) { - this.logger.debug(`Found ${assetIdsToMarkOffline.length} offline asset(s) previously marked as online`); - await this.assetRepository.updateAll(assetIdsToMarkOffline, { isOffline: true }); - } - if (assetIdsToMarkOnline.length > 0) { this.logger.debug(`Found ${assetIdsToMarkOnline.length} online asset(s) previously marked as offline`); await this.assetRepository.updateAll(assetIdsToMarkOnline, { isOffline: false });