Skip to content

Commit

Permalink
chore(server): change save -> update in asset repository (#8055)
Browse files Browse the repository at this point in the history
* `save` -> `update`

* change return type

* include relations

* fix tests

* remove when mocks

* fix

* stricter typing

* simpler type
  • Loading branch information
mertalev committed Mar 20, 2024
1 parent 9e4bab7 commit 2daed74
Show file tree
Hide file tree
Showing 14 changed files with 128 additions and 138 deletions.
8 changes: 4 additions & 4 deletions server/src/domain/asset/asset.service.spec.ts
Expand Up @@ -548,19 +548,19 @@ describe(AssetService.name, () => {
await expect(sut.update(authStub.admin, 'asset-1', { isArchived: false })).rejects.toBeInstanceOf(
BadRequestException,
);
expect(assetMock.save).not.toHaveBeenCalled();
expect(assetMock.update).not.toHaveBeenCalled();
});

it('should update the asset', async () => {
accessMock.asset.checkOwnerAccess.mockResolvedValue(new Set(['asset-1']));
assetMock.save.mockResolvedValue(assetStub.image);
assetMock.getById.mockResolvedValue(assetStub.image);
await sut.update(authStub.admin, 'asset-1', { isFavorite: true });
expect(assetMock.save).toHaveBeenCalledWith({ id: 'asset-1', isFavorite: true });
expect(assetMock.update).toHaveBeenCalledWith({ id: 'asset-1', isFavorite: true });
});

it('should update the exif description', async () => {
accessMock.asset.checkOwnerAccess.mockResolvedValue(new Set(['asset-1']));
assetMock.save.mockResolvedValue(assetStub.image);
assetMock.getById.mockResolvedValue(assetStub.image);
await sut.update(authStub.admin, 'asset-1', { description: 'Test description' });
expect(assetMock.upsertExif).toHaveBeenCalledWith({ assetId: 'asset-1', description: 'Test description' });
});
Expand Down
14 changes: 13 additions & 1 deletion server/src/domain/asset/asset.service.ts
Expand Up @@ -324,7 +324,19 @@ export class AssetService {
const { description, dateTimeOriginal, latitude, longitude, ...rest } = dto;
await this.updateMetadata({ id, description, dateTimeOriginal, latitude, longitude });

const asset = await this.assetRepository.save({ id, ...rest });
await this.assetRepository.update({ id, ...rest });
const asset = await this.assetRepository.getById(id, {
exifInfo: true,
owner: true,
smartInfo: true,
tags: true,
faces: {
person: true,
},
});
if (!asset) {
throw new BadRequestException('Asset not found');
}
return mapAsset(asset, { auth });
}

Expand Down
10 changes: 5 additions & 5 deletions server/src/domain/audit/audit.service.ts
Expand Up @@ -93,27 +93,27 @@ export class AuditService {

switch (pathType) {
case AssetPathType.ENCODED_VIDEO: {
await this.assetRepository.save({ id, encodedVideoPath: pathValue });
await this.assetRepository.update({ id, encodedVideoPath: pathValue });
break;
}

case AssetPathType.JPEG_THUMBNAIL: {
await this.assetRepository.save({ id, resizePath: pathValue });
await this.assetRepository.update({ id, resizePath: pathValue });
break;
}

case AssetPathType.WEBP_THUMBNAIL: {
await this.assetRepository.save({ id, webpPath: pathValue });
await this.assetRepository.update({ id, webpPath: pathValue });
break;
}

case AssetPathType.ORIGINAL: {
await this.assetRepository.save({ id, originalPath: pathValue });
await this.assetRepository.update({ id, originalPath: pathValue });
break;
}

case AssetPathType.SIDECAR: {
await this.assetRepository.save({ id, sidecarPath: pathValue });
await this.assetRepository.update({ id, sidecarPath: pathValue });
break;
}

Expand Down
8 changes: 4 additions & 4 deletions server/src/domain/library/library.service.spec.ts
Expand Up @@ -584,7 +584,7 @@ describe(LibraryService.name, () => {

await expect(sut.handleAssetRefresh(mockLibraryJob)).resolves.toBe(JobStatus.SUCCESS);

expect(assetMock.save).toHaveBeenCalledWith({ id: assetStub.image.id, isOffline: true });
expect(assetMock.update).toHaveBeenCalledWith({ id: assetStub.image.id, isOffline: true });
expect(jobMock.queue).not.toHaveBeenCalled();
expect(jobMock.queueAll).not.toHaveBeenCalled();
});
Expand All @@ -602,7 +602,7 @@ describe(LibraryService.name, () => {

await expect(sut.handleAssetRefresh(mockLibraryJob)).resolves.toBe(JobStatus.SUCCESS);

expect(assetMock.save).toHaveBeenCalledWith({ id: assetStub.offline.id, isOffline: false });
expect(assetMock.update).toHaveBeenCalledWith({ id: assetStub.offline.id, isOffline: false });

expect(jobMock.queue).toHaveBeenCalledWith({
name: JobName.METADATA_EXTRACTION,
Expand Down Expand Up @@ -631,7 +631,7 @@ describe(LibraryService.name, () => {
assetMock.getByLibraryIdAndOriginalPath.mockResolvedValue(assetStub.image);
assetMock.create.mockResolvedValue(assetStub.image);

expect(assetMock.save).not.toHaveBeenCalled();
expect(assetMock.update).not.toHaveBeenCalled();

await expect(sut.handleAssetRefresh(mockLibraryJob)).resolves.toBe(JobStatus.SUCCESS);
});
Expand Down Expand Up @@ -1257,7 +1257,7 @@ describe(LibraryService.name, () => {

await sut.watchAll();

expect(assetMock.save).toHaveBeenCalledWith({ id: assetStub.external.id, isOffline: true });
expect(assetMock.update).toHaveBeenCalledWith({ id: assetStub.external.id, isOffline: true });
});

it('should handle an error event', async () => {
Expand Down
6 changes: 3 additions & 3 deletions server/src/domain/library/library.service.ts
Expand Up @@ -173,7 +173,7 @@ export class LibraryService extends EventEmitter {
this.logger.debug(`Detected deleted file at ${path} in library ${library.id}`);
const asset = await this.assetRepository.getByLibraryIdAndOriginalPath(library.id, path);
if (asset && matcher(path)) {
await this.assetRepository.save({ id: asset.id, isOffline: true });
await this.assetRepository.update({ id: asset.id, isOffline: true });
}
this.emit(StorageEventType.UNLINK, path);
};
Expand Down Expand Up @@ -429,7 +429,7 @@ export class LibraryService extends EventEmitter {
// Mark asset as offline
this.logger.debug(`Marking asset as offline: ${assetPath}`);

await this.assetRepository.save({ id: existingAssetEntity.id, isOffline: true });
await this.assetRepository.update({ id: existingAssetEntity.id, isOffline: true });
return JobStatus.SUCCESS;
} else {
// File can't be accessed and does not already exist in db
Expand Down Expand Up @@ -462,7 +462,7 @@ export class LibraryService extends EventEmitter {
if (stats && existingAssetEntity?.isOffline) {
// File was previously offline but is now online
this.logger.debug(`Marking previously-offline asset as online: ${assetPath}`);
await this.assetRepository.save({ id: existingAssetEntity.id, isOffline: false });
await this.assetRepository.update({ id: existingAssetEntity.id, isOffline: false });
doRefresh = true;
}

Expand Down
20 changes: 10 additions & 10 deletions server/src/domain/media/media.service.spec.ts
Expand Up @@ -205,15 +205,15 @@ describe(MediaService.name, () => {
assetMock.getByIds.mockResolvedValue([]);
await sut.handleGenerateJpegThumbnail({ id: assetStub.image.id });
expect(mediaMock.resize).not.toHaveBeenCalled();
expect(assetMock.save).not.toHaveBeenCalledWith();
expect(assetMock.update).not.toHaveBeenCalledWith();
});

it('should skip video thumbnail generation if no video stream', async () => {
mediaMock.probe.mockResolvedValue(probeStub.noVideoStreams);
assetMock.getByIds.mockResolvedValue([assetStub.video]);
await sut.handleGenerateJpegThumbnail({ id: assetStub.image.id });
expect(mediaMock.resize).not.toHaveBeenCalled();
expect(assetMock.save).not.toHaveBeenCalledWith();
expect(assetMock.update).not.toHaveBeenCalledWith();
});

it('should generate a thumbnail for an image', async () => {
Expand All @@ -227,7 +227,7 @@ describe(MediaService.name, () => {
quality: 80,
colorspace: Colorspace.SRGB,
});
expect(assetMock.save).toHaveBeenCalledWith({
expect(assetMock.update).toHaveBeenCalledWith({
id: 'asset-id',
resizePath: 'upload/thumbs/user-id/as/se/asset-id.jpeg',
});
Expand All @@ -246,7 +246,7 @@ describe(MediaService.name, () => {
quality: 80,
colorspace: Colorspace.P3,
});
expect(assetMock.save).toHaveBeenCalledWith({
expect(assetMock.update).toHaveBeenCalledWith({
id: 'asset-id',
resizePath: 'upload/thumbs/user-id/as/se/asset-id.jpeg',
});
Expand All @@ -271,7 +271,7 @@ describe(MediaService.name, () => {
twoPass: false,
},
);
expect(assetMock.save).toHaveBeenCalledWith({
expect(assetMock.update).toHaveBeenCalledWith({
id: 'asset-id',
resizePath: 'upload/thumbs/user-id/as/se/asset-id.jpeg',
});
Expand All @@ -296,7 +296,7 @@ describe(MediaService.name, () => {
twoPass: false,
},
);
expect(assetMock.save).toHaveBeenCalledWith({
expect(assetMock.update).toHaveBeenCalledWith({
id: 'asset-id',
resizePath: 'upload/thumbs/user-id/as/se/asset-id.jpeg',
});
Expand Down Expand Up @@ -337,7 +337,7 @@ describe(MediaService.name, () => {
assetMock.getByIds.mockResolvedValue([]);
await sut.handleGenerateWebpThumbnail({ id: assetStub.image.id });
expect(mediaMock.resize).not.toHaveBeenCalled();
expect(assetMock.save).not.toHaveBeenCalledWith();
expect(assetMock.update).not.toHaveBeenCalledWith();
});

it('should generate a thumbnail', async () => {
Expand All @@ -350,7 +350,7 @@ describe(MediaService.name, () => {
quality: 80,
colorspace: Colorspace.SRGB,
});
expect(assetMock.save).toHaveBeenCalledWith({
expect(assetMock.update).toHaveBeenCalledWith({
id: 'asset-id',
webpPath: 'upload/thumbs/user-id/as/se/asset-id.webp',
});
Expand All @@ -370,7 +370,7 @@ describe(MediaService.name, () => {
quality: 80,
colorspace: Colorspace.P3,
});
expect(assetMock.save).toHaveBeenCalledWith({
expect(assetMock.update).toHaveBeenCalledWith({
id: 'asset-id',
webpPath: 'upload/thumbs/user-id/as/se/asset-id.webp',
});
Expand All @@ -397,7 +397,7 @@ describe(MediaService.name, () => {
await sut.handleGenerateThumbhashThumbnail({ id: assetStub.image.id });

expect(mediaMock.generateThumbhash).toHaveBeenCalledWith('/uploads/user-id/thumbs/path.jpg');
expect(assetMock.save).toHaveBeenCalledWith({ id: 'asset-id', thumbhash: thumbhashBuffer });
expect(assetMock.update).toHaveBeenCalledWith({ id: 'asset-id', thumbhash: thumbhashBuffer });
});
});

Expand Down
10 changes: 5 additions & 5 deletions server/src/domain/media/media.service.ts
Expand Up @@ -172,7 +172,7 @@ export class MediaService {
}

const resizePath = await this.generateThumbnail(asset, 'jpeg');
await this.assetRepository.save({ id: asset.id, resizePath });
await this.assetRepository.update({ id: asset.id, resizePath });
return JobStatus.SUCCESS;
}

Expand Down Expand Up @@ -222,7 +222,7 @@ export class MediaService {
}

const webpPath = await this.generateThumbnail(asset, 'webp');
await this.assetRepository.save({ id: asset.id, webpPath });
await this.assetRepository.update({ id: asset.id, webpPath });
return JobStatus.SUCCESS;
}

Expand All @@ -233,7 +233,7 @@ export class MediaService {
}

const thumbhash = await this.mediaRepository.generateThumbhash(asset.resizePath);
await this.assetRepository.save({ id: asset.id, thumbhash });
await this.assetRepository.update({ id: asset.id, thumbhash });

return JobStatus.SUCCESS;
}
Expand Down Expand Up @@ -286,7 +286,7 @@ export class MediaService {
if (asset.encodedVideoPath) {
this.logger.log(`Transcoded video exists for asset ${asset.id}, but is no longer required. Deleting...`);
await this.jobRepository.queue({ name: JobName.DELETE_FILES, data: { files: [asset.encodedVideoPath] } });
await this.assetRepository.save({ id: asset.id, encodedVideoPath: null });
await this.assetRepository.update({ id: asset.id, encodedVideoPath: null });
}

return JobStatus.SKIPPED;
Expand Down Expand Up @@ -321,7 +321,7 @@ export class MediaService {

this.logger.log(`Successfully encoded ${asset.id}`);

await this.assetRepository.save({ id: asset.id, encodedVideoPath: output });
await this.assetRepository.update({ id: asset.id, encodedVideoPath: output });

return JobStatus.SUCCESS;
}
Expand Down

0 comments on commit 2daed74

Please sign in to comment.