Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(server): keep system config transformations #13796

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions server/src/services/system-config.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,24 @@ describe(SystemConfigService.name, () => {
expect(systemMock.readFile).toHaveBeenCalledWith('immich-config.json');
});

it('should transform booleans', async () => {
configMock.getEnv.mockReturnValue(mockEnvData({ configFile: 'immich-config.json' }));
systemMock.readFile.mockResolvedValue(JSON.stringify({ ffmpeg: { twoPass: 'false' } }));

await expect(sut.getSystemConfig()).resolves.toMatchObject({
ffmpeg: expect.objectContaining({ twoPass: false }),
});
});

it('should transform numbers', async () => {
configMock.getEnv.mockReturnValue(mockEnvData({ configFile: 'immich-config.json' }));
systemMock.readFile.mockResolvedValue(JSON.stringify({ ffmpeg: { threads: '42' } }));

await expect(sut.getSystemConfig()).resolves.toMatchObject({
ffmpeg: expect.objectContaining({ threads: 42 }),
});
});

it('should log errors with the config file', async () => {
configMock.getEnv.mockReturnValue(mockEnvData({ configFile: 'immich-config.json' }));

Expand Down
14 changes: 9 additions & 5 deletions server/src/utils/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import AsyncLock from 'async-lock';
import { plainToInstance } from 'class-transformer';
import { instanceToPlain, plainToInstance } from 'class-transformer';
import { validate } from 'class-validator';
import { load as loadYaml } from 'js-yaml';
import * as _ from 'lodash';
Expand Down Expand Up @@ -87,13 +87,13 @@ const buildConfig = async (repos: RepoDeps) => {
: await metadataRepo.get(SystemMetadataKey.SYSTEM_CONFIG);

// merge with defaults
const config = _.cloneDeep(defaults);
const rawConfig = _.cloneDeep(defaults);
for (const property of getKeysDeep(partial)) {
_.set(config, property, _.get(partial, property));
_.set(rawConfig, property, _.get(partial, property));
}

// check for extra properties
const unknownKeys = _.cloneDeep(config);
const unknownKeys = _.cloneDeep(rawConfig);
for (const property of getKeysDeep(defaults)) {
unsetDeep(unknownKeys, property);
}
Expand All @@ -103,7 +103,8 @@ const buildConfig = async (repos: RepoDeps) => {
}

// validate full config
const errors = await validate(plainToInstance(SystemConfigDto, config));
const instance = plainToInstance(SystemConfigDto, rawConfig);
const errors = await validate(instance);
if (errors.length > 0) {
if (configFile) {
throw new Error(`Invalid value(s) in file: ${errors}`);
Expand All @@ -112,6 +113,9 @@ const buildConfig = async (repos: RepoDeps) => {
}
}

// return config with class-transform changes
const config = instanceToPlain(instance) as SystemConfig;

if (config.server.externalDomain.length > 0) {
config.server.externalDomain = new URL(config.server.externalDomain).origin;
}
Expand Down
Loading