Skip to content

Commit 9dbf5db

Browse files
fix(server): checkExistingAssets (#10192)
Co-authored-by: Alex Tran <[email protected]>
1 parent 5217042 commit 9dbf5db

File tree

5 files changed

+37
-7
lines changed

5 files changed

+37
-7
lines changed

Diff for: e2e/src/api/specs/asset.e2e-spec.ts

+25
Original file line numberDiff line numberDiff line change
@@ -1148,4 +1148,29 @@ describe('/asset', () => {
11481148
expect(video.checksum).toStrictEqual(checksum);
11491149
});
11501150
});
1151+
1152+
describe('POST /assets/exist', () => {
1153+
it('ignores invalid deviceAssetIds', async () => {
1154+
const response = await utils.checkExistingAssets(user1.accessToken, {
1155+
deviceId: 'test-assets-exist',
1156+
deviceAssetIds: ['invalid', 'INVALID'],
1157+
});
1158+
1159+
expect(response.existingIds).toHaveLength(0);
1160+
});
1161+
1162+
it('returns the IDs of existing assets', async () => {
1163+
await utils.createAsset(user1.accessToken, {
1164+
deviceId: 'test-assets-exist',
1165+
deviceAssetId: 'test-asset-0',
1166+
});
1167+
1168+
const response = await utils.checkExistingAssets(user1.accessToken, {
1169+
deviceId: 'test-assets-exist',
1170+
deviceAssetIds: ['test-asset-0'],
1171+
});
1172+
1173+
expect(response.existingIds).toEqual(['test-asset-0']);
1174+
});
1175+
});
11511176
});

Diff for: e2e/src/utils.ts

+5
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ import {
33
AssetMediaCreateDto,
44
AssetMediaResponseDto,
55
AssetResponseDto,
6+
CheckExistingAssetsDto,
67
CreateAlbumDto,
78
CreateLibraryDto,
89
MetadataSearchDto,
910
PersonCreateDto,
1011
SharedLinkCreateDto,
1112
UserAdminCreateDto,
1213
ValidateLibraryDto,
14+
checkExistingAssets,
1315
createAlbum,
1416
createApiKey,
1517
createLibrary,
@@ -374,6 +376,9 @@ export const utils = {
374376

375377
getAssetInfo: (accessToken: string, id: string) => getAssetInfo({ id }, { headers: asBearerAuth(accessToken) }),
376378

379+
checkExistingAssets: (accessToken: string, checkExistingAssetsDto: CheckExistingAssetsDto) =>
380+
checkExistingAssets({ checkExistingAssetsDto }, { headers: asBearerAuth(accessToken) }),
381+
377382
metadataSearch: async (accessToken: string, dto: MetadataSearchDto) => {
378383
return searchMetadata({ metadataSearchDto: dto }, { headers: asBearerAuth(accessToken) });
379384
},

Diff for: server/src/interfaces/asset.interface.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ export interface IAssetRepository {
156156
getByChecksums(userId: string, checksums: Buffer[]): Promise<AssetEntity[]>;
157157
getUploadAssetIdByChecksum(ownerId: string, checksum: Buffer): Promise<string | undefined>;
158158
getByAlbumId(pagination: PaginationOptions, albumId: string): Paginated<AssetEntity>;
159-
getByDeviceIds(ownerId: string, deviceId: string, deviceAssetIds: string[]): Promise<AssetEntity[]>;
159+
getByDeviceIds(ownerId: string, deviceId: string, deviceAssetIds: string[]): Promise<string[]>;
160160
getByUserId(pagination: PaginationOptions, userId: string, options?: AssetSearchOptions): Paginated<AssetEntity>;
161161
getById(
162162
id: string,

Diff for: server/src/repositories/asset.repository.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ export class AssetRepository implements IAssetRepository {
155155
});
156156
}
157157

158-
getByDeviceIds(ownerId: string, deviceId: string, deviceAssetIds: string[]): Promise<AssetEntity[]> {
159-
return this.repository.find({
158+
async getByDeviceIds(ownerId: string, deviceId: string, deviceAssetIds: string[]): Promise<string[]> {
159+
const assets = await this.repository.find({
160160
select: { deviceAssetId: true },
161161
where: {
162162
deviceAssetId: In(deviceAssetIds),
@@ -165,6 +165,8 @@ export class AssetRepository implements IAssetRepository {
165165
},
166166
withDeleted: true,
167167
});
168+
169+
return assets.map((asset) => asset.deviceAssetId);
168170
}
169171

170172
getByUserId(

Diff for: server/src/services/asset-media.service.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -277,14 +277,12 @@ export class AssetMediaService {
277277
auth: AuthDto,
278278
checkExistingAssetsDto: CheckExistingAssetsDto,
279279
): Promise<CheckExistingAssetsResponseDto> {
280-
const assets = await this.assetRepository.getByDeviceIds(
280+
const existingIds = await this.assetRepository.getByDeviceIds(
281281
auth.user.id,
282282
checkExistingAssetsDto.deviceId,
283283
checkExistingAssetsDto.deviceAssetIds,
284284
);
285-
return {
286-
existingIds: assets.map((asset) => asset.id),
287-
};
285+
return { existingIds };
288286
}
289287

290288
async bulkUploadCheck(auth: AuthDto, dto: AssetBulkUploadCheckDto): Promise<AssetBulkUploadCheckResponseDto> {

0 commit comments

Comments
 (0)