From 9dbf5db72e259512d5f122935ede75eed70bd38c Mon Sep 17 00:00:00 2001 From: Michel Heusschen <59014050+michelheusschen@users.noreply.github.com> Date: Wed, 12 Jun 2024 15:48:44 +0200 Subject: [PATCH] fix(server): checkExistingAssets (#10192) Co-authored-by: Alex Tran --- e2e/src/api/specs/asset.e2e-spec.ts | 25 +++++++++++++++++++++ e2e/src/utils.ts | 5 +++++ server/src/interfaces/asset.interface.ts | 2 +- server/src/repositories/asset.repository.ts | 6 +++-- server/src/services/asset-media.service.ts | 6 ++--- 5 files changed, 37 insertions(+), 7 deletions(-) diff --git a/e2e/src/api/specs/asset.e2e-spec.ts b/e2e/src/api/specs/asset.e2e-spec.ts index cf0557bd6c..f315810464 100644 --- a/e2e/src/api/specs/asset.e2e-spec.ts +++ b/e2e/src/api/specs/asset.e2e-spec.ts @@ -1148,4 +1148,29 @@ describe('/asset', () => { expect(video.checksum).toStrictEqual(checksum); }); }); + + describe('POST /assets/exist', () => { + it('ignores invalid deviceAssetIds', async () => { + const response = await utils.checkExistingAssets(user1.accessToken, { + deviceId: 'test-assets-exist', + deviceAssetIds: ['invalid', 'INVALID'], + }); + + expect(response.existingIds).toHaveLength(0); + }); + + it('returns the IDs of existing assets', async () => { + await utils.createAsset(user1.accessToken, { + deviceId: 'test-assets-exist', + deviceAssetId: 'test-asset-0', + }); + + const response = await utils.checkExistingAssets(user1.accessToken, { + deviceId: 'test-assets-exist', + deviceAssetIds: ['test-asset-0'], + }); + + expect(response.existingIds).toEqual(['test-asset-0']); + }); + }); }); diff --git a/e2e/src/utils.ts b/e2e/src/utils.ts index 2b26537677..72b3480299 100644 --- a/e2e/src/utils.ts +++ b/e2e/src/utils.ts @@ -3,6 +3,7 @@ import { AssetMediaCreateDto, AssetMediaResponseDto, AssetResponseDto, + CheckExistingAssetsDto, CreateAlbumDto, CreateLibraryDto, MetadataSearchDto, @@ -10,6 +11,7 @@ import { SharedLinkCreateDto, UserAdminCreateDto, ValidateLibraryDto, + checkExistingAssets, createAlbum, createApiKey, createLibrary, @@ -374,6 +376,9 @@ export const utils = { getAssetInfo: (accessToken: string, id: string) => getAssetInfo({ id }, { headers: asBearerAuth(accessToken) }), + checkExistingAssets: (accessToken: string, checkExistingAssetsDto: CheckExistingAssetsDto) => + checkExistingAssets({ checkExistingAssetsDto }, { headers: asBearerAuth(accessToken) }), + metadataSearch: async (accessToken: string, dto: MetadataSearchDto) => { return searchMetadata({ metadataSearchDto: dto }, { headers: asBearerAuth(accessToken) }); }, diff --git a/server/src/interfaces/asset.interface.ts b/server/src/interfaces/asset.interface.ts index 43886a5534..a9a74f711b 100644 --- a/server/src/interfaces/asset.interface.ts +++ b/server/src/interfaces/asset.interface.ts @@ -156,7 +156,7 @@ export interface IAssetRepository { getByChecksums(userId: string, checksums: Buffer[]): Promise; getUploadAssetIdByChecksum(ownerId: string, checksum: Buffer): Promise; getByAlbumId(pagination: PaginationOptions, albumId: string): Paginated; - getByDeviceIds(ownerId: string, deviceId: string, deviceAssetIds: string[]): Promise; + getByDeviceIds(ownerId: string, deviceId: string, deviceAssetIds: string[]): Promise; getByUserId(pagination: PaginationOptions, userId: string, options?: AssetSearchOptions): Paginated; getById( id: string, diff --git a/server/src/repositories/asset.repository.ts b/server/src/repositories/asset.repository.ts index 15ff5ced7a..ee7a97004a 100644 --- a/server/src/repositories/asset.repository.ts +++ b/server/src/repositories/asset.repository.ts @@ -155,8 +155,8 @@ export class AssetRepository implements IAssetRepository { }); } - getByDeviceIds(ownerId: string, deviceId: string, deviceAssetIds: string[]): Promise { - return this.repository.find({ + async getByDeviceIds(ownerId: string, deviceId: string, deviceAssetIds: string[]): Promise { + const assets = await this.repository.find({ select: { deviceAssetId: true }, where: { deviceAssetId: In(deviceAssetIds), @@ -165,6 +165,8 @@ export class AssetRepository implements IAssetRepository { }, withDeleted: true, }); + + return assets.map((asset) => asset.deviceAssetId); } getByUserId( diff --git a/server/src/services/asset-media.service.ts b/server/src/services/asset-media.service.ts index d11fc5dbaf..efe7b1688f 100644 --- a/server/src/services/asset-media.service.ts +++ b/server/src/services/asset-media.service.ts @@ -277,14 +277,12 @@ export class AssetMediaService { auth: AuthDto, checkExistingAssetsDto: CheckExistingAssetsDto, ): Promise { - const assets = await this.assetRepository.getByDeviceIds( + const existingIds = await this.assetRepository.getByDeviceIds( auth.user.id, checkExistingAssetsDto.deviceId, checkExistingAssetsDto.deviceAssetIds, ); - return { - existingIds: assets.map((asset) => asset.id), - }; + return { existingIds }; } async bulkUploadCheck(auth: AuthDto, dto: AssetBulkUploadCheckDto): Promise {