From 04d4a3047160c5c9f47fcd3236d58a1666849a1c Mon Sep 17 00:00:00 2001 From: Mert <101130780+mertalev@users.noreply.github.com> Date: Mon, 4 Sep 2023 19:24:55 -0400 Subject: [PATCH] fix(server): await thumbnail generation before returning (#3975) * await sharp command, minor fixes * removed outdated test --- server/src/domain/media/media.service.spec.ts | 6 ------ server/src/domain/media/media.service.ts | 2 +- server/src/infra/repositories/media.repository.ts | 3 ++- 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/server/src/domain/media/media.service.spec.ts b/server/src/domain/media/media.service.spec.ts index dc9359aa4..35ba91b2f 100644 --- a/server/src/domain/media/media.service.spec.ts +++ b/server/src/domain/media/media.service.spec.ts @@ -200,12 +200,6 @@ describe(MediaService.name, () => { expect(assetMock.save).not.toHaveBeenCalledWith(); }); - it('should skip thumbnail generate if resize path is missing', async () => { - assetMock.getByIds.mockResolvedValue([assetStub.noResizePath]); - await sut.handleGenerateWebpThumbnail({ id: assetStub.noResizePath.id }); - expect(mediaMock.resize).not.toHaveBeenCalled(); - }); - it('should generate a thumbnail', async () => { assetMock.getByIds.mockResolvedValue([assetStub.image]); await sut.handleGenerateWebpThumbnail({ id: assetStub.image.id }); diff --git a/server/src/domain/media/media.service.ts b/server/src/domain/media/media.service.ts index 2d0c5755d..76e62f72d 100644 --- a/server/src/domain/media/media.service.ts +++ b/server/src/domain/media/media.service.ts @@ -109,7 +109,7 @@ export class MediaService { async handleGenerateWebpThumbnail({ id }: IEntityJob) { const [asset] = await this.assetRepository.getByIds([id]); - if (!asset || !asset.resizePath) { + if (!asset) { return false; } diff --git a/server/src/infra/repositories/media.repository.ts b/server/src/infra/repositories/media.repository.ts index 2d711a530..e3b3027e0 100644 --- a/server/src/infra/repositories/media.repository.ts +++ b/server/src/infra/repositories/media.repository.ts @@ -15,6 +15,7 @@ export class MediaRepository implements IMediaRepository { crop(input: string | Buffer, options: CropOptions): Promise { return sharp(input, { failOn: 'none' }) + .pipelineColorspace('rgb16') .extract({ left: options.left, top: options.top, @@ -38,7 +39,7 @@ export class MediaRepository implements IMediaRepository { } } const chromaSubsampling = options.quality >= 80 ? '4:4:4' : '4:2:0'; // this is default in libvips (except the threshold is 90), but we need to set it manually in sharp - sharp(input, { failOn: 'none' }) + await sharp(input, { failOn: 'none' }) .pipelineColorspace('rgb16') .resize(options.size, options.size, { fit: 'outside', withoutEnlargement: true }) .rotate()