2023-06-01 12:32:51 +02:00
|
|
|
import {
|
2023-07-09 04:43:11 +02:00
|
|
|
AudioCodec,
|
2023-06-01 12:32:51 +02:00
|
|
|
SystemConfig,
|
|
|
|
SystemConfigEntity,
|
|
|
|
SystemConfigKey,
|
|
|
|
SystemConfigValue,
|
2023-07-09 04:43:11 +02:00
|
|
|
TranscodePolicy,
|
|
|
|
VideoCodec,
|
2023-06-01 12:32:51 +02:00
|
|
|
} from '@app/infra/entities';
|
2022-12-16 21:26:12 +01:00
|
|
|
import { BadRequestException, Injectable, Logger } from '@nestjs/common';
|
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-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',
|
|
|
|
twoPass: false,
|
2023-07-09 04:43:11 +02:00
|
|
|
transcode: TranscodePolicy.REQUIRED,
|
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 },
|
|
|
|
[QueueName.STORAGE_TEMPLATE_MIGRATION]: { concurrency: 5 },
|
|
|
|
[QueueName.THUMBNAIL_GENERATION]: { concurrency: 5 },
|
|
|
|
[QueueName.VIDEO_CONVERSION]: { concurrency: 1 },
|
|
|
|
},
|
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}}',
|
|
|
|
},
|
2022-12-09 21:51:42 +01:00
|
|
|
});
|
2022-11-15 05:39:32 +01:00
|
|
|
|
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-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
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-12-09 21:51:42 +01:00
|
|
|
public async getConfig() {
|
2023-01-21 17:11:55 +01:00
|
|
|
const overrides = await this.repository.load();
|
2022-12-09 21:51:42 +01:00
|
|
|
const config: DeepPartial<SystemConfig> = {};
|
|
|
|
for (const { key, value } of overrides) {
|
|
|
|
// set via dot notation
|
|
|
|
_.set(config, key, value);
|
2022-11-15 05:39:32 +01:00
|
|
|
}
|
|
|
|
|
2022-12-09 21:51:42 +01:00
|
|
|
return _.defaultsDeep(config, defaults) as SystemConfig;
|
2022-11-15 05:39:32 +01:00
|
|
|
}
|
|
|
|
|
2022-12-16 21:26:12 +01:00
|
|
|
public async updateConfig(config: SystemConfig): Promise<SystemConfig> {
|
|
|
|
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() {
|
|
|
|
const newConfig = await this.getConfig();
|
|
|
|
|
|
|
|
this.config$.next(newConfig);
|
|
|
|
}
|
2022-11-15 05:39:32 +01:00
|
|
|
}
|