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

366 lines
14 KiB
TypeScript
Raw Normal View History

2018-11-19 03:26:29 +08:00
import {Config} from '../../../common/config/private/Config';
import {Logger} from '../../Logger';
import {NotificationManager} from '../NotifocationManager';
import {ProjectPath} from '../../ProjectPath';
import {SQLConnection} from '../database/sql/SQLConnection';
2018-03-31 03:30:30 +08:00
import * as fs from 'fs';
2018-11-19 03:26:29 +08:00
import {ClientConfig} from '../../../common/config/public/ConfigClass';
import {FFmpegFactory} from '../FFmpegFactory';
import {IPrivateConfig, ServerConfig} from '../../../common/config/private/IPrivateConfig';
2018-03-31 03:30:30 +08:00
const LOG_TAG = '[ConfigDiagnostics]';
2019-12-10 22:25:57 +08:00
2017-07-14 05:39:09 +08:00
export class ConfigDiagnostics {
2019-12-10 22:25:57 +08:00
static checkReadWritePermission(path: string) {
return new Promise((resolve, reject) => {
// tslint:disable-next-line:no-bitwise
fs.access(path, fs.constants.R_OK | fs.constants.W_OK, (err) => {
if (err) {
return reject(err);
}
return resolve();
});
});
}
static async testDatabase(databaseConfig: ServerConfig.DataBaseConfig) {
if (databaseConfig.type !== ServerConfig.DatabaseType.memory) {
await SQLConnection.tryConnection(databaseConfig);
2017-07-14 05:39:09 +08:00
}
2019-12-10 22:25:57 +08:00
if (databaseConfig.type !== ServerConfig.DatabaseType.sqlite) {
try {
await this.checkReadWritePermission(ProjectPath.getAbsolutePath(databaseConfig.sqlite.storage));
} catch (e) {
throw new Error('Cannot read or write sqlite storage file: ' +
ProjectPath.getAbsolutePath(databaseConfig.sqlite.storage));
}
}
2017-07-14 05:39:09 +08:00
}
static async testMetaFileConfig(metaFileConfig: ClientConfig.MetaFileConfig, config: IPrivateConfig) {
2018-12-02 06:53:35 +08:00
if (metaFileConfig.enabled === true &&
config.Client.Map.enabled === false) {
throw new Error('*.gpx meta files are not supported without MAP');
}
}
static testClientVideoConfig(videoConfig: ClientConfig.VideoConfig) {
2018-11-19 03:26:29 +08:00
return new Promise((resolve, reject) => {
try {
if (videoConfig.enabled === true) {
const ffmpeg = FFmpegFactory.get();
2018-11-29 06:49:33 +08:00
ffmpeg().getAvailableCodecs((err: Error) => {
2018-11-19 03:26:29 +08:00
if (err) {
return reject(new Error('Error accessing ffmpeg, cant find executable: ' + err.toString()));
}
2018-11-29 06:49:33 +08:00
ffmpeg(__dirname + '/blank.jpg').ffprobe((err2: Error) => {
2018-11-19 03:26:29 +08:00
if (err2) {
return reject(new Error('Error accessing ffmpeg-probe, cant find executable: ' + err2.toString()));
}
return resolve();
});
});
} else {
return resolve();
}
} catch (e) {
2019-12-13 02:11:49 +08:00
return reject(new Error('unknown video error: ' + e.toString()));
2018-11-19 03:26:29 +08:00
}
});
}
static async testServerVideoConfig(videoConfig: ServerConfig.VideoConfig, config: IPrivateConfig) {
if (config.Client.Media.Video.enabled === true) {
if (videoConfig.transcoding.fps <= 0) {
throw new Error('fps should be grater than 0');
}
}
}
static async testSharp() {
const sharp = require('sharp');
sharp();
}
static async testGM() {
const gm = require('gm');
await new Promise((resolve, reject) => {
gm(ProjectPath.FrontendFolder + '/assets/icon.png').size((err: Error) => {
if (err) {
return reject(err.toString());
}
return resolve();
});
});
}
2019-12-15 21:40:31 +08:00
static async testThumbnailLib(processingLibrary: ServerConfig.PhotoProcessingLib) {
2017-07-14 05:39:09 +08:00
switch (processingLibrary) {
2019-12-15 21:40:31 +08:00
case ServerConfig.PhotoProcessingLib.sharp:
await this.testSharp();
2017-07-14 05:39:09 +08:00
break;
2019-12-15 21:40:31 +08:00
case ServerConfig.PhotoProcessingLib.gm:
await this.testGM();
2017-07-14 05:39:09 +08:00
break;
}
}
static async testTempFolder(folder: string) {
await this.checkReadWritePermission(folder);
2017-07-14 05:39:09 +08:00
}
2018-11-19 03:26:29 +08:00
static testImageFolder(folder: string) {
return new Promise((resolve, reject) => {
2017-07-14 05:39:09 +08:00
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();
});
}
2019-12-15 22:53:03 +08:00
static async testServerPhotoConfig(server: ServerConfig.PhotoConfig) {
}
static async testClientPhotoConfig(client: ClientConfig.PhotoConfig) {
}
2019-12-15 21:40:31 +08:00
public static async testServerThumbnailConfig(server: ServerConfig.ThumbnailConfig) {
if (server.personFaceMargin < 0 || server.personFaceMargin > 1) {
throw new Error('personFaceMargin should be between 0 and 1');
}
2017-07-14 05:39:09 +08:00
}
2017-07-15 18:47:11 +08:00
static async testClientThumbnailConfig(thumbnailConfig: ClientConfig.ThumbnailConfig) {
if (isNaN(thumbnailConfig.iconSize) || thumbnailConfig.iconSize <= 0) {
throw new Error('IconSize has to be >= 0 integer, got: ' + thumbnailConfig.iconSize);
2017-07-15 18:47:11 +08:00
}
if (!thumbnailConfig.thumbnailSizes.length) {
throw new Error('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) {
throw new Error('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
2019-12-24 19:22:25 +08:00
static async testTasksConfig(task: ServerConfig.JobConfig, config: IPrivateConfig) {
2019-08-20 18:54:45 +08:00
}
2019-07-21 22:39:52 +08:00
static async testFacesConfig(faces: ClientConfig.FacesConfig, config: IPrivateConfig) {
2019-07-21 22:55:19 +08:00
if (faces.enabled === true) {
if (config.Server.Database.type === ServerConfig.DatabaseType.memory) {
2019-07-21 22:55:19 +08:00
throw new Error('Memory Database do not support faces');
}
if (config.Client.Search.enabled === false) {
throw new Error('Faces support needs enabled search');
}
2019-07-21 22:39:52 +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 === ServerConfig.DatabaseType.memory) {
throw new Error('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 === ServerConfig.DatabaseType.memory) {
throw new Error('Memory Database do not support sharing');
2017-07-14 05:39:09 +08:00
}
2018-05-25 09:29:05 +08:00
if (sharing.enabled === true &&
config.Client.authenticationRequired === false) {
throw new Error('In case of no authentication, sharing is not supported');
}
2017-07-14 05:39:09 +08:00
}
static async testRandomPhotoConfig(sharing: ClientConfig.RandomPhotoConfig, config: IPrivateConfig) {
if (sharing.enabled === true &&
config.Server.Database.type === ServerConfig.DatabaseType.memory) {
throw new Error('Memory Database do not support random photo');
}
}
2018-12-08 18:28:56 +08:00
static async testMapConfig(map: ClientConfig.MapConfig): Promise<void> {
if (map.enabled === false) {
return;
}
if (map.mapProvider === ClientConfig.MapProviders.Mapbox &&
(!map.mapboxAccessToken || map.mapboxAccessToken.length === 0)) {
throw new Error('Mapbox needs a valid api key.');
}
if (map.mapProvider === ClientConfig.MapProviders.Custom &&
(!map.customLayers || map.customLayers.length === 0)) {
throw new Error('Custom maps need at least one valid layer');
}
if (map.mapProvider === ClientConfig.MapProviders.Custom) {
map.customLayers.forEach(l => {
if (!l.url || l.url.length === 0) {
throw new Error('Custom maps url need to be a valid layer');
}
});
2017-07-14 05:39:09 +08:00
}
}
static async runDiagnostics() {
if (Config.Server.Database.type !== ServerConfig.DatabaseType.memory) {
2017-07-14 05:39:09 +08:00
try {
await ConfigDiagnostics.testDatabase(Config.Server.Database);
2018-05-25 09:29:05 +08:00
} catch (ex) {
const err: Error = ex;
Logger.warn(LOG_TAG, '[SQL error]', err.toString());
2018-03-31 03:30:30 +08:00
Logger.warn(LOG_TAG, 'Error during initializing SQL falling back temporally to memory DB');
2018-05-25 09:29:05 +08:00
NotificationManager.warning('Error during initializing SQL falling back temporally to memory DB', err.toString());
Config.setDatabaseType(ServerConfig.DatabaseType.memory);
2017-07-14 05:39:09 +08:00
}
}
2019-12-15 21:40:31 +08:00
if (Config.Server.Media.photoProcessingLibrary !== ServerConfig.PhotoProcessingLib.Jimp) {
2017-07-14 05:39:09 +08:00
try {
2019-12-15 21:40:31 +08:00
await ConfigDiagnostics.testThumbnailLib(Config.Server.Media.photoProcessingLibrary);
2018-05-25 09:29:05 +08:00
} catch (ex) {
const err: Error = ex;
2018-03-31 03:30:30 +08:00
NotificationManager.warning('Thumbnail hardware acceleration is not possible.' +
2019-12-15 21:40:31 +08:00
' \'' + ServerConfig.PhotoProcessingLib[Config.Server.Media.photoProcessingLibrary] + '\' node module is not found.' +
2018-05-25 09:29:05 +08:00
' Falling back temporally to JS based thumbnail generation', err.toString());
Logger.warn(LOG_TAG, '[Thumbnail hardware acceleration] module error: ', err.toString());
2018-03-31 03:30:30 +08:00
Logger.warn(LOG_TAG, 'Thumbnail hardware acceleration is not possible.' +
2019-12-15 21:40:31 +08:00
' \'' + ServerConfig.PhotoProcessingLib[Config.Server.Media.photoProcessingLibrary] + '\' node module is not found.' +
2018-03-31 03:30:30 +08:00
' Falling back temporally to JS based thumbnail generation');
2019-12-15 21:40:31 +08:00
Config.Server.Media.photoProcessingLibrary = ServerConfig.PhotoProcessingLib.Jimp;
2017-07-14 05:39:09 +08:00
}
}
try {
await ConfigDiagnostics.testTempFolder(Config.Server.Media.tempFolder);
2018-05-25 09:29:05 +08:00
} catch (ex) {
const err: Error = ex;
NotificationManager.error('Thumbnail folder error', err.toString());
Logger.error(LOG_TAG, 'Thumbnail folder error', err.toString());
2017-07-14 05:39:09 +08:00
}
2018-11-19 03:26:29 +08:00
try {
await ConfigDiagnostics.testClientVideoConfig(Config.Client.Media.Video);
await ConfigDiagnostics.testServerVideoConfig(Config.Server.Media.Video, Config);
2018-11-19 03:26:29 +08:00
} catch (ex) {
const err: Error = ex;
NotificationManager.warning('Video support error, switching off..', err.toString());
Logger.warn(LOG_TAG, 'Video support error, switching off..', err.toString());
Config.Client.Media.Video.enabled = false;
2018-11-19 03:26:29 +08:00
}
2018-12-02 06:53:35 +08:00
try {
await ConfigDiagnostics.testMetaFileConfig(Config.Client.MetaFile, Config);
} catch (ex) {
const err: Error = ex;
NotificationManager.warning('Meta file support error, switching off..', err.toString());
Logger.warn(LOG_TAG, 'Meta file support error, switching off..', err.toString());
Config.Client.MetaFile.enabled = false;
}
2018-11-19 03:26:29 +08:00
2017-07-14 05:39:09 +08:00
try {
await ConfigDiagnostics.testImageFolder(Config.Server.Media.folder);
2018-05-25 09:29:05 +08:00
} catch (ex) {
const err: Error = ex;
NotificationManager.error('Images folder error', err.toString());
Logger.error(LOG_TAG, 'Images folder error', err.toString());
2017-07-14 05:39:09 +08:00
}
2017-07-15 18:47:11 +08:00
try {
await ConfigDiagnostics.testClientThumbnailConfig(Config.Client.Media.Thumbnail);
2018-05-25 09:29:05 +08:00
} catch (ex) {
const err: Error = ex;
NotificationManager.error('Thumbnail settings error', err.toString());
Logger.error(LOG_TAG, 'Thumbnail settings error', err.toString());
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);
2018-05-25 09:29:05 +08:00
} catch (ex) {
const err: Error = ex;
NotificationManager.warning('Search is not supported with these settings. Disabling temporally. ' +
2018-05-25 09:29:05 +08:00
'Please adjust the config properly.', err.toString());
Logger.warn(LOG_TAG, 'Search is not supported with these settings, switching off..', err.toString());
2017-07-14 05:39:09 +08:00
Config.Client.Search.enabled = false;
}
2019-07-21 22:39:52 +08:00
try {
await ConfigDiagnostics.testFacesConfig(Config.Client.Faces, Config);
} catch (ex) {
const err: Error = ex;
NotificationManager.warning('Faces are not supported with these settings. Disabling temporally. ' +
'Please adjust the config properly.', err.toString());
Logger.warn(LOG_TAG, 'Faces are not supported with these settings, switching off..', err.toString());
Config.Client.Faces.enabled = false;
}
2019-08-20 18:54:45 +08:00
try {
2019-12-24 19:22:25 +08:00
await ConfigDiagnostics.testTasksConfig(Config.Server.Jobs, Config);
2019-08-20 18:54:45 +08:00
} catch (ex) {
const err: Error = ex;
NotificationManager.warning('Some Tasks are not supported with these settings. Disabling temporally. ' +
'Please adjust the config properly.', err.toString());
Logger.warn(LOG_TAG, 'Some Tasks not supported with these settings, switching off..', err.toString());
Config.Client.Faces.enabled = false;
}
2017-07-14 05:39:09 +08:00
try {
2017-07-15 20:27:12 +08:00
await ConfigDiagnostics.testSharingConfig(Config.Client.Sharing, Config);
2018-05-25 09:29:05 +08:00
} catch (ex) {
const err: Error = ex;
NotificationManager.warning('Sharing is not supported with these settings. Disabling temporally. ' +
2018-05-25 09:29:05 +08:00
'Please adjust the config properly.', err.toString());
Logger.warn(LOG_TAG, 'Sharing is not supported with these settings, switching off..', err.toString());
2017-07-14 05:39:09 +08:00
Config.Client.Sharing.enabled = false;
}
try {
await ConfigDiagnostics.testRandomPhotoConfig(Config.Client.Sharing, Config);
} catch (ex) {
const err: Error = ex;
2018-11-05 02:28:32 +08:00
NotificationManager.warning('Random Media is not supported with these settings. Disabling temporally. ' +
'Please adjust the config properly.', err.toString());
2018-11-05 02:28:32 +08:00
Logger.warn(LOG_TAG, 'Random Media is not supported with these settings, switching off..', err.toString());
Config.Client.Sharing.enabled = false;
}
2017-07-14 05:39:09 +08:00
try {
await ConfigDiagnostics.testMapConfig(Config.Client.Map);
2018-05-25 09:29:05 +08:00
} catch (ex) {
const err: Error = ex;
NotificationManager.warning('Maps is not supported with these settings. Using open street maps temporally. ' +
2018-05-25 09:29:05 +08:00
'Please adjust the config properly.', err.toString());
Logger.warn(LOG_TAG, 'Maps is not supported with these settings. Using open street maps temporally ' +
2018-05-25 09:29:05 +08:00
'Please adjust the config properly.', err.toString());
Config.Client.Map.mapProvider = ClientConfig.MapProviders.OpenStreetMap;
2017-07-14 05:39:09 +08:00
}
}
2019-12-15 21:40:31 +08:00
2017-07-14 05:39:09 +08:00
}