2023-06-01 12:32:51 +02:00
|
|
|
import {
|
2023-07-09 04:43:11 +02:00
|
|
|
AudioCodec,
|
2023-09-03 03:22:42 +02:00
|
|
|
CQMode,
|
2023-09-03 08:21:51 +02:00
|
|
|
Colorspace,
|
2023-06-01 12:32:51 +02:00
|
|
|
SystemConfig,
|
|
|
|
SystemConfigEntity,
|
|
|
|
SystemConfigKey,
|
|
|
|
SystemConfigValue,
|
2023-08-07 22:35:25 +02:00
|
|
|
ToneMapping,
|
2023-08-02 03:56:10 +02:00
|
|
|
TranscodeHWAccel,
|
2023-07-09 04:43:11 +02:00
|
|
|
TranscodePolicy,
|
|
|
|
VideoCodec,
|
2023-06-01 12:32:51 +02:00
|
|
|
} from '@app/infra/entities';
|
2023-08-25 06:15:03 +02:00
|
|
|
import { BadRequestException, ForbiddenException, Injectable, Logger } from '@nestjs/common';
|
2023-08-25 19:44:52 +02:00
|
|
|
import { plainToClass } from 'class-transformer';
|
|
|
|
import { validate } from 'class-validator';
|
2022-12-09 21:51:42 +01:00
|
|
|
import * as _ from 'lodash';
|
2022-12-16 21:26:12 +01:00
|
|
|
import { Subject } from 'rxjs';
|
2023-01-21 17:11:55 +01:00
|
|
|
import { DeepPartial } from 'typeorm';
|
2023-06-01 12:32:51 +02:00
|
|
|
import { QueueName } from '../job/job.constants';
|
2023-08-25 19:44:52 +02:00
|
|
|
import { SystemConfigDto } from './dto';
|
2023-01-21 17:11:55 +01:00
|
|
|
import { ISystemConfigRepository } from './system-config.repository';
|
2022-12-09 21:51:42 +01:00
|
|
|
|
2022-12-16 21:26:12 +01:00
|
|
|
export type SystemConfigValidator = (config: SystemConfig) => void | Promise<void>;
|
|
|
|
|
2023-07-15 06:03:56 +02:00
|
|
|
export const defaults = Object.freeze<SystemConfig>({
|
2022-12-09 21:51:42 +01:00
|
|
|
ffmpeg: {
|
2023-05-22 20:07:43 +02:00
|
|
|
crf: 23,
|
|
|
|
threads: 0,
|
2022-12-09 21:51:42 +01:00
|
|
|
preset: 'ultrafast',
|
2023-07-09 04:43:11 +02:00
|
|
|
targetVideoCodec: VideoCodec.H264,
|
|
|
|
targetAudioCodec: AudioCodec.AAC,
|
2023-04-04 03:42:53 +02:00
|
|
|
targetResolution: '720',
|
2023-05-22 20:07:43 +02:00
|
|
|
maxBitrate: '0',
|
2023-09-03 03:22:42 +02:00
|
|
|
bframes: -1,
|
|
|
|
refs: 0,
|
|
|
|
gopSize: 0,
|
|
|
|
npl: 0,
|
|
|
|
temporalAQ: false,
|
|
|
|
cqMode: CQMode.AUTO,
|
2023-05-22 20:07:43 +02:00
|
|
|
twoPass: false,
|
2023-07-09 04:43:11 +02:00
|
|
|
transcode: TranscodePolicy.REQUIRED,
|
2023-08-07 22:35:25 +02:00
|
|
|
tonemap: ToneMapping.HABLE,
|
2023-08-02 03:56:10 +02:00
|
|
|
accel: TranscodeHWAccel.DISABLED,
|
2022-11-15 05:39:32 +01:00
|
|
|
},
|
2023-06-01 12:32:51 +02:00
|
|
|
job: {
|
|
|
|
[QueueName.BACKGROUND_TASK]: { concurrency: 5 },
|
|
|
|
[QueueName.CLIP_ENCODING]: { concurrency: 2 },
|
|
|
|
[QueueName.METADATA_EXTRACTION]: { concurrency: 5 },
|
|
|
|
[QueueName.OBJECT_TAGGING]: { concurrency: 2 },
|
|
|
|
[QueueName.RECOGNIZE_FACES]: { concurrency: 2 },
|
|
|
|
[QueueName.SEARCH]: { concurrency: 5 },
|
|
|
|
[QueueName.SIDECAR]: { concurrency: 5 },
|
2023-09-20 13:16:33 +02:00
|
|
|
[QueueName.LIBRARY]: { concurrency: 1 },
|
2023-06-01 12:32:51 +02:00
|
|
|
[QueueName.STORAGE_TEMPLATE_MIGRATION]: { concurrency: 5 },
|
2023-09-25 17:07:21 +02:00
|
|
|
[QueueName.MIGRATION]: { concurrency: 5 },
|
2023-06-01 12:32:51 +02:00
|
|
|
[QueueName.THUMBNAIL_GENERATION]: { concurrency: 5 },
|
|
|
|
[QueueName.VIDEO_CONVERSION]: { concurrency: 1 },
|
|
|
|
},
|
2023-08-25 06:15:03 +02:00
|
|
|
machineLearning: {
|
|
|
|
enabled: process.env.IMMICH_MACHINE_LEARNING_ENABLED !== 'false',
|
|
|
|
url: process.env.IMMICH_MACHINE_LEARNING_URL || 'http://immich-machine-learning:3003',
|
2023-08-29 15:58:00 +02:00
|
|
|
classification: {
|
|
|
|
enabled: true,
|
|
|
|
modelName: 'microsoft/resnet-50',
|
|
|
|
minScore: 0.9,
|
|
|
|
},
|
|
|
|
clip: {
|
|
|
|
enabled: true,
|
|
|
|
modelName: 'ViT-B-32::openai',
|
|
|
|
},
|
|
|
|
facialRecognition: {
|
|
|
|
enabled: true,
|
|
|
|
modelName: 'buffalo_l',
|
|
|
|
minScore: 0.7,
|
|
|
|
maxDistance: 0.6,
|
2023-09-18 06:05:35 +02:00
|
|
|
minFaces: 1,
|
2023-08-29 15:58:00 +02:00
|
|
|
},
|
2023-08-25 06:15:03 +02:00
|
|
|
},
|
2023-09-09 04:51:46 +02:00
|
|
|
map: {
|
|
|
|
enabled: true,
|
|
|
|
tileUrl: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
|
|
|
|
},
|
2022-12-09 21:51:42 +01:00
|
|
|
oauth: {
|
|
|
|
enabled: false,
|
|
|
|
issuerUrl: '',
|
|
|
|
clientId: '',
|
|
|
|
clientSecret: '',
|
2022-12-29 21:47:30 +01:00
|
|
|
mobileOverrideEnabled: false,
|
|
|
|
mobileRedirectUri: '',
|
2022-12-09 21:51:42 +01:00
|
|
|
scope: 'openid email profile',
|
2023-07-15 21:50:29 +02:00
|
|
|
storageLabelClaim: 'preferred_username',
|
2022-12-09 21:51:42 +01:00
|
|
|
buttonText: 'Login with OAuth',
|
|
|
|
autoRegister: true,
|
2023-01-09 22:32:58 +01:00
|
|
|
autoLaunch: false,
|
|
|
|
},
|
|
|
|
passwordLogin: {
|
|
|
|
enabled: true,
|
2022-11-15 05:39:32 +01:00
|
|
|
},
|
2022-12-16 21:26:12 +01:00
|
|
|
|
|
|
|
storageTemplate: {
|
|
|
|
template: '{{y}}/{{y}}-{{MM}}-{{dd}}/{{filename}}',
|
|
|
|
},
|
2023-08-08 16:39:51 +02:00
|
|
|
|
|
|
|
thumbnail: {
|
|
|
|
webpSize: 250,
|
|
|
|
jpegSize: 1440,
|
2023-09-03 08:21:51 +02:00
|
|
|
quality: 80,
|
|
|
|
colorspace: Colorspace.P3,
|
2023-08-08 16:39:51 +02:00
|
|
|
},
|
2022-12-09 21:51:42 +01:00
|
|
|
});
|
2022-11-15 05:39:32 +01:00
|
|
|
|
2023-08-25 06:15:03 +02:00
|
|
|
export enum FeatureFlag {
|
|
|
|
CLIP_ENCODE = 'clipEncode',
|
|
|
|
FACIAL_RECOGNITION = 'facialRecognition',
|
|
|
|
TAG_IMAGE = 'tagImage',
|
2023-09-09 04:51:46 +02:00
|
|
|
MAP = 'map',
|
2023-08-25 06:15:03 +02:00
|
|
|
SIDECAR = 'sidecar',
|
|
|
|
SEARCH = 'search',
|
|
|
|
OAUTH = 'oauth',
|
|
|
|
OAUTH_AUTO_LAUNCH = 'oauthAutoLaunch',
|
|
|
|
PASSWORD_LOGIN = 'passwordLogin',
|
2023-08-25 19:44:52 +02:00
|
|
|
CONFIG_FILE = 'configFile',
|
2023-08-25 06:15:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export type FeatureFlags = Record<FeatureFlag, boolean>;
|
|
|
|
|
2023-01-24 05:13:42 +01:00
|
|
|
const singleton = new Subject<SystemConfig>();
|
|
|
|
|
2022-11-15 05:39:32 +01:00
|
|
|
@Injectable()
|
2023-01-21 17:11:55 +01:00
|
|
|
export class SystemConfigCore {
|
|
|
|
private logger = new Logger(SystemConfigCore.name);
|
2022-12-16 21:26:12 +01:00
|
|
|
private validators: SystemConfigValidator[] = [];
|
2023-08-25 19:44:52 +02:00
|
|
|
private configCache: SystemConfig | null = null;
|
2022-12-16 21:26:12 +01:00
|
|
|
|
2023-01-24 05:13:42 +01:00
|
|
|
public config$ = singleton;
|
2022-12-16 21:26:12 +01:00
|
|
|
|
2023-01-21 17:11:55 +01:00
|
|
|
constructor(private repository: ISystemConfigRepository) {}
|
2022-11-15 05:39:32 +01:00
|
|
|
|
2023-08-25 06:15:03 +02:00
|
|
|
async requireFeature(feature: FeatureFlag) {
|
|
|
|
const hasFeature = await this.hasFeature(feature);
|
|
|
|
if (!hasFeature) {
|
|
|
|
switch (feature) {
|
|
|
|
case FeatureFlag.CLIP_ENCODE:
|
|
|
|
throw new BadRequestException('Clip encoding is not enabled');
|
|
|
|
case FeatureFlag.FACIAL_RECOGNITION:
|
|
|
|
throw new BadRequestException('Facial recognition is not enabled');
|
|
|
|
case FeatureFlag.TAG_IMAGE:
|
|
|
|
throw new BadRequestException('Image tagging is not enabled');
|
|
|
|
case FeatureFlag.SIDECAR:
|
|
|
|
throw new BadRequestException('Sidecar is not enabled');
|
|
|
|
case FeatureFlag.SEARCH:
|
|
|
|
throw new BadRequestException('Search is not enabled');
|
|
|
|
case FeatureFlag.OAUTH:
|
|
|
|
throw new BadRequestException('OAuth is not enabled');
|
|
|
|
case FeatureFlag.PASSWORD_LOGIN:
|
|
|
|
throw new BadRequestException('Password login is not enabled');
|
2023-08-25 19:44:52 +02:00
|
|
|
case FeatureFlag.CONFIG_FILE:
|
|
|
|
throw new BadRequestException('Config file is not set');
|
2023-08-25 06:15:03 +02:00
|
|
|
default:
|
|
|
|
throw new ForbiddenException(`Missing required feature: ${feature}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async hasFeature(feature: FeatureFlag) {
|
|
|
|
const features = await this.getFeatures();
|
|
|
|
return features[feature] ?? false;
|
|
|
|
}
|
|
|
|
|
|
|
|
async getFeatures(): Promise<FeatureFlags> {
|
|
|
|
const config = await this.getConfig();
|
|
|
|
const mlEnabled = config.machineLearning.enabled;
|
|
|
|
|
|
|
|
return {
|
2023-08-29 15:58:00 +02:00
|
|
|
[FeatureFlag.CLIP_ENCODE]: mlEnabled && config.machineLearning.clip.enabled,
|
|
|
|
[FeatureFlag.FACIAL_RECOGNITION]: mlEnabled && config.machineLearning.facialRecognition.enabled,
|
|
|
|
[FeatureFlag.TAG_IMAGE]: mlEnabled && config.machineLearning.classification.enabled,
|
2023-09-09 04:51:46 +02:00
|
|
|
[FeatureFlag.MAP]: config.map.enabled,
|
2023-08-25 06:15:03 +02:00
|
|
|
[FeatureFlag.SIDECAR]: true,
|
|
|
|
[FeatureFlag.SEARCH]: process.env.TYPESENSE_ENABLED !== 'false',
|
|
|
|
|
|
|
|
// TODO: use these instead of `POST oauth/config`
|
|
|
|
[FeatureFlag.OAUTH]: config.oauth.enabled,
|
|
|
|
[FeatureFlag.OAUTH_AUTO_LAUNCH]: config.oauth.autoLaunch,
|
|
|
|
[FeatureFlag.PASSWORD_LOGIN]: config.passwordLogin.enabled,
|
2023-08-25 19:44:52 +02:00
|
|
|
[FeatureFlag.CONFIG_FILE]: !!process.env.IMMICH_CONFIG_FILE,
|
2023-08-25 06:15:03 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-12-09 21:51:42 +01:00
|
|
|
public getDefaults(): SystemConfig {
|
|
|
|
return defaults;
|
2022-11-15 05:39:32 +01:00
|
|
|
}
|
|
|
|
|
2022-12-16 21:26:12 +01:00
|
|
|
public addValidator(validator: SystemConfigValidator) {
|
|
|
|
this.validators.push(validator);
|
|
|
|
}
|
|
|
|
|
2023-08-25 19:44:52 +02:00
|
|
|
public getConfig(force = false): Promise<SystemConfig> {
|
|
|
|
const configFilePath = process.env.IMMICH_CONFIG_FILE;
|
|
|
|
return configFilePath ? this.loadFromFile(configFilePath, force) : this.loadFromDatabase();
|
2022-11-15 05:39:32 +01:00
|
|
|
}
|
|
|
|
|
2022-12-16 21:26:12 +01:00
|
|
|
public async updateConfig(config: SystemConfig): Promise<SystemConfig> {
|
2023-08-25 19:44:52 +02:00
|
|
|
if (await this.hasFeature(FeatureFlag.CONFIG_FILE)) {
|
|
|
|
throw new BadRequestException('Cannot update configuration while IMMICH_CONFIG_FILE is in use');
|
|
|
|
}
|
|
|
|
|
2022-12-16 21:26:12 +01:00
|
|
|
try {
|
|
|
|
for (const validator of this.validators) {
|
|
|
|
await validator(config);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
this.logger.warn(`Unable to save system config due to a validation error: ${e}`);
|
|
|
|
throw new BadRequestException(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
|
2022-11-15 05:39:32 +01:00
|
|
|
const updates: SystemConfigEntity[] = [];
|
2022-12-09 21:51:42 +01:00
|
|
|
const deletes: SystemConfigEntity[] = [];
|
|
|
|
|
|
|
|
for (const key of Object.values(SystemConfigKey)) {
|
|
|
|
// get via dot notation
|
2023-06-01 12:32:51 +02:00
|
|
|
const item = { key, value: _.get(config, key) as SystemConfigValue };
|
2022-12-09 21:51:42 +01:00
|
|
|
const defaultValue = _.get(defaults, key);
|
|
|
|
const isMissing = !_.has(config, key);
|
2022-11-15 05:39:32 +01:00
|
|
|
|
2022-12-09 21:51:42 +01:00
|
|
|
if (isMissing || item.value === null || item.value === '' || item.value === defaultValue) {
|
2022-11-15 05:39:32 +01:00
|
|
|
deletes.push(item);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
updates.push(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (updates.length > 0) {
|
2023-01-21 17:11:55 +01:00
|
|
|
await this.repository.saveAll(updates);
|
2022-11-15 05:39:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (deletes.length > 0) {
|
2023-01-21 17:11:55 +01:00
|
|
|
await this.repository.deleteKeys(deletes.map((item) => item.key));
|
2022-11-15 05:39:32 +01:00
|
|
|
}
|
2022-12-16 21:26:12 +01:00
|
|
|
|
|
|
|
const newConfig = await this.getConfig();
|
|
|
|
|
|
|
|
this.config$.next(newConfig);
|
|
|
|
|
|
|
|
return newConfig;
|
2022-11-15 05:39:32 +01:00
|
|
|
}
|
2022-12-19 19:13:10 +01:00
|
|
|
|
|
|
|
public async refreshConfig() {
|
2023-08-25 19:44:52 +02:00
|
|
|
const newConfig = await this.getConfig(true);
|
2022-12-19 19:13:10 +01:00
|
|
|
|
|
|
|
this.config$.next(newConfig);
|
|
|
|
}
|
2023-08-25 19:44:52 +02:00
|
|
|
|
|
|
|
private async loadFromDatabase() {
|
|
|
|
const config: DeepPartial<SystemConfig> = {};
|
|
|
|
const overrides = await this.repository.load();
|
|
|
|
for (const { key, value } of overrides) {
|
|
|
|
// set via dot notation
|
|
|
|
_.set(config, key, value);
|
|
|
|
}
|
|
|
|
|
2023-08-29 15:58:00 +02:00
|
|
|
return plainToClass(SystemConfigDto, _.defaultsDeep(config, defaults));
|
2023-08-25 19:44:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private async loadFromFile(filepath: string, force = false) {
|
|
|
|
if (force || !this.configCache) {
|
|
|
|
try {
|
|
|
|
const overrides = JSON.parse((await this.repository.readFile(filepath)).toString());
|
|
|
|
const config = plainToClass(SystemConfigDto, _.defaultsDeep(overrides, defaults));
|
|
|
|
|
|
|
|
const errors = await validate(config, {
|
|
|
|
whitelist: true,
|
|
|
|
forbidNonWhitelisted: true,
|
|
|
|
forbidUnknownValues: true,
|
|
|
|
});
|
|
|
|
if (errors.length > 0) {
|
|
|
|
this.logger.error('Validation error', errors);
|
|
|
|
throw new Error(`Invalid value(s) in file: ${errors}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.configCache = config;
|
|
|
|
} catch (error: Error | any) {
|
|
|
|
this.logger.error(`Unable to load configuration file: ${filepath} due to ${error}`, error?.stack);
|
|
|
|
throw new Error('Invalid configuration file');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.configCache;
|
|
|
|
}
|
2022-11-15 05:39:32 +01:00
|
|
|
}
|