mirror of
https://github.com/KevinMidboe/immich.git
synced 2025-10-29 17:40:28 +00:00
feat(server,web): system config for admin (#959)
* feat: add admin config module for user configured config, uses it for ffmpeg * feat: add api endpoint to retrieve admin config settings and values * feat: add settings panel to admin page on web (wip) * feat: add api endpoint to update the admin config * chore: re-generate openapi spec after rebase * refactor: move from admin config to system config naming * chore: move away from UseGuards to new @Authenticated decorator * style: dark mode styling for lists and fix conflicting colors * wip: 2 column design, no edit button * refactor: system config * chore: generate open api * chore: rm broken test * chore: cleanup types * refactor: config module names Co-authored-by: Zack Pollard <zackpollard@ymail.com> Co-authored-by: Zack Pollard <zack.pollard@moonpig.com>
This commit is contained in:
@@ -1 +1,6 @@
|
||||
# Immich Server- NestJs
|
||||
## How to run migration
|
||||
|
||||
1. Attached to the container shell
|
||||
2. Run `npm run typeorm -- migration:generate ./libs/database/src/<migration-name> -d libs/database/src/config/database.config.ts`
|
||||
3. Check if the migration file makes sense
|
||||
4. Move the migration file to folder `server/libs/database/src/migrations` in your code editor.
|
||||
@@ -0,0 +1,20 @@
|
||||
import { SystemConfigKey, SystemConfigValue } from '@app/database/entities/system-config.entity';
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsEnum, IsNotEmpty, ValidateNested } from 'class-validator';
|
||||
|
||||
export class UpdateSystemConfigDto {
|
||||
@IsNotEmpty()
|
||||
@ValidateNested({ each: true })
|
||||
config!: SystemConfigItem[];
|
||||
}
|
||||
|
||||
export class SystemConfigItem {
|
||||
@IsNotEmpty()
|
||||
@IsEnum(SystemConfigKey)
|
||||
@ApiProperty({
|
||||
enum: SystemConfigKey,
|
||||
enumName: 'SystemConfigKey',
|
||||
})
|
||||
key!: SystemConfigKey;
|
||||
value!: SystemConfigValue;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import { SystemConfigKey, SystemConfigValue } from '@app/database/entities/system-config.entity';
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
|
||||
export class SystemConfigResponseDto {
|
||||
config!: SystemConfigResponseItem[];
|
||||
}
|
||||
|
||||
export class SystemConfigResponseItem {
|
||||
@ApiProperty({ type: 'string' })
|
||||
name!: string;
|
||||
|
||||
@ApiProperty({ enumName: 'SystemConfigKey', enum: SystemConfigKey })
|
||||
key!: SystemConfigKey;
|
||||
|
||||
@ApiProperty({ type: 'string' })
|
||||
value!: SystemConfigValue;
|
||||
|
||||
@ApiProperty({ type: 'string' })
|
||||
defaultValue!: SystemConfigValue;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import { Body, Controller, Get, Put, ValidationPipe } from '@nestjs/common';
|
||||
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
|
||||
import { Authenticated } from '../../decorators/authenticated.decorator';
|
||||
import { UpdateSystemConfigDto } from './dto/update-system-config';
|
||||
import { SystemConfigResponseDto } from './response-dto/system-config-response.dto';
|
||||
import { SystemConfigService } from './system-config.service';
|
||||
|
||||
@ApiTags('System Config')
|
||||
@ApiBearerAuth()
|
||||
@Authenticated({ admin: true })
|
||||
@Controller('system-config')
|
||||
export class SystemConfigController {
|
||||
constructor(private readonly systemConfigService: SystemConfigService) {}
|
||||
|
||||
@Get()
|
||||
getConfig(): Promise<SystemConfigResponseDto> {
|
||||
return this.systemConfigService.getConfig();
|
||||
}
|
||||
|
||||
@Put()
|
||||
async updateConfig(@Body(ValidationPipe) dto: UpdateSystemConfigDto): Promise<SystemConfigResponseDto> {
|
||||
return this.systemConfigService.updateConfig(dto);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import { SystemConfigEntity } from '@app/database/entities/system-config.entity';
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { ImmichConfigModule } from 'libs/immich-config/src';
|
||||
import { ImmichJwtModule } from '../../modules/immich-jwt/immich-jwt.module';
|
||||
import { SystemConfigController } from './system-config.controller';
|
||||
import { SystemConfigService } from './system-config.service';
|
||||
|
||||
@Module({
|
||||
imports: [ImmichJwtModule, ImmichConfigModule, TypeOrmModule.forFeature([SystemConfigEntity])],
|
||||
controllers: [SystemConfigController],
|
||||
providers: [SystemConfigService],
|
||||
})
|
||||
export class SystemConfigModule {}
|
||||
@@ -0,0 +1,20 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { ImmichConfigService } from 'libs/immich-config/src';
|
||||
import { UpdateSystemConfigDto } from './dto/update-system-config';
|
||||
import { SystemConfigResponseDto } from './response-dto/system-config-response.dto';
|
||||
|
||||
@Injectable()
|
||||
export class SystemConfigService {
|
||||
constructor(private immichConfigService: ImmichConfigService) {}
|
||||
|
||||
async getConfig(): Promise<SystemConfigResponseDto> {
|
||||
const config = await this.immichConfigService.getSystemConfig();
|
||||
return { config };
|
||||
}
|
||||
|
||||
async updateConfig(dto: UpdateSystemConfigDto): Promise<SystemConfigResponseDto> {
|
||||
await this.immichConfigService.updateSystemConfig(dto.config);
|
||||
const config = await this.immichConfigService.getSystemConfig();
|
||||
return { config };
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@ import { ScheduleModule } from '@nestjs/schedule';
|
||||
import { ScheduleTasksModule } from './modules/schedule-tasks/schedule-tasks.module';
|
||||
import { DatabaseModule } from '@app/database';
|
||||
import { JobModule } from './api-v1/job/job.module';
|
||||
import { SystemConfigModule } from './api-v1/system-config/system-config.module';
|
||||
import { OAuthModule } from './api-v1/oauth/oauth.module';
|
||||
|
||||
@Module({
|
||||
@@ -60,6 +61,8 @@ import { OAuthModule } from './api-v1/oauth/oauth.module';
|
||||
ScheduleTasksModule,
|
||||
|
||||
JobModule,
|
||||
|
||||
SystemConfigModule,
|
||||
],
|
||||
controllers: [AppController],
|
||||
providers: [],
|
||||
|
||||
@@ -7,8 +7,9 @@ import { UserEntity } from '@app/database/entities/user.entity';
|
||||
import { QueueNameEnum } from '@app/job/constants/queue-name.constant';
|
||||
import { BullModule } from '@nestjs/bull';
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { ImmichConfigModule } from 'libs/immich-config/src';
|
||||
import { CommunicationModule } from '../../immich/src/api-v1/communication/communication.module';
|
||||
import { MicroservicesService } from './microservices.service';
|
||||
import { AssetUploadedProcessor } from './processors/asset-uploaded.processor';
|
||||
@@ -22,6 +23,7 @@ import { VideoTranscodeProcessor } from './processors/video-transcode.processor'
|
||||
imports: [
|
||||
ConfigModule.forRoot(immichAppConfig),
|
||||
DatabaseModule,
|
||||
ImmichConfigModule,
|
||||
TypeOrmModule.forFeature([UserEntity, ExifEntity, AssetEntity, SmartInfoEntity]),
|
||||
BullModule.forRootAsync({
|
||||
useFactory: async () => ({
|
||||
@@ -96,7 +98,6 @@ import { VideoTranscodeProcessor } from './processors/video-transcode.processor'
|
||||
VideoTranscodeProcessor,
|
||||
GenerateChecksumProcessor,
|
||||
MachineLearningProcessor,
|
||||
ConfigService,
|
||||
],
|
||||
exports: [],
|
||||
})
|
||||
|
||||
@@ -9,6 +9,7 @@ import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Job } from 'bull';
|
||||
import ffmpeg from 'fluent-ffmpeg';
|
||||
import { existsSync, mkdirSync } from 'fs';
|
||||
import { ImmichConfigService } from 'libs/immich-config/src';
|
||||
import { Repository } from 'typeorm';
|
||||
|
||||
@Processor(QueueNameEnum.VIDEO_CONVERSION)
|
||||
@@ -16,6 +17,7 @@ export class VideoTranscodeProcessor {
|
||||
constructor(
|
||||
@InjectRepository(AssetEntity)
|
||||
private assetRepository: Repository<AssetEntity>,
|
||||
private immichConfigService: ImmichConfigService,
|
||||
) {}
|
||||
|
||||
@Process({ name: mp4ConversionProcessorName, concurrency: 1 })
|
||||
@@ -40,9 +42,17 @@ export class VideoTranscodeProcessor {
|
||||
}
|
||||
|
||||
async runFFMPEGPipeLine(asset: AssetEntity, savedEncodedPath: string): Promise<void> {
|
||||
const config = await this.immichConfigService.getSystemConfigMap();
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
ffmpeg(asset.originalPath)
|
||||
.outputOptions(['-crf 23', '-preset ultrafast', '-vcodec libx264', '-acodec mp3', '-vf scale=1280:-2'])
|
||||
.outputOptions([
|
||||
`-crf ${config.ffmpeg_crf}`,
|
||||
`-preset ${config.ffmpeg_preset}`,
|
||||
`-vcodec ${config.ffmpeg_target_video_codec}`,
|
||||
`-acodec ${config.ffmpeg_target_audio_codec}`,
|
||||
`-vf scale=${config.ffmpeg_target_scaling}`,
|
||||
])
|
||||
.output(savedEncodedPath)
|
||||
.on('start', () => {
|
||||
Logger.log('Start Converting Video', 'mp4Conversion');
|
||||
|
||||
File diff suppressed because one or more lines are too long
27
server/libs/database/src/entities/system-config.entity.ts
Normal file
27
server/libs/database/src/entities/system-config.entity.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { Column, Entity, PrimaryColumn } from 'typeorm';
|
||||
|
||||
@Entity('system_config')
|
||||
export class SystemConfigEntity {
|
||||
@PrimaryColumn()
|
||||
key!: SystemConfigKey;
|
||||
|
||||
@Column({ type: 'varchar', nullable: true })
|
||||
value!: SystemConfigValue;
|
||||
}
|
||||
|
||||
export type SystemConfig = SystemConfigEntity[];
|
||||
|
||||
export enum SystemConfigKey {
|
||||
FFMPEG_CRF = 'ffmpeg_crf',
|
||||
FFMPEG_PRESET = 'ffmpeg_preset',
|
||||
FFMPEG_TARGET_VIDEO_CODEC = 'ffmpeg_target_video_codec',
|
||||
FFMPEG_TARGET_AUDIO_CODEC = 'ffmpeg_target_audio_codec',
|
||||
FFMPEG_TARGET_SCALING = 'ffmpeg_target_scaling',
|
||||
}
|
||||
|
||||
export type SystemConfigValue = string | null;
|
||||
|
||||
export interface SystemConfigItem {
|
||||
key: SystemConfigKey;
|
||||
value: SystemConfigValue;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { MigrationInterface, QueryRunner } from 'typeorm';
|
||||
|
||||
export class CreateSystemConfigTable1665540663419 implements MigrationInterface {
|
||||
name = 'CreateSystemConfigTable1665540663419';
|
||||
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(
|
||||
`CREATE TABLE "system_config" ("key" character varying NOT NULL, "value" character varying, CONSTRAINT "PK_aab69295b445016f56731f4d535" PRIMARY KEY ("key"))`,
|
||||
);
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`DROP TABLE "system_config"`);
|
||||
}
|
||||
}
|
||||
11
server/libs/immich-config/src/immich-config.module.ts
Normal file
11
server/libs/immich-config/src/immich-config.module.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { SystemConfigEntity } from '@app/database/entities/system-config.entity';
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { ImmichConfigService } from './immich-config.service';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([SystemConfigEntity])],
|
||||
providers: [ImmichConfigService],
|
||||
exports: [ImmichConfigService],
|
||||
})
|
||||
export class ImmichConfigModule {}
|
||||
97
server/libs/immich-config/src/immich-config.service.ts
Normal file
97
server/libs/immich-config/src/immich-config.service.ts
Normal file
@@ -0,0 +1,97 @@
|
||||
import { SystemConfigEntity, SystemConfigKey, SystemConfigValue } from '@app/database/entities/system-config.entity';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { In, Repository } from 'typeorm';
|
||||
|
||||
type SystemConfigMap = Record<SystemConfigKey, SystemConfigValue>;
|
||||
|
||||
const configDefaults: Record<SystemConfigKey, { name: string; value: SystemConfigValue }> = {
|
||||
[SystemConfigKey.FFMPEG_CRF]: {
|
||||
name: 'FFmpeg Constant Rate Factor (-crf)',
|
||||
value: '23',
|
||||
},
|
||||
[SystemConfigKey.FFMPEG_PRESET]: {
|
||||
name: 'FFmpeg preset (-preset)',
|
||||
value: 'ultrafast',
|
||||
},
|
||||
[SystemConfigKey.FFMPEG_TARGET_VIDEO_CODEC]: {
|
||||
name: 'FFmpeg target video codec (-vcodec)',
|
||||
value: 'libx264',
|
||||
},
|
||||
[SystemConfigKey.FFMPEG_TARGET_AUDIO_CODEC]: {
|
||||
name: 'FFmpeg target audio codec (-acodec)',
|
||||
value: 'mp3',
|
||||
},
|
||||
[SystemConfigKey.FFMPEG_TARGET_SCALING]: {
|
||||
name: 'FFmpeg target scaling (-vf scale=)',
|
||||
value: '1280:-2',
|
||||
},
|
||||
};
|
||||
|
||||
@Injectable()
|
||||
export class ImmichConfigService {
|
||||
constructor(
|
||||
@InjectRepository(SystemConfigEntity)
|
||||
private systemConfigRepository: Repository<SystemConfigEntity>,
|
||||
) {}
|
||||
|
||||
public async getSystemConfig() {
|
||||
const items = this._getDefaults();
|
||||
|
||||
// override default values
|
||||
const overrides = await this.systemConfigRepository.find();
|
||||
for (const override of overrides) {
|
||||
const item = items.find((_item) => _item.key === override.key);
|
||||
if (item) {
|
||||
item.value = override.value;
|
||||
}
|
||||
}
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
public async getSystemConfigMap(): Promise<SystemConfigMap> {
|
||||
const items = await this.getSystemConfig();
|
||||
const map: Partial<SystemConfigMap> = {};
|
||||
|
||||
for (const { key, value } of items) {
|
||||
map[key] = value;
|
||||
}
|
||||
|
||||
return map as SystemConfigMap;
|
||||
}
|
||||
|
||||
public async updateSystemConfig(items: SystemConfigEntity[]): Promise<void> {
|
||||
const deletes: SystemConfigEntity[] = [];
|
||||
const updates: SystemConfigEntity[] = [];
|
||||
|
||||
for (const item of items) {
|
||||
if (item.value === null || item.value === this._getDefaultValue(item.key)) {
|
||||
deletes.push(item);
|
||||
continue;
|
||||
}
|
||||
|
||||
updates.push(item);
|
||||
}
|
||||
|
||||
if (updates.length > 0) {
|
||||
await this.systemConfigRepository.save(updates);
|
||||
}
|
||||
|
||||
if (deletes.length > 0) {
|
||||
await this.systemConfigRepository.delete({ key: In(deletes.map((item) => item.key)) });
|
||||
}
|
||||
}
|
||||
|
||||
private _getDefaults() {
|
||||
return Object.values(SystemConfigKey).map((key) => ({
|
||||
key,
|
||||
defaultValue: configDefaults[key].value,
|
||||
...configDefaults[key],
|
||||
}));
|
||||
}
|
||||
|
||||
private _getDefaultValue(key: SystemConfigKey) {
|
||||
return this._getDefaults().find((item) => item.key === key)?.value || null;
|
||||
}
|
||||
}
|
||||
2
server/libs/immich-config/src/index.ts
Normal file
2
server/libs/immich-config/src/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from './immich-config.module';
|
||||
export * from './immich-config.service';
|
||||
9
server/libs/immich-config/tsconfig.lib.json
Normal file
9
server/libs/immich-config/tsconfig.lib.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"declaration": true,
|
||||
"outDir": "../../dist/libs/immich-config"
|
||||
},
|
||||
"include": ["src/**/*"],
|
||||
"exclude": ["node_modules", "dist", "test", "**/*spec.ts"]
|
||||
}
|
||||
@@ -70,6 +70,15 @@
|
||||
"compilerOptions": {
|
||||
"tsConfigPath": "libs/job/tsconfig.lib.json"
|
||||
}
|
||||
},
|
||||
"system-config": {
|
||||
"type": "library",
|
||||
"root": "libs/system-config",
|
||||
"entryFile": "index",
|
||||
"sourceRoot": "libs/system-config/src",
|
||||
"compilerOptions": {
|
||||
"tsConfigPath": "libs/system-config/tsconfig.lib.json"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@
|
||||
"build": "nest build immich && nest build microservices && nest build cli",
|
||||
"format": "prettier --write \"apps/**/*.ts\" \"libs/**/*.ts\"",
|
||||
"start": "nest start",
|
||||
"nest": "nest",
|
||||
"start:dev": "nest start --watch",
|
||||
"start:debug": "nest start --debug 0.0.0.0:9230 --watch",
|
||||
"start:prod": "node dist/main",
|
||||
@@ -139,7 +140,8 @@
|
||||
"@app/database/config/(.*)": "<rootDir>/libs/database/src/config/$1",
|
||||
"@app/database/config": "<rootDir>/libs/database/src/config",
|
||||
"@app/common": "<rootDir>/libs/common/src",
|
||||
"^@app/job(|/.*)$": "<rootDir>/libs/job/src/$1"
|
||||
"^@app/job(|/.*)$": "<rootDir>/libs/job/src/$1",
|
||||
"^@app/system-config(|/.*)$": "<rootDir>/libs/system-config/src/$1"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,29 +16,15 @@
|
||||
"esModuleInterop": true,
|
||||
"baseUrl": "./",
|
||||
"paths": {
|
||||
"@app/common": [
|
||||
"libs/common/src"
|
||||
],
|
||||
"@app/common/*": [
|
||||
"libs/common/src/*"
|
||||
],
|
||||
"@app/database": [
|
||||
"libs/database/src"
|
||||
],
|
||||
"@app/database/*": [
|
||||
"libs/database/src/*"
|
||||
],
|
||||
"@app/job": [
|
||||
"libs/job/src"
|
||||
],
|
||||
"@app/job/*": [
|
||||
"libs/job/src/*"
|
||||
]
|
||||
"@app/common": ["libs/common/src"],
|
||||
"@app/common/*": ["libs/common/src/*"],
|
||||
"@app/database": ["libs/database/src"],
|
||||
"@app/database/*": ["libs/database/src/*"],
|
||||
"@app/job": ["libs/job/src"],
|
||||
"@app/job/*": ["libs/job/src/*"],
|
||||
"@app/system-config": ["libs/immich-config/src"],
|
||||
"@app/system-config/*": ["libs/immich-config/src/*"]
|
||||
}
|
||||
},
|
||||
"exclude": [
|
||||
"dist",
|
||||
"node_modules",
|
||||
"upload"
|
||||
]
|
||||
}
|
||||
"exclude": ["dist", "node_modules", "upload"]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user