2024-03-17 20:16:02 +01:00
|
|
|
import { BadRequestException, Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { OnEvent } from '@nestjs/event-emitter';
|
2023-12-14 17:55:40 +01:00
|
|
|
import { instanceToPlain } from 'class-transformer';
|
|
|
|
import _ from 'lodash';
|
2024-03-20 21:20:38 +01:00
|
|
|
import { SystemConfigCore } from 'src/cores/system-config.core';
|
2024-03-20 19:32:04 +01:00
|
|
|
import { SystemConfigDto, mapConfig } from 'src/domain/system-config/dto/system-config.dto';
|
|
|
|
import { SystemConfigTemplateStorageOptionDto } from 'src/domain/system-config/response-dto/system-config-template-storage-option.dto';
|
2023-01-21 17:11:55 +01:00
|
|
|
import {
|
|
|
|
supportedDayTokens,
|
|
|
|
supportedHourTokens,
|
|
|
|
supportedMinuteTokens,
|
|
|
|
supportedMonthTokens,
|
|
|
|
supportedPresetTokens,
|
|
|
|
supportedSecondTokens,
|
2023-09-28 19:47:31 +02:00
|
|
|
supportedWeekTokens,
|
2023-01-21 17:11:55 +01:00
|
|
|
supportedYearTokens,
|
2024-03-20 19:32:04 +01:00
|
|
|
} from 'src/domain/system-config/system-config.constants';
|
|
|
|
import { LogLevel, SystemConfig } from 'src/infra/entities/system-config.entity';
|
|
|
|
import { ImmichLogger } from 'src/infra/logger';
|
2024-03-20 21:42:58 +01:00
|
|
|
import {
|
|
|
|
ClientEvent,
|
|
|
|
ICommunicationRepository,
|
|
|
|
InternalEvent,
|
|
|
|
InternalEventMap,
|
|
|
|
ServerEvent,
|
|
|
|
} from 'src/interfaces/communication.repository';
|
|
|
|
import { ISearchRepository } from 'src/interfaces/search.repository';
|
|
|
|
import { ISystemConfigRepository } from 'src/interfaces/system-config.repository';
|
2023-01-21 17:11:55 +01:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class SystemConfigService {
|
2023-12-14 17:55:40 +01:00
|
|
|
private logger = new ImmichLogger(SystemConfigService.name);
|
2023-01-21 17:11:55 +01:00
|
|
|
private core: SystemConfigCore;
|
2023-12-13 18:23:51 +01:00
|
|
|
|
2023-01-21 17:11:55 +01:00
|
|
|
constructor(
|
2023-11-09 17:10:56 +01:00
|
|
|
@Inject(ISystemConfigRepository) private repository: ISystemConfigRepository,
|
2023-10-06 09:01:14 +02:00
|
|
|
@Inject(ICommunicationRepository) private communicationRepository: ICommunicationRepository,
|
2024-02-13 02:50:47 +01:00
|
|
|
@Inject(ISearchRepository) private smartInfoRepository: ISearchRepository,
|
2023-01-21 17:11:55 +01:00
|
|
|
) {
|
2023-10-09 02:51:03 +02:00
|
|
|
this.core = SystemConfigCore.create(repository);
|
2023-12-13 18:23:51 +01:00
|
|
|
this.communicationRepository.on(ServerEvent.CONFIG_UPDATE, () => this.handleConfigUpdate());
|
2023-12-14 17:55:40 +01:00
|
|
|
this.core.config$.subscribe((config) => this.setLogLevel(config));
|
|
|
|
}
|
|
|
|
|
|
|
|
async init() {
|
|
|
|
const config = await this.core.getConfig();
|
2024-01-01 19:16:44 +01:00
|
|
|
this.config$.next(config);
|
2023-01-21 17:11:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
get config$() {
|
|
|
|
return this.core.config$;
|
|
|
|
}
|
|
|
|
|
|
|
|
async getConfig(): Promise<SystemConfigDto> {
|
|
|
|
const config = await this.core.getConfig();
|
|
|
|
return mapConfig(config);
|
|
|
|
}
|
|
|
|
|
|
|
|
getDefaults(): SystemConfigDto {
|
|
|
|
const config = this.core.getDefaults();
|
|
|
|
return mapConfig(config);
|
|
|
|
}
|
|
|
|
|
2024-03-17 20:16:02 +01:00
|
|
|
@OnEvent(InternalEvent.VALIDATE_CONFIG)
|
|
|
|
validateConfig({ newConfig, oldConfig }: InternalEventMap[InternalEvent.VALIDATE_CONFIG]) {
|
|
|
|
if (!_.isEqual(instanceToPlain(newConfig.logging), oldConfig.logging) && this.getEnvLogLevel()) {
|
|
|
|
throw new Error('Logging cannot be changed while the environment variable LOG_LEVEL is set.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-21 17:11:55 +01:00
|
|
|
async updateConfig(dto: SystemConfigDto): Promise<SystemConfigDto> {
|
2023-12-08 17:15:46 +01:00
|
|
|
const oldConfig = await this.core.getConfig();
|
2024-03-17 20:16:02 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
await this.communicationRepository.emitAsync(InternalEvent.VALIDATE_CONFIG, { newConfig: dto, oldConfig });
|
|
|
|
} catch (error) {
|
|
|
|
this.logger.warn(`Unable to save system config due to a validation error: ${error}`);
|
|
|
|
throw new BadRequestException(error instanceof Error ? error.message : error);
|
|
|
|
}
|
|
|
|
|
2023-12-08 17:15:46 +01:00
|
|
|
const newConfig = await this.core.updateConfig(dto);
|
2023-12-13 18:23:51 +01:00
|
|
|
|
|
|
|
this.communicationRepository.broadcast(ClientEvent.CONFIG_UPDATE, {});
|
|
|
|
this.communicationRepository.sendServerEvent(ServerEvent.CONFIG_UPDATE);
|
|
|
|
|
2023-12-08 17:15:46 +01:00
|
|
|
if (oldConfig.machineLearning.clip.modelName !== newConfig.machineLearning.clip.modelName) {
|
|
|
|
await this.smartInfoRepository.init(newConfig.machineLearning.clip.modelName);
|
|
|
|
}
|
|
|
|
return mapConfig(newConfig);
|
2023-01-21 17:11:55 +01:00
|
|
|
}
|
|
|
|
|
2023-12-13 18:23:51 +01:00
|
|
|
// this is only used by the cli on config change, and it's not actually needed anymore
|
2023-01-21 17:11:55 +01:00
|
|
|
async refreshConfig() {
|
2023-12-13 18:23:51 +01:00
|
|
|
this.communicationRepository.sendServerEvent(ServerEvent.CONFIG_UPDATE);
|
2023-01-21 17:11:55 +01:00
|
|
|
await this.core.refreshConfig();
|
2023-05-26 21:43:24 +02:00
|
|
|
return true;
|
2023-01-21 17:11:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getStorageTemplateOptions(): SystemConfigTemplateStorageOptionDto {
|
|
|
|
const options = new SystemConfigTemplateStorageOptionDto();
|
|
|
|
|
|
|
|
options.dayOptions = supportedDayTokens;
|
2023-09-28 19:47:31 +02:00
|
|
|
options.weekOptions = supportedWeekTokens;
|
2023-01-21 17:11:55 +01:00
|
|
|
options.monthOptions = supportedMonthTokens;
|
|
|
|
options.yearOptions = supportedYearTokens;
|
|
|
|
options.hourOptions = supportedHourTokens;
|
|
|
|
options.secondOptions = supportedSecondTokens;
|
|
|
|
options.minuteOptions = supportedMinuteTokens;
|
|
|
|
options.presetOptions = supportedPresetTokens;
|
|
|
|
|
|
|
|
return options;
|
|
|
|
}
|
2023-11-09 17:10:56 +01:00
|
|
|
|
|
|
|
async getMapStyle(theme: 'light' | 'dark') {
|
|
|
|
const { map } = await this.getConfig();
|
|
|
|
const styleUrl = theme === 'dark' ? map.darkStyle : map.lightStyle;
|
|
|
|
|
|
|
|
if (styleUrl) {
|
|
|
|
return this.repository.fetchStyle(styleUrl);
|
|
|
|
}
|
|
|
|
|
2023-11-25 19:53:30 +01:00
|
|
|
return JSON.parse(await this.repository.readFile(`./resources/style-${theme}.json`));
|
2023-11-09 17:10:56 +01:00
|
|
|
}
|
2023-11-18 05:13:36 +01:00
|
|
|
|
|
|
|
async getCustomCss(): Promise<string> {
|
|
|
|
const { theme } = await this.core.getConfig();
|
|
|
|
return theme.customCss;
|
|
|
|
}
|
2023-12-13 18:23:51 +01:00
|
|
|
|
|
|
|
private async handleConfigUpdate() {
|
|
|
|
await this.core.refreshConfig();
|
|
|
|
}
|
2023-12-14 17:55:40 +01:00
|
|
|
|
2024-03-05 23:23:06 +01:00
|
|
|
private setLogLevel({ logging }: SystemConfig) {
|
2023-12-14 17:55:40 +01:00
|
|
|
const envLevel = this.getEnvLogLevel();
|
|
|
|
const configLevel = logging.enabled ? logging.level : false;
|
2024-02-02 04:18:00 +01:00
|
|
|
const level = envLevel ?? configLevel;
|
2023-12-14 17:55:40 +01:00
|
|
|
ImmichLogger.setLogLevel(level);
|
|
|
|
this.logger.log(`LogLevel=${level} ${envLevel ? '(set via LOG_LEVEL)' : '(set via system config)'}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
private getEnvLogLevel() {
|
|
|
|
return process.env.LOG_LEVEL as LogLevel;
|
|
|
|
}
|
2023-01-21 17:11:55 +01:00
|
|
|
}
|