2017-07-08 18:43:42 +08:00
|
|
|
import {NextFunction, Request, Response} from "express";
|
2017-07-15 18:47:11 +08:00
|
|
|
import {ErrorCodes, ErrorDTO} from "../../common/entities/Error";
|
2017-07-08 18:43:42 +08:00
|
|
|
import {ObjectManagerRepository} from "../model/ObjectManagerRepository";
|
|
|
|
import {Logger} from "../Logger";
|
|
|
|
import {MySQLConnection} from "../model/mysql/MySQLConnection";
|
2017-07-15 18:47:11 +08:00
|
|
|
import {DataBaseConfig, DatabaseType, ThumbnailConfig} from "../../common/config/private/IPrivateConfig";
|
2017-07-08 18:43:42 +08:00
|
|
|
import {Config} from "../../common/config/private/Config";
|
2017-07-14 05:39:09 +08:00
|
|
|
import {ConfigDiagnostics} from "../model/ConfigDiagnostics";
|
2017-07-15 18:47:11 +08:00
|
|
|
import {ClientConfig} from "../../common/config/public/ConfigClass";
|
|
|
|
import set = Reflect.set;
|
2017-07-08 18:43:42 +08:00
|
|
|
|
|
|
|
|
|
|
|
const LOG_TAG = "[AdminMWs]";
|
|
|
|
export class AdminMWs {
|
|
|
|
|
|
|
|
|
|
|
|
public static async updateDatabaseSettings(req: Request, res: Response, next: NextFunction) {
|
|
|
|
|
2017-07-14 05:39:09 +08:00
|
|
|
if ((typeof req.body === 'undefined') || (typeof req.body.settings === 'undefined')) {
|
2017-07-15 18:47:11 +08:00
|
|
|
return next(new ErrorDTO(ErrorCodes.INPUT_ERROR, "settings is needed"));
|
2017-07-08 18:43:42 +08:00
|
|
|
}
|
|
|
|
|
2017-07-14 05:39:09 +08:00
|
|
|
const databaseSettings = <DataBaseConfig>req.body.settings;
|
2017-07-08 18:43:42 +08:00
|
|
|
|
|
|
|
try {
|
2017-07-13 00:31:19 +08:00
|
|
|
if (databaseSettings.type == DatabaseType.mysql) {
|
2017-07-08 18:43:42 +08:00
|
|
|
await MySQLConnection.tryConnection(databaseSettings);
|
|
|
|
}
|
|
|
|
Config.Server.database = databaseSettings;
|
2017-07-14 05:39:09 +08:00
|
|
|
//only updating explicitly set config (not saving config set by the diagnostics)
|
|
|
|
const original = Config.original();
|
|
|
|
original.Server.database = databaseSettings;
|
|
|
|
original.save();
|
|
|
|
await ConfigDiagnostics.runDiagnostics();
|
2017-07-08 18:43:42 +08:00
|
|
|
Logger.info(LOG_TAG, "new config:");
|
|
|
|
Logger.info(LOG_TAG, JSON.stringify(Config, null, '\t'));
|
|
|
|
|
|
|
|
ObjectManagerRepository.reset();
|
|
|
|
if (Config.Server.database.type == DatabaseType.mysql) {
|
|
|
|
await ObjectManagerRepository.InitMySQLManagers();
|
|
|
|
} else {
|
|
|
|
await ObjectManagerRepository.InitMemoryManagers();
|
|
|
|
}
|
|
|
|
|
|
|
|
return next();
|
|
|
|
} catch (err) {
|
2017-07-15 18:47:11 +08:00
|
|
|
return next(new ErrorDTO(ErrorCodes.SETTINGS_ERROR, "ErrorDTO saving database settings", err));
|
2017-07-08 18:43:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-15 18:47:11 +08:00
|
|
|
public static async updateMapSettings(req: Request, res: Response, next: NextFunction) {
|
2017-07-14 05:39:09 +08:00
|
|
|
if ((typeof req.body === 'undefined') || (typeof req.body.settings === 'undefined')) {
|
2017-07-15 18:47:11 +08:00
|
|
|
return next(new ErrorDTO(ErrorCodes.INPUT_ERROR, "settings is needed"));
|
2017-07-08 18:43:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2017-07-15 18:47:11 +08:00
|
|
|
await ConfigDiagnostics.testMapConfig(<ClientConfig.MapConfig>req.body.settings);
|
|
|
|
|
|
|
|
Config.Client.Map = <ClientConfig.MapConfig>req.body.settings;
|
|
|
|
//only updating explicitly set config (not saving config set by the diagnostics)
|
|
|
|
const original = Config.original();
|
|
|
|
original.Client.Map = <ClientConfig.MapConfig>req.body.settings;
|
|
|
|
original.save();
|
|
|
|
await ConfigDiagnostics.runDiagnostics();
|
|
|
|
Logger.info(LOG_TAG, "new config:");
|
|
|
|
Logger.info(LOG_TAG, JSON.stringify(Config, null, '\t'));
|
2017-07-08 18:43:42 +08:00
|
|
|
return next();
|
|
|
|
} catch (err) {
|
2017-07-15 18:47:11 +08:00
|
|
|
return next(new ErrorDTO(ErrorCodes.SETTINGS_ERROR, "Settings error: " + JSON.stringify(err, null, ' '), err));
|
2017-07-08 18:43:42 +08:00
|
|
|
}
|
|
|
|
}
|
2017-07-14 05:39:09 +08:00
|
|
|
|
2017-07-15 20:27:12 +08:00
|
|
|
public static async updateSearchSettings(req: Request, res: Response, next: NextFunction) {
|
|
|
|
if ((typeof req.body === 'undefined') || (typeof req.body.settings === 'undefined')) {
|
|
|
|
return next(new ErrorDTO(ErrorCodes.INPUT_ERROR, "settings is needed"));
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
//only updating explicitly set config (not saving config set by the diagnostics)
|
|
|
|
const original = Config.original();
|
|
|
|
await ConfigDiagnostics.testSearchConfig(<ClientConfig.SearchConfig>req.body.settings, original);
|
|
|
|
|
|
|
|
Config.Client.Search = <ClientConfig.SearchConfig>req.body.settings;
|
|
|
|
original.Client.Search = <ClientConfig.SearchConfig>req.body.settings;
|
|
|
|
original.save();
|
|
|
|
await ConfigDiagnostics.runDiagnostics();
|
|
|
|
Logger.info(LOG_TAG, "new config:");
|
|
|
|
Logger.info(LOG_TAG, JSON.stringify(Config, null, '\t'));
|
|
|
|
return next();
|
|
|
|
} catch (err) {
|
|
|
|
return next(new ErrorDTO(ErrorCodes.SETTINGS_ERROR, "Settings error: " + JSON.stringify(err, null, ' '), err));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-15 18:47:11 +08:00
|
|
|
public static async updateAuthenticationSettings(req: Request, res: Response, next: NextFunction) {
|
2017-07-14 05:39:09 +08:00
|
|
|
if ((typeof req.body === 'undefined') || (typeof req.body.settings === 'undefined')) {
|
2017-07-15 18:47:11 +08:00
|
|
|
return next(new ErrorDTO(ErrorCodes.INPUT_ERROR, "settings is needed"));
|
2017-07-14 05:39:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2017-07-15 18:47:11 +08:00
|
|
|
Config.Client.authenticationRequired = <boolean>req.body.settings;
|
2017-07-14 05:39:09 +08:00
|
|
|
//only updating explicitly set config (not saving config set by the diagnostics)
|
|
|
|
const original = Config.original();
|
2017-07-15 18:47:11 +08:00
|
|
|
original.Client.authenticationRequired = <boolean>req.body.settings;
|
2017-07-14 05:39:09 +08:00
|
|
|
original.save();
|
|
|
|
await ConfigDiagnostics.runDiagnostics();
|
|
|
|
Logger.info(LOG_TAG, "new config:");
|
|
|
|
Logger.info(LOG_TAG, JSON.stringify(Config, null, '\t'));
|
|
|
|
return next();
|
|
|
|
} catch (err) {
|
2017-07-15 18:47:11 +08:00
|
|
|
return next(new ErrorDTO(ErrorCodes.SETTINGS_ERROR, "Settings error: " + JSON.stringify(err, null, ' '), err));
|
2017-07-14 05:39:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-15 18:47:11 +08:00
|
|
|
public static async updateThumbnailSettings(req: Request, res: Response, next: NextFunction) {
|
2017-07-14 05:39:09 +08:00
|
|
|
if ((typeof req.body === 'undefined') || (typeof req.body.settings === 'undefined')) {
|
2017-07-15 18:47:11 +08:00
|
|
|
return next(new ErrorDTO(ErrorCodes.INPUT_ERROR, "settings is needed"));
|
2017-07-14 05:39:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2017-07-15 18:47:11 +08:00
|
|
|
const settings: {
|
|
|
|
server: ThumbnailConfig,
|
|
|
|
client: ClientConfig.ThumbnailConfig
|
|
|
|
} = req.body.settings;
|
|
|
|
|
|
|
|
await ConfigDiagnostics.testServerThumbnailConfig(settings.server);
|
|
|
|
await ConfigDiagnostics.testClientThumbnailConfig(settings.client);
|
|
|
|
Config.Server.thumbnail = settings.server;
|
|
|
|
Config.Client.Thumbnail = settings.client;
|
|
|
|
//only updating explicitly set config (not saving config set by the diagnostics)
|
|
|
|
const original = Config.original();
|
|
|
|
original.Server.thumbnail = settings.server;
|
|
|
|
original.Client.Thumbnail = settings.client;
|
|
|
|
original.save();
|
|
|
|
await ConfigDiagnostics.runDiagnostics();
|
|
|
|
Logger.info(LOG_TAG, "new config:");
|
|
|
|
Logger.info(LOG_TAG, JSON.stringify(Config, null, '\t'));
|
2017-07-14 05:39:09 +08:00
|
|
|
return next();
|
|
|
|
} catch (err) {
|
2017-07-15 18:47:11 +08:00
|
|
|
if (err instanceof Error) {
|
|
|
|
return next(new ErrorDTO(ErrorCodes.SETTINGS_ERROR, "Settings error: " + err.toString(), err));
|
|
|
|
}
|
|
|
|
return next(new ErrorDTO(ErrorCodes.SETTINGS_ERROR, "Settings error: " + JSON.stringify(err, null, ' '), err));
|
2017-07-14 05:39:09 +08:00
|
|
|
}
|
|
|
|
}
|
2017-07-15 18:47:11 +08:00
|
|
|
|
|
|
|
|
2017-07-08 18:43:42 +08:00
|
|
|
}
|