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

Moving sendMail checking to be only on startup #683

This commit is contained in:
Patrik J. Braun 2023-07-30 14:06:25 +02:00
parent a408f1b24d
commit 33ca2040a8
6 changed files with 26 additions and 10 deletions

View File

@ -0,0 +1,6 @@
/**
* Keeps the environment context
*/
export const ServerEnvironment = {
sendMailAvailable: false
};

View File

@ -109,7 +109,6 @@ export class RenderingMWs {
res: Response res: Response
): Promise<void> { ): Promise<void> {
const originalConf = await Config.original(); const originalConf = await Config.original();
await ConfigDiagnostics.checkEnvironment(originalConf);
// These are sensitive information, do not send to the client side // These are sensitive information, do not send to the client side
originalConf.Server.sessionSecret = null; originalConf.Server.sessionSecret = null;
const message = new Message<PrivateConfigClass>( const message = new Message<PrivateConfigClass>(

View File

@ -28,6 +28,7 @@ import {SearchQueryTypes, TextSearch,} from '../../../common/entities/SearchQuer
import {Utils} from '../../../common/Utils'; import {Utils} from '../../../common/Utils';
import {createTransport} from 'nodemailer'; import {createTransport} from 'nodemailer';
import {EmailMessagingType, MessagingConfig} from '../../../common/config/private/MessagingConfig'; import {EmailMessagingType, MessagingConfig} from '../../../common/config/private/MessagingConfig';
import {ServerEnvironment} from '../../Environment';
const LOG_TAG = '[ConfigDiagnostics]'; const LOG_TAG = '[ConfigDiagnostics]';
@ -85,7 +86,7 @@ export class ConfigDiagnostics {
Logger.debug(LOG_TAG, 'Testing EmailMessaging config'); Logger.debug(LOG_TAG, 'Testing EmailMessaging config');
if (Messaging.Email.type === EmailMessagingType.sendmail && !Config.Environment.sendMailAvailable) { if (Messaging.Email.type === EmailMessagingType.sendmail && !Config.Environment.sendMailAvailable) {
throw new Error('sendmail e-mail sending method is not supported as the sendmail application cannot be found in the OS.') throw new Error('sendmail e-mail sending method is not supported as the sendmail application cannot be found in the OS.');
} }
} }
@ -299,7 +300,7 @@ export class ConfigDiagnostics {
} }
static async checkEnvironment(Config:PrivateConfigClass): Promise<void> { static async checkAndSetEnvironment(): Promise<void> {
Logger.debug(LOG_TAG, 'Checking sendmail availability'); Logger.debug(LOG_TAG, 'Checking sendmail availability');
const transporter = createTransport({ const transporter = createTransport({
sendmail: true, sendmail: true,
@ -309,6 +310,7 @@ export class ConfigDiagnostics {
} catch (e) { } catch (e) {
Config.Environment.sendMailAvailable = false; Config.Environment.sendMailAvailable = false;
} }
ServerEnvironment.sendMailAvailable = Config.Environment.sendMailAvailable;
if (!Config.Environment.sendMailAvailable) { if (!Config.Environment.sendMailAvailable) {
Config.Messaging.Email.type = EmailMessagingType.SMTP; Config.Messaging.Email.type = EmailMessagingType.SMTP;
Logger.info(LOG_TAG, 'Sendmail is not available on the OS. You will need to use an SMTP server if you wish the app to send mails.'); Logger.info(LOG_TAG, 'Sendmail is not available on the OS. You will need to use an SMTP server if you wish the app to send mails.');
@ -322,7 +324,7 @@ export class ConfigDiagnostics {
} }
try { try {
await ConfigDiagnostics.checkEnvironment(Config); await ConfigDiagnostics.checkAndSetEnvironment();
} catch (ex) { } catch (ex) {
const err: Error = ex; const err: Error = ex;
NotificationManager.error( NotificationManager.error(

View File

@ -6,6 +6,9 @@ import {ConfigClass, ConfigClassBuilder} from 'typeconfig/node';
import {IConfigClass} from 'typeconfig/common'; import {IConfigClass} from 'typeconfig/common';
import {PasswordHelper} from '../../../backend/model/PasswordHelper'; import {PasswordHelper} from '../../../backend/model/PasswordHelper';
import {TAGS} from '../public/ClientConfig'; import {TAGS} from '../public/ClientConfig';
import {ServerEnvironment} from '../../../backend/Environment';
import {EmailMessagingType} from './MessagingConfig';
import {Logger} from '../../../backend/Logger';
declare const process: any; declare const process: any;
@ -79,6 +82,7 @@ export class PrivateConfigClass extends ServerConfig {
require('../../../../package.json').buildCommitHash; require('../../../../package.json').buildCommitHash;
this.Environment.upTime = upTime; this.Environment.upTime = upTime;
this.Environment.isDocker = !!process.env.PI_DOCKER; this.Environment.isDocker = !!process.env.PI_DOCKER;
this.Environment.sendMailAvailable = ServerEnvironment.sendMailAvailable;
} }
async original(): Promise<PrivateConfigClass & IConfigClass> { async original(): Promise<PrivateConfigClass & IConfigClass> {

View File

@ -30,7 +30,7 @@ import {DefaultsJobs} from '../../entities/job/JobDTO';
import {SearchQueryDTO, SearchQueryTypes, TextSearch,} from '../../entities/SearchQueryDTO'; import {SearchQueryDTO, SearchQueryTypes, TextSearch,} from '../../entities/SearchQueryDTO';
import {SortingMethods} from '../../entities/SortingMethods'; import {SortingMethods} from '../../entities/SortingMethods';
import {UserRoles} from '../../entities/UserDTO'; import {UserRoles} from '../../entities/UserDTO';
import {MessagingConfig} from './MessagingConfig'; import {EmailMessagingType, MessagingConfig} from './MessagingConfig';
declare let $localize: (s: TemplateStringsArray) => string; declare let $localize: (s: TemplateStringsArray) => string;
@ -904,6 +904,7 @@ export class ServerPreviewConfig {
@SubConfigClass({softReadonly: true}) @SubConfigClass({softReadonly: true})
export class ServerMediaConfig extends ClientMediaConfig { export class ServerMediaConfig extends ClientMediaConfig {
@ConfigProperty({ @ConfigProperty({
tags: { tags: {
name: $localize`Images folder`, name: $localize`Images folder`,
priority: ConfigPriority.basic, priority: ConfigPriority.basic,
@ -1050,8 +1051,13 @@ export class ServerEnvironmentConfig {
buildCommitHash: string | undefined; buildCommitHash: string | undefined;
@ConfigProperty({volatile: true}) @ConfigProperty({volatile: true})
isDocker: boolean | undefined; isDocker: boolean | undefined;
@ConfigProperty({ @ConfigProperty<boolean, ServerConfig, TAGS>({
volatile: true, volatile: true,
onNewValue: (value, config) => {
if (value === false) {
config.Messaging.Email.type = EmailMessagingType.SMTP;
}
},
description: 'App updates on start-up if sendmail binary is available' description: 'App updates on start-up if sendmail binary is available'
}) })
sendMailAvailable: boolean | undefined; sendMailAvailable: boolean | undefined;

View File

@ -1,12 +1,12 @@
import * as path from 'path'; import * as path from 'path';
import * as fs from 'fs'; import * as fs from 'fs';
import {Config} from '../../../../../src/common/config/private/Config'; import {Config} from '../../../../../src/common/config/private/Config';
import {SQLConnection} from '../../../../../src/backend/model/database/SQLConnection';
import {Server} from '../../../../../src/backend/server'; import {Server} from '../../../../../src/backend/server';
import {DatabaseType, ServerConfig} from '../../../../../src/common/config/private/PrivateConfig'; import {DatabaseType, ServerConfig} from '../../../../../src/common/config/private/PrivateConfig';
import {ProjectPath} from '../../../../../src/backend/ProjectPath'; import {ProjectPath} from '../../../../../src/backend/ProjectPath';
import {TAGS} from '../../../../../src/common/config/public/ClientConfig'; import {TAGS} from '../../../../../src/common/config/public/ClientConfig';
import {ObjectManagers} from '../../../../../src/backend/model/ObjectManagers'; import {ObjectManagers} from '../../../../../src/backend/model/ObjectManagers';
import {UserRoles} from '../../../../../src/common/entities/UserDTO';
process.env.NODE_ENV = 'test'; process.env.NODE_ENV = 'test';
const chai: any = require('chai'); const chai: any = require('chai');
@ -34,9 +34,8 @@ describe('SettingsRouter', () => {
describe('/GET settings', () => { describe('/GET settings', () => {
it('it should GET the settings', async () => { it('it should GET the settings', async () => {
Config.Users.authenticationRequired = false; Config.Users.authenticationRequired = false;
Config.Users.unAuthenticatedUserRole = UserRoles.Admin;
const originalSettings = await Config.original(); const originalSettings = await Config.original();
// originalSettings.Server.sessionSecret = null;
// originalSettings.Users.enforcedUsers = null;
const srv = new Server(); const srv = new Server();
await srv.onStarted.wait(); await srv.onStarted.wait();
const result = await chai.request(srv.App) const result = await chai.request(srv.App)