Skip to content

Commit

Permalink
feat: optionally generate thumbnails for invalid images (#11126)
Browse files Browse the repository at this point in the history
  • Loading branch information
Saschl authored Jul 18, 2024
1 parent c777022 commit d37e8ed
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 13 deletions.
23 changes: 12 additions & 11 deletions docs/docs/install/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,18 @@ Regardless of filesystem, it is not recommended to use a network share for your

## General

| Variable | Description | Default | Containers | Workers |
| :---------------------------------- | :---------------------------------------------- | :--------------------------: | :----------------------- | :----------------- |
| `TZ` | Timezone | | server | microservices |
| `IMMICH_ENV` | Environment (production, development) | `production` | server, machine learning | api, microservices |
| `IMMICH_LOG_LEVEL` | Log Level (verbose, debug, log, warn, error) | `log` | server, machine learning | api, microservices |
| `IMMICH_MEDIA_LOCATION` | Media Location | `./upload`<sup>\*1</sup> | server | api, microservices |
| `IMMICH_CONFIG_FILE` | Path to config file | | server | api, microservices |
| `NO_COLOR` | Set to `true` to disable color-coded log output | `false` | server, machine learning | |
| `CPU_CORES` | Amount of cores available to the immich server | auto-detected cpu core count | server | |
| `IMMICH_API_METRICS_PORT` | Port for the OTEL metrics | `8081` | server | api |
| `IMMICH_MICROSERVICES_METRICS_PORT` | Port for the OTEL metrics | `8082` | server | microservices |
| Variable | Description | Default | Containers | Workers |
| :---------------------------------- | :-------------------------------------------------- | :--------------------------: | :----------------------- | :----------------- |
| `TZ` | Timezone | | server | microservices |
| `IMMICH_ENV` | Environment (production, development) | `production` | server, machine learning | api, microservices |
| `IMMICH_LOG_LEVEL` | Log Level (verbose, debug, log, warn, error) | `log` | server, machine learning | api, microservices |
| `IMMICH_MEDIA_LOCATION` | Media Location | `./upload`<sup>\*1</sup> | server | api, microservices |
| `IMMICH_CONFIG_FILE` | Path to config file | | server | api, microservices |
| `NO_COLOR` | Set to `true` to disable color-coded log output | `false` | server, machine learning | |
| `CPU_CORES` | Amount of cores available to the immich server | auto-detected cpu core count | server | |
| `IMMICH_API_METRICS_PORT` | Port for the OTEL metrics | `8081` | server | api |
| `IMMICH_MICROSERVICES_METRICS_PORT` | Port for the OTEL metrics | `8082` | server | microservices |
| `IMMICH_PROCESS_INVALID_IMAGES` | When `true`, generate thumbnails for invalid images | | server | microservices |

\*1: With the default `WORKDIR` of `/usr/src/app`, this path will resolve to `/usr/src/app/upload`.
It only need to be set if the Immich deployment method is changing.
Expand Down
1 change: 1 addition & 0 deletions server/src/interfaces/media.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface ThumbnailOptions {
colorspace: string;
quality: number;
crop?: CropOptions;
processInvalidImages: boolean;
}

export interface VideoStreamInfo {
Expand Down
3 changes: 2 additions & 1 deletion server/src/repositories/media.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ export class MediaRepository implements IMediaRepository {
}

async generateThumbnail(input: string | Buffer, output: string, options: ThumbnailOptions): Promise<void> {
const pipeline = sharp(input, { failOn: 'error', limitInputPixels: false })
// some invalid images can still be processed by sharp, but we want to fail on them by default to avoid crashes
const pipeline = sharp(input, { failOn: options.processInvalidImages ? 'none' : 'error', limitInputPixels: false })
.pipelineColorspace(options.colorspace === Colorspace.SRGB ? 'srgb' : 'rgb16')
.rotate();

Expand Down
30 changes: 30 additions & 0 deletions server/src/services/media.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ describe(MediaService.name, () => {
format,
quality: 80,
colorspace: Colorspace.SRGB,
processInvalidImages: false,
});
expect(assetMock.update).toHaveBeenCalledWith({ id: 'asset-id', previewPath });
});
Expand Down Expand Up @@ -326,6 +327,7 @@ describe(MediaService.name, () => {
format: ImageFormat.JPEG,
quality: 80,
colorspace: Colorspace.P3,
processInvalidImages: false,
},
);
expect(assetMock.update).toHaveBeenCalledWith({
Expand Down Expand Up @@ -468,6 +470,7 @@ describe(MediaService.name, () => {
format,
quality: 80,
colorspace: Colorspace.SRGB,
processInvalidImages: false,
});
expect(assetMock.update).toHaveBeenCalledWith({ id: 'asset-id', thumbnailPath });
},
Expand Down Expand Up @@ -498,6 +501,7 @@ describe(MediaService.name, () => {
size: 250,
quality: 80,
colorspace: Colorspace.P3,
processInvalidImages: false,
},
);
expect(assetMock.update).toHaveBeenCalledWith({
Expand All @@ -524,6 +528,7 @@ describe(MediaService.name, () => {
size: 250,
quality: 80,
colorspace: Colorspace.P3,
processInvalidImages: false,
},
],
]);
Expand All @@ -548,6 +553,7 @@ describe(MediaService.name, () => {
size: 250,
quality: 80,
colorspace: Colorspace.P3,
processInvalidImages: false,
},
],
]);
Expand All @@ -570,6 +576,7 @@ describe(MediaService.name, () => {
size: 250,
quality: 80,
colorspace: Colorspace.P3,
processInvalidImages: false,
},
);
expect(mediaMock.getImageDimensions).not.toHaveBeenCalled();
Expand All @@ -590,11 +597,34 @@ describe(MediaService.name, () => {
size: 250,
quality: 80,
colorspace: Colorspace.P3,
processInvalidImages: false,
},
);
expect(mediaMock.getImageDimensions).not.toHaveBeenCalled();
});

it('should process invalid images if enabled', async () => {
vi.stubEnv('IMMICH_PROCESS_INVALID_IMAGES', 'true');

assetMock.getByIds.mockResolvedValue([assetStub.imageDng]);

await sut.handleGenerateThumbnail({ id: assetStub.image.id });

expect(mediaMock.generateThumbnail).toHaveBeenCalledWith(
assetStub.imageDng.originalPath,
'upload/thumbs/user-id/as/se/asset-id-thumbnail.webp',
{
format: ImageFormat.WEBP,
size: 250,
quality: 80,
colorspace: Colorspace.P3,
processInvalidImages: true,
},
);
expect(mediaMock.getImageDimensions).not.toHaveBeenCalled();
vi.unstubAllEnvs();
});

describe('handleGenerateThumbhash', () => {
it('should skip thumbhash generation if asset not found', async () => {
assetMock.getByIds.mockResolvedValue([]);
Expand Down
8 changes: 7 additions & 1 deletion server/src/services/media.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,13 @@ export class MediaService {
try {
const useExtracted = didExtract && (await this.shouldUseExtractedImage(extractedPath, image.previewSize));
const colorspace = this.isSRGB(asset) ? Colorspace.SRGB : image.colorspace;
const imageOptions = { format, size, colorspace, quality: image.quality };
const imageOptions = {
format,
size,
colorspace,
quality: image.quality,
processInvalidImages: process.env.IMMICH_PROCESS_INVALID_IMAGES === 'true',
};

const outputPath = useExtracted ? extractedPath : asset.originalPath;
await this.mediaRepository.generateThumbnail(outputPath, path, imageOptions);
Expand Down
4 changes: 4 additions & 0 deletions server/src/services/person.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,7 @@ describe(PersonService.name, () => {
width: 274,
height: 274,
},
processInvalidImages: false,
},
);
expect(personMock.update).toHaveBeenCalledWith({
Expand Down Expand Up @@ -987,6 +988,7 @@ describe(PersonService.name, () => {
width: 510,
height: 510,
},
processInvalidImages: false,
},
);
});
Expand All @@ -1012,6 +1014,7 @@ describe(PersonService.name, () => {
width: 408,
height: 408,
},
processInvalidImages: false,
},
);
});
Expand All @@ -1038,6 +1041,7 @@ describe(PersonService.name, () => {
width: 588,
height: 588,
},
processInvalidImages: false,
},
);
});
Expand Down
1 change: 1 addition & 0 deletions server/src/services/person.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ export class PersonService {
colorspace: image.colorspace,
quality: image.quality,
crop: this.getCrop({ old: { width: oldWidth, height: oldHeight }, new: { width, height } }, { x1, y1, x2, y2 }),
processInvalidImages: process.env.IMMICH_PROCESS_INVALID_IMAGES === 'true',
} as const;

await this.mediaRepository.generateThumbnail(inputPath, thumbnailPath, thumbnailOptions);
Expand Down

0 comments on commit d37e8ed

Please sign in to comment.