gancio-upstream/server/api/controller/settings.js

237 lines
7 KiB
JavaScript
Raw Normal View History

2019-07-24 21:26:56 +02:00
const path = require('path')
2021-09-30 11:13:44 +02:00
const URL = require('url')
const crypto = require('crypto')
2021-09-30 11:13:44 +02:00
const { promisify } = require('util')
2020-07-07 21:58:52 +02:00
const sharp = require('sharp')
2021-09-30 11:13:44 +02:00
const config = require('../../config')
const generateKeyPair = promisify(crypto.generateKeyPair)
2021-03-05 14:17:10 +01:00
const log = require('../../log')
2022-11-29 14:40:19 +01:00
// const locales = require('../../../locales/index')
const escape = require('lodash/escape')
const DB = require('../models/models')
2021-09-30 11:13:44 +02:00
let defaultHostname
try {
defaultHostname = new URL.URL(config.baseurl).hostname
} catch (e) {}
const defaultSettings = {
2021-10-21 12:17:41 +02:00
title: config.title || 'Gancio',
description: config.description || 'A shared agenda for local communities',
2021-09-30 11:13:44 +02:00
baseurl: config.baseurl || '',
hostname: defaultHostname,
instance_timezone: 'Europe/Rome',
2020-02-20 18:37:10 +01:00
instance_locale: 'en',
2021-09-30 11:13:44 +02:00
instance_name: 'gancio',
2020-03-18 10:11:24 +01:00
instance_place: '',
allow_registration: true,
allow_anon_event: true,
allow_multidate_event: true,
allow_recurrent_event: false,
recurrent_event_visible: false,
allow_event_only_online: false,
allow_event_also_online: false,
allow_geolocation: false,
geocoding_provider_type: 'Nominatim',
geocoding_provider: 'https://nominatim.openstreetmap.org/search',
geocoding_countrycodes: [],
tilelayer_provider: 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
tilelayer_provider_attribution: "<a target=\"_blank\" href=\"http://osm.org/copyright\">OpenStreetMap</a> contributors",
enable_federation: true,
enable_resources: false,
2020-03-14 18:45:20 +01:00
hide_boosts: true,
2020-03-18 10:11:24 +01:00
enable_trusted_instances: true,
trusted_instances: [],
2021-10-21 16:18:41 +02:00
'theme.is_dark': true,
'theme.primary': '#FF4500',
trusted_instances_label: '',
hide_thumbs: false,
hide_calendar: false,
footerLinks: [
2022-11-25 09:47:46 +01:00
{ href: '/', label: 'common.home' },
{ href: '/about', label: 'common.about' }
2021-09-30 11:13:44 +02:00
],
2022-07-15 20:42:27 +02:00
plugins: [],
2021-10-18 15:46:38 +02:00
admin_email: config.admin_email || '',
2022-05-09 20:45:54 +02:00
smtp: config.smtp || {}
}
2019-10-11 18:34:14 +02:00
/**
* Settings controller: store instance settings
*/
2019-04-03 00:25:12 +02:00
const settingsController = {
2019-06-25 01:05:38 +02:00
settings: { initialized: false },
2019-07-26 23:51:32 +02:00
user_locale: {},
2019-06-25 01:05:38 +02:00
secretSettings: {},
2019-06-06 23:54:32 +02:00
async load () {
2022-08-02 16:10:15 +02:00
if (config.status !== 'CONFIGURED') {
2021-09-27 10:42:17 +02:00
settingsController.settings = defaultSettings
return
}
if (settingsController.settings.initialized) return
settingsController.settings.initialized = true
// initialize instance settings from db
// note that this is done only once when the server starts
// and not for each request
const settings = await DB.Setting.findAll()
settingsController.settings = defaultSettings
settings.forEach(s => {
if (s.is_secret) {
settingsController.secretSettings[s.key] = s.value
} else {
settingsController.settings[s.key] = s.value
}
})
// add pub/priv instance key if needed
if (!settingsController.settings.publicKey) {
log.info('Instance priv/pub key not found, generating....')
const { publicKey, privateKey } = await generateKeyPair('rsa', {
modulusLength: 4096,
publicKeyEncoding: {
type: 'spki',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem'
2019-06-25 01:05:38 +02:00
}
})
2019-07-26 23:51:32 +02:00
await settingsController.set('publicKey', publicKey)
await settingsController.set('privateKey', privateKey, true)
}
2019-09-26 22:46:04 +02:00
// initialize user_locale
2022-11-29 14:40:19 +01:00
// if (config.user_locale && fs.existsSync(path.resolve(config.user_locale))) {
// const user_locales_files = fs.readdirSync(path.resolve(config.user_locale))
// user_locales_files.forEach( f => {
// const locale = path.basename(f ,'.json')
// if (locales[locale]) {
// log.info(`Adding custom locale ${locale}`)
// settingsController.user_locale[locale] = require(path.resolve(config.user_locale, f)).default
// } else {
// log.warning(`Unknown custom user locale: ${locale} [valid locales are ${locales}]`)
// }
// })
// }
const pluginController = require('./plugins')
2022-08-09 18:32:47 +02:00
pluginController._load()
},
2019-11-06 11:31:56 +01:00
async set (key, value, is_secret = false) {
2021-05-20 11:19:26 +02:00
log.info(`SET ${key} ${is_secret ? '*****' : value}`)
try {
const [setting, created] = await DB.Setting.findOrCreate({
where: { key },
defaults: { value, is_secret }
2019-04-03 00:25:12 +02:00
})
2020-07-06 00:52:32 +02:00
if (!created) { setting.update({ value, is_secret }) }
2019-11-06 11:31:56 +01:00
settingsController[is_secret ? 'secretSettings' : 'settings'][key] = value
return true
2019-11-06 11:31:56 +01:00
} catch (e) {
2021-07-08 20:41:56 +02:00
log.error('[SETTING SET]', e)
return false
}
2019-04-03 00:25:12 +02:00
},
2019-11-06 11:31:56 +01:00
async setRequest (req, res) {
const { key, value, is_secret } = req.body
const ret = await settingsController.set(key, value, is_secret)
2019-11-06 11:31:56 +01:00
if (ret) { res.sendStatus(200) } else { res.sendStatus(400) }
},
2021-10-18 15:46:38 +02:00
async testSMTP (req, res) {
const smtp = req.body
await settingsController.set('smtp', smtp.smtp)
const mail = require('../mail')
try {
await mail._send(settingsController.settings.admin_email, 'test')
2021-10-18 15:46:38 +02:00
return res.sendStatus(200)
} catch (e) {
console.error(e)
return res.status(400).send(escape(String(e)))
2021-10-18 15:46:38 +02:00
}
},
2022-07-18 10:05:59 +02:00
getSMTPSettings (_req, res) {
return res.json(settingsController['settings']['smtp'])
},
2022-07-29 11:30:15 +02:00
getAll (_req, res) {
return res.json(settingsController.settings)
},
2020-07-07 21:58:52 +02:00
setLogo (req, res) {
2020-02-01 22:39:31 +01:00
if (!req.file) {
2021-04-28 12:44:26 +02:00
settingsController.set('logo', false)
return res.status(200)
2020-02-01 22:39:31 +01:00
}
2020-07-05 23:53:37 +02:00
2020-07-28 12:24:39 +02:00
const uploadedPath = path.join(req.file.destination, req.file.filename)
const baseImgPath = path.resolve(config.upload_path, 'logo')
2020-07-07 21:58:52 +02:00
// convert and resize to png
return sharp(uploadedPath)
2020-07-07 21:58:52 +02:00
.resize(400)
.png({ quality: 90 })
2022-05-09 20:45:54 +02:00
.toFile(baseImgPath + '.png', (err) => {
2021-03-05 14:17:10 +01:00
if (err) {
log.error('[LOGO] ' + err)
2021-03-05 14:17:10 +01:00
}
2020-07-28 12:24:39 +02:00
settingsController.set('logo', baseImgPath)
2020-07-07 21:58:52 +02:00
res.sendStatus(200)
})
2022-10-28 12:01:59 +02:00
},
setFallbackImage (req, res) {
if (!req.file) {
settingsController.set('fallback_image', false)
2022-10-28 12:01:59 +02:00
return res.status(200)
}
const uploadedPath = path.join(req.file.destination, req.file.filename)
const baseImgPath = path.resolve(config.upload_path, 'fallbackImage.png')
// convert and resize to png
return sharp(uploadedPath)
.resize(600)
.png({ quality: 99 })
.toFile(baseImgPath, (err) => {
if (err) {
log.error('[FALLBACK IMAGE] ' + err)
}
settingsController.set('fallback_image', baseImgPath)
res.sendStatus(200)
})
},
setHeaderImage (req, res) {
if (!req.file) {
settingsController.set('header_image', false)
return res.status(200)
}
const uploadedPath = path.join(req.file.destination, req.file.filename)
2022-12-13 16:01:11 +01:00
const baseImgPath = path.resolve(config.upload_path, 'headerImage.png')
// convert and resize to png
return sharp(uploadedPath)
.resize(600)
.png({ quality: 99 })
.toFile(baseImgPath, (err) => {
if (err) {
log.error('[HEADER IMAGE] ' + err)
}
settingsController.set('header_image', baseImgPath)
2022-10-28 12:01:59 +02:00
res.sendStatus(200)
})
2019-11-06 11:31:56 +01:00
}
2022-10-28 12:01:59 +02:00
2019-04-03 00:25:12 +02:00
}
module.exports = settingsController