From 5761765ea7f1ffd7201d1172a12380b59c70a02d Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 12 Sep 2022 22:06:52 -0500 Subject: [PATCH] fix(server): remove album thumbnail when the asset is deleted from the database (#681) --- .../immich/src/api-v1/album/album.module.ts | 5 ++++ .../immich/src/api-v1/album/album.service.ts | 25 ++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/server/apps/immich/src/api-v1/album/album.module.ts b/server/apps/immich/src/api-v1/album/album.module.ts index a19d041e5b..6ef1f5cc49 100644 --- a/server/apps/immich/src/api-v1/album/album.module.ts +++ b/server/apps/immich/src/api-v1/album/album.module.ts @@ -8,6 +8,7 @@ import { AlbumEntity } from '../../../../../libs/database/src/entities/album.ent import { AssetAlbumEntity } from '@app/database/entities/asset-album.entity'; import { UserAlbumEntity } from '@app/database/entities/user-album.entity'; import { AlbumRepository, ALBUM_REPOSITORY } from './album-repository'; +import { AssetRepository, ASSET_REPOSITORY } from '../asset/asset-repository'; @Module({ imports: [TypeOrmModule.forFeature([AssetEntity, UserEntity, AlbumEntity, AssetAlbumEntity, UserAlbumEntity])], @@ -18,6 +19,10 @@ import { AlbumRepository, ALBUM_REPOSITORY } from './album-repository'; provide: ALBUM_REPOSITORY, useClass: AlbumRepository, }, + { + provide: ASSET_REPOSITORY, + useClass: AssetRepository, + }, ], }) export class AlbumModule {} diff --git a/server/apps/immich/src/api-v1/album/album.service.ts b/server/apps/immich/src/api-v1/album/album.service.ts index 465df920b1..fdc13fea03 100644 --- a/server/apps/immich/src/api-v1/album/album.service.ts +++ b/server/apps/immich/src/api-v1/album/album.service.ts @@ -10,10 +10,14 @@ import { GetAlbumsDto } from './dto/get-albums.dto'; import { AlbumResponseDto, mapAlbum, mapAlbumExcludeAssetInfo } from './response-dto/album-response.dto'; import { ALBUM_REPOSITORY, IAlbumRepository } from './album-repository'; import { AlbumCountResponseDto } from './response-dto/album-count-response.dto'; +import { ASSET_REPOSITORY, IAssetRepository } from '../asset/asset-repository'; @Injectable() export class AlbumService { - constructor(@Inject(ALBUM_REPOSITORY) private _albumRepository: IAlbumRepository) {} + constructor( + @Inject(ALBUM_REPOSITORY) private _albumRepository: IAlbumRepository, + @Inject(ASSET_REPOSITORY) private _assetRepository: IAssetRepository, + ) {} private async _getAlbum({ authUser, @@ -54,6 +58,11 @@ export class AlbumService { return albums.map(mapAlbumExcludeAssetInfo); } const albums = await this._albumRepository.getList(authUser.id, getAlbumsDto); + + for (const album of albums) { + await this._checkValidThumbnail(album); + } + return albums.map((album) => mapAlbumExcludeAssetInfo(album)); } @@ -123,4 +132,18 @@ export class AlbumService { async getAlbumCountByUserId(authUser: AuthUserDto): Promise { return this._albumRepository.getCountByUserId(authUser.id); } + + async _checkValidThumbnail(album: AlbumEntity): Promise { + const assetId = album.albumThumbnailAssetId; + if (assetId) { + try { + await this._assetRepository.getById(assetId); + } catch (e) { + album.albumThumbnailAssetId = null; + return await this._albumRepository.updateAlbum(album, {}); + } + } + + return album; + } }