1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/backend/model/ConfigDiagnostics.ts

190 lines
7.0 KiB
TypeScript
Raw Normal View History

2018-03-31 03:30:30 +08:00
import {Config} from '../../common/config/private/Config';
2017-07-14 05:39:09 +08:00
import {
DataBaseConfig,
DatabaseType,
2017-07-15 20:27:12 +08:00
IPrivateConfig,
2017-07-14 05:39:09 +08:00
ThumbnailConfig,
ThumbnailProcessingLib
2018-03-31 03:30:30 +08:00
} from '../../common/config/private/IPrivateConfig';
import {Logger} from '../Logger';
import {NotificationManager} from './NotifocationManager';
import {ProjectPath} from '../ProjectPath';
import {SQLConnection} from './sql/SQLConnection';
import * as fs from 'fs';
import {ClientConfig} from '../../common/config/public/ConfigClass';
const LOG_TAG = '[ConfigDiagnostics]';
2017-07-14 05:39:09 +08:00
export class ConfigDiagnostics {
static async testDatabase(databaseConfig: DataBaseConfig) {
if (databaseConfig.type != DatabaseType.memory) {
await SQLConnection.tryConnection(databaseConfig);
2017-07-14 05:39:09 +08:00
}
}
static async testThumbnailLib(processingLibrary: ThumbnailProcessingLib) {
switch (processingLibrary) {
case ThumbnailProcessingLib.sharp:
2018-03-31 03:30:30 +08:00
const sharp = require('sharp');
2017-07-14 05:39:09 +08:00
sharp();
break;
case ThumbnailProcessingLib.gm:
2018-03-31 03:30:30 +08:00
const gm = require('gm');
2017-07-14 05:39:09 +08:00
await new Promise((resolve, reject) => {
2018-03-31 03:30:30 +08:00
gm(ProjectPath.FrontendFolder + '/assets/icon.png').size((err, value) => {
2017-07-15 18:47:11 +08:00
if (err) {
return reject(err.toString());
2017-07-14 05:39:09 +08:00
}
2017-07-15 18:47:11 +08:00
return resolve();
2017-07-14 05:39:09 +08:00
});
});
break;
}
}
static async testThumbnailFolder(folder: string) {
await new Promise((resolve, reject) => {
2017-07-14 15:28:52 +08:00
fs.access(folder, fs.constants.W_OK, (err) => {
2017-07-14 05:39:09 +08:00
if (err) {
2018-03-31 03:30:30 +08:00
reject({message: 'Error during getting write access to temp folder', error: err.toString()});
2017-07-14 05:39:09 +08:00
}
});
resolve();
});
}
static async testImageFolder(folder: string) {
await new Promise((resolve, reject) => {
if (!fs.existsSync(folder)) {
2018-03-31 03:30:30 +08:00
reject('Images folder not exists: \'' + folder + '\'');
2017-07-14 05:39:09 +08:00
}
2017-07-14 15:28:52 +08:00
fs.access(folder, fs.constants.R_OK, (err) => {
2017-07-14 05:39:09 +08:00
if (err) {
2018-03-31 03:30:30 +08:00
reject({message: 'Error during getting read access to images folder', error: err.toString()});
2017-07-14 05:39:09 +08:00
}
});
resolve();
});
}
2017-07-15 18:47:11 +08:00
static async testServerThumbnailConfig(thumbnailConfig: ThumbnailConfig) {
2017-07-14 05:39:09 +08:00
await ConfigDiagnostics.testThumbnailLib(thumbnailConfig.processingLibrary);
await ConfigDiagnostics.testThumbnailFolder(thumbnailConfig.folder);
}
2017-07-15 18:47:11 +08:00
static async testClientThumbnailConfig(thumbnailConfig: ClientConfig.ThumbnailConfig) {
if (isNaN(thumbnailConfig.iconSize) || thumbnailConfig.iconSize <= 0) {
2018-03-31 03:30:30 +08:00
throw 'IconSize has to be >= 0 integer, got: ' + thumbnailConfig.iconSize;
2017-07-15 18:47:11 +08:00
}
if (!thumbnailConfig.thumbnailSizes.length) {
2018-03-31 03:30:30 +08:00
throw 'At least one thumbnail size is needed';
2017-07-15 18:47:11 +08:00
}
for (let i = 0; i < thumbnailConfig.thumbnailSizes.length; i++) {
if (isNaN(thumbnailConfig.thumbnailSizes[i]) || thumbnailConfig.thumbnailSizes[i] <= 0) {
2018-03-31 03:30:30 +08:00
throw 'Thumbnail size has to be >= 0 integer, got: ' + thumbnailConfig.thumbnailSizes[i];
2017-07-15 18:47:11 +08:00
}
}
}
2017-07-14 05:39:09 +08:00
2017-07-15 20:27:12 +08:00
static async testSearchConfig(search: ClientConfig.SearchConfig, config: IPrivateConfig) {
if (search.enabled == true && config.Server.database.type == DatabaseType.memory) {
2018-03-31 03:30:30 +08:00
throw 'Memory Database do not support searching';
2017-07-14 05:39:09 +08:00
}
}
2017-07-15 20:27:12 +08:00
static async testSharingConfig(sharing: ClientConfig.SharingConfig, config: IPrivateConfig) {
if (sharing.enabled == true && config.Server.database.type == DatabaseType.memory) {
2018-03-31 03:30:30 +08:00
throw 'Memory Database do not support sharing';
2017-07-14 05:39:09 +08:00
}
}
2017-07-15 18:47:11 +08:00
static async testMapConfig(map: ClientConfig.MapConfig) {
2017-07-14 05:39:09 +08:00
if (map.enabled == true && (!map.googleApiKey || map.googleApiKey.length == 0)) {
2018-03-31 03:30:30 +08:00
throw 'Maps need a valid google api key';
2017-07-14 05:39:09 +08:00
}
}
static async runDiagnostics() {
if (Config.Server.database.type != DatabaseType.memory) {
2017-07-14 05:39:09 +08:00
try {
await ConfigDiagnostics.testDatabase(Config.Server.database);
} catch (err) {
2018-03-31 03:30:30 +08:00
Logger.warn(LOG_TAG, '[SQL error]', err);
Logger.warn(LOG_TAG, 'Error during initializing SQL falling back temporally to memory DB');
NotificationManager.warning('Error during initializing SQL falling back temporally to memory DB', err);
2017-07-14 05:39:09 +08:00
Config.setDatabaseType(DatabaseType.memory);
}
}
if (Config.Server.thumbnail.processingLibrary != ThumbnailProcessingLib.Jimp) {
try {
await ConfigDiagnostics.testThumbnailLib(Config.Server.thumbnail.processingLibrary);
} catch (err) {
2018-03-31 03:30:30 +08:00
NotificationManager.warning('Thumbnail hardware acceleration is not possible.' +
' \'' + ThumbnailProcessingLib[Config.Server.thumbnail.processingLibrary] + '\' node module is not found.' +
' Falling back temporally to JS based thumbnail generation', err);
Logger.warn(LOG_TAG, '[Thumbnail hardware acceleration] module error: ', err);
Logger.warn(LOG_TAG, 'Thumbnail hardware acceleration is not possible.' +
' \'' + ThumbnailProcessingLib[Config.Server.thumbnail.processingLibrary] + '\' node module is not found.' +
' Falling back temporally to JS based thumbnail generation');
2017-07-14 05:39:09 +08:00
Config.Server.thumbnail.processingLibrary = ThumbnailProcessingLib.Jimp;
}
}
try {
2018-03-31 03:30:30 +08:00
await ConfigDiagnostics.testThumbnailFolder(Config.Server.thumbnail.folder);
2017-07-14 05:39:09 +08:00
} catch (err) {
2018-03-31 03:30:30 +08:00
NotificationManager.error('Thumbnail folder error', err);
Logger.error(LOG_TAG, 'Thumbnail folder error', err);
2017-07-14 05:39:09 +08:00
}
try {
2018-03-31 03:30:30 +08:00
await ConfigDiagnostics.testImageFolder(Config.Server.imagesFolder);
2017-07-14 05:39:09 +08:00
} catch (err) {
2018-03-31 03:30:30 +08:00
NotificationManager.error('Images folder error', err);
Logger.error(LOG_TAG, 'Images folder error', err);
2017-07-14 05:39:09 +08:00
}
2017-07-15 18:47:11 +08:00
try {
2018-03-31 03:30:30 +08:00
await ConfigDiagnostics.testClientThumbnailConfig(Config.Client.Thumbnail);
2017-07-15 18:47:11 +08:00
} catch (err) {
2018-03-31 03:30:30 +08:00
NotificationManager.error('Thumbnail settings error', err);
Logger.error(LOG_TAG, 'Thumbnail settings error', err);
2017-07-15 18:47:11 +08:00
}
2017-07-14 05:39:09 +08:00
try {
2017-07-15 20:27:12 +08:00
await ConfigDiagnostics.testSearchConfig(Config.Client.Search, Config);
2017-07-14 05:39:09 +08:00
} catch (err) {
2018-03-31 03:30:30 +08:00
NotificationManager.warning('Search is not supported with these settings. Disabling temporally. Please adjust the config properly.', err);
Logger.warn(LOG_TAG, 'Search is not supported with these settings, switching off..', err);
2017-07-14 05:39:09 +08:00
Config.Client.Search.enabled = false;
}
try {
2017-07-15 20:27:12 +08:00
await ConfigDiagnostics.testSharingConfig(Config.Client.Sharing, Config);
2017-07-14 05:39:09 +08:00
} catch (err) {
2018-03-31 03:30:30 +08:00
NotificationManager.warning('Sharing is not supported with these settings. Disabling temporally. Please adjust the config properly.', err);
Logger.warn(LOG_TAG, 'Sharing is not supported with these settings, switching off..', err);
2017-07-14 05:39:09 +08:00
Config.Client.Sharing.enabled = false;
}
try {
await ConfigDiagnostics.testMapConfig(Config.Client.Map);
} catch (err) {
2018-03-31 03:30:30 +08:00
NotificationManager.warning('Maps is not supported with these settings. Disabling temporally. Please adjust the config properly.', err);
Logger.warn(LOG_TAG, 'Maps is not supported with these settings. Disabling temporally. Please adjust the config properly.', err);
2017-07-14 05:39:09 +08:00
Config.Client.Map.enabled = false;
}
}
}