mirror of
https://github.com/immich-app/immich.git
synced 2025-01-24 12:42:44 +01:00
f44fa45aa0
* add unicorn to eslint * fix lint errors for cli * fix merge * fix album name extraction * Update cli/src/commands/upload.command.ts Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> * es2k23 * use lowercase os * return undefined album name * fix bug in asset response dto * auto fix issues * fix server code style * es2022 and formatting * fix compilation error * fix test * fix config load * fix last lint errors * set string type * bump ts * start work on web * web formatting * Fix UUIDParamDto as UUIDParamDto * fix library service lint * fix web errors * fix errors * formatting * wip * lints fixed * web can now start * alphabetical package json * rename error * chore: clean up --------- Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> Co-authored-by: Jason Rasmussen <jrasm91@gmail.com>
138 lines
4.7 KiB
TypeScript
138 lines
4.7 KiB
TypeScript
import { LogLevel, SystemConfig } from '@app/infra/entities';
|
|
import { ImmichLogger } from '@app/infra/logger';
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
import { instanceToPlain } from 'class-transformer';
|
|
import _ from 'lodash';
|
|
import {
|
|
ClientEvent,
|
|
ICommunicationRepository,
|
|
ISmartInfoRepository,
|
|
ISystemConfigRepository,
|
|
ServerEvent,
|
|
} from '../repositories';
|
|
import { SystemConfigDto, mapConfig } from './dto/system-config.dto';
|
|
import { SystemConfigTemplateStorageOptionDto } from './response-dto/system-config-template-storage-option.dto';
|
|
import {
|
|
supportedDayTokens,
|
|
supportedHourTokens,
|
|
supportedMinuteTokens,
|
|
supportedMonthTokens,
|
|
supportedPresetTokens,
|
|
supportedSecondTokens,
|
|
supportedWeekTokens,
|
|
supportedYearTokens,
|
|
} from './system-config.constants';
|
|
import { SystemConfigCore, SystemConfigValidator } from './system-config.core';
|
|
|
|
@Injectable()
|
|
export class SystemConfigService {
|
|
private logger = new ImmichLogger(SystemConfigService.name);
|
|
private core: SystemConfigCore;
|
|
|
|
constructor(
|
|
@Inject(ISystemConfigRepository) private repository: ISystemConfigRepository,
|
|
@Inject(ICommunicationRepository) private communicationRepository: ICommunicationRepository,
|
|
@Inject(ISmartInfoRepository) private smartInfoRepository: ISmartInfoRepository,
|
|
) {
|
|
this.core = SystemConfigCore.create(repository);
|
|
this.communicationRepository.on(ServerEvent.CONFIG_UPDATE, () => this.handleConfigUpdate());
|
|
this.core.config$.subscribe((config) => this.setLogLevel(config));
|
|
this.core.addValidator((newConfig, oldConfig) => this.validateConfig(newConfig, oldConfig));
|
|
}
|
|
|
|
async init() {
|
|
const config = await this.core.getConfig();
|
|
this.config$.next(config);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
async updateConfig(dto: SystemConfigDto): Promise<SystemConfigDto> {
|
|
const oldConfig = await this.core.getConfig();
|
|
const newConfig = await this.core.updateConfig(dto);
|
|
|
|
this.communicationRepository.broadcast(ClientEvent.CONFIG_UPDATE, {});
|
|
this.communicationRepository.sendServerEvent(ServerEvent.CONFIG_UPDATE);
|
|
|
|
if (oldConfig.machineLearning.clip.modelName !== newConfig.machineLearning.clip.modelName) {
|
|
await this.smartInfoRepository.init(newConfig.machineLearning.clip.modelName);
|
|
}
|
|
return mapConfig(newConfig);
|
|
}
|
|
|
|
// this is only used by the cli on config change, and it's not actually needed anymore
|
|
async refreshConfig() {
|
|
this.communicationRepository.sendServerEvent(ServerEvent.CONFIG_UPDATE);
|
|
await this.core.refreshConfig();
|
|
return true;
|
|
}
|
|
|
|
addValidator(validator: SystemConfigValidator) {
|
|
this.core.addValidator(validator);
|
|
}
|
|
|
|
getStorageTemplateOptions(): SystemConfigTemplateStorageOptionDto {
|
|
const options = new SystemConfigTemplateStorageOptionDto();
|
|
|
|
options.dayOptions = supportedDayTokens;
|
|
options.weekOptions = supportedWeekTokens;
|
|
options.monthOptions = supportedMonthTokens;
|
|
options.yearOptions = supportedYearTokens;
|
|
options.hourOptions = supportedHourTokens;
|
|
options.secondOptions = supportedSecondTokens;
|
|
options.minuteOptions = supportedMinuteTokens;
|
|
options.presetOptions = supportedPresetTokens;
|
|
|
|
return options;
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
return JSON.parse(await this.repository.readFile(`./resources/style-${theme}.json`));
|
|
}
|
|
|
|
async getCustomCss(): Promise<string> {
|
|
const { theme } = await this.core.getConfig();
|
|
return theme.customCss;
|
|
}
|
|
|
|
private async handleConfigUpdate() {
|
|
await this.core.refreshConfig();
|
|
}
|
|
|
|
private async setLogLevel({ logging }: SystemConfig) {
|
|
const envLevel = this.getEnvLogLevel();
|
|
const configLevel = logging.enabled ? logging.level : false;
|
|
const level = envLevel ?? configLevel;
|
|
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;
|
|
}
|
|
|
|
private async validateConfig(newConfig: SystemConfig, oldConfig: SystemConfig) {
|
|
if (!_.isEqual(instanceToPlain(newConfig.logging), oldConfig.logging) && this.getEnvLogLevel()) {
|
|
throw new Error('Logging cannot be changed while the environment variable LOG_LEVEL is set.');
|
|
}
|
|
}
|
|
}
|