Verified Commit b63f22fd authored by les's avatar les
Browse files

ok lot of smtp minors

parent 858d6b31
......@@ -39,7 +39,8 @@ const defaultSettings = {
{ href: '/', label: 'home' },
{ href: '/about', label: 'about' }
],
admin_email: config.admin_email || ''
admin_email: config.admin_email || '',
smtp: config.smtp || false
}
/**
......@@ -127,6 +128,19 @@ const settingsController = {
if (ret) { res.sendStatus(200) } else { res.sendStatus(400) }
},
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')
return res.sendStatus(200)
} catch (e) {
console.error(e)
return res.status(400).send(String(e))
}
},
setLogo (req, res) {
if (!req.file) {
settingsController.set('logo', false)
......
const URL = require('url')
const helpers = require('../../helpers.js')
const log = require('../../log')
const db = require('../models/index.js')
const config = require('../../config')
const settingsController = require('./settings')
const crypto = require('crypto')
const { promisify } = require('util')
const randomBytes = promisify(crypto.randomBytes)
async function randomString (len = 16) {
const bytes = await randomBytes(len * 8)
return crypto
.createHash('sha1')
.update(bytes)
.digest('hex')
}
const setupController = {
......@@ -62,8 +53,8 @@ const setupController = {
await settingsController.set('hostname', new URL.URL(settingsController.settings.baseurl).hostname)
// create admin
const password = await randomString()
const email = `admin@${settingsController.settings.hostname}`
const password = helpers.randomString()
const email = `admin`
const User = require('../models/user')
await User.create({
email,
......
......@@ -17,6 +17,7 @@ if (config.firstrun) {
api.post('/settings', settingsController.setRequest)
api.post('/setup/db', setupController.setupDb)
api.post('/setup/restart', setupController.restart)
api.post('/settings/smtp', settingsController.testSMTP)
} else {
......
const Email = require('email-templates')
const path = require('path')
const moment = require('dayjs')
const config = require('../config')
const settings = require('./controller/settings').settings
const settingsController = require('./controller/settings')
const log = require('../log')
const { Task, TaskManager } = require('../taskManager')
const locales = require('../../locales')
const mail = {
send (addresses, template, locals, locale = settings.instance_locale) {
send (addresses, template, locals, locale = settingsController.settings.instance_locale) {
log.debug('Enqueue new email ', template, locale)
const task = new Task({
name: 'MAIL',
......@@ -19,6 +18,7 @@ const mail = {
},
_send (addresses, template, locals, locale) {
const settings = settingsController.settings
log.info(`Send ${template} email to ${addresses} with locale ${locale}`)
const email = new Email({
views: { root: path.join(__dirname, '..', 'emails') },
......@@ -49,8 +49,7 @@ const mail = {
const msg = {
template,
message: {
to: addresses,
bcc: settings.admin_email
to: addresses
},
locals: {
...locals,
......@@ -62,6 +61,7 @@ const mail = {
return email.send(msg)
.catch(e => {
log.error('[MAIL]', e)
throw e
})
}
}
......
const Sequelize = require('sequelize')
const Umzug = require('umzug')
const path = require('path')
const config = require('../../config')
const log = require('../../log')
const db = {
sequelize: null,
close () {
return db.sequelize.close()
if (db.sequelize) {
return db.sequelize.close()
}
},
async connect (dbConf = config.db) {
log.debug(`Connecting to DB: ${dbConf}`)
connect (dbConf = config.db) {
log.debug(`Connecting to DB: ${JSON.stringify(dbConf)}`)
db.sequelize = new Sequelize(dbConf)
await db.sequelize.authenticate()
return db.sequelize
return db.sequelize.authenticate()
},
async isEmpty () {
const users = await db.sequelize.query('SELECT * from users').catch(e => {})
return !(users && users.length)
},
async runMigrations () {
const logging = config.firstrun ? false : log.debug.bind(log)
const umzug = new Umzug({
storage: 'sequelize',
storageOptions: { sequelize: db.sequelize },
logging,
migrations: {
wrap: fun => {
return () =>
fun(db.sequelize.queryInterface, Sequelize).catch(e => {
// log.error(e)
return false
})
},
path: path.resolve(__dirname, '..', '..', 'migrations')
}
})
return await umzug.up()
}
}
if (!config.firstrun) {
try {
db.connect()
db.connect().then(e => {
log.debug('Running migrations')
db.runMigrations()
})
} catch (e) {
log.warn(` ⚠️ Cannot connect to db, check your configuration => ${e}`)
process.exit(1)
......
......@@ -9,15 +9,13 @@ if (process.env.NODE_ENV === 'production') {
process.chdir(path.resolve(__dirname, '..'))
}
async function start (options) {
try {
require('./config')
} catch (e) {
console.error(e)
process.exit(-1)
}
require('./index')
async function start () {
const suffix = require('../package.json').name.includes('-edge') ? '-edge' : ''
require('@nuxt/cli' + suffix).run(['start', '--modern'])
.catch((error) => {
console.error(error)
process.exit(2)
})
}
console.info(`📅 ${pkg.name} - v${pkg.version} - ${pkg.description} (nodejs: ${process.version}, ENV: ${process.env.NODE_ENV})`)
......
......@@ -9,9 +9,9 @@ let config = {
port: 13120
},
log_level: 'debug',
log_path: path.resolve(process.env.cwd, 'logs'),
log_path: path.resolve(process.env.cwd || '', 'logs'),
db: {},
upload_path: path.resolve(process.env.cwd, 'uploads'),
upload_path: path.resolve(process.env.cwd || '', 'uploads'),
write (config_path= process.env.config_path || './config.json') {
return fs.writeFileSync(config_path, JSON.stringify(config, null, 2))
},
......
......@@ -47,6 +47,14 @@ domPurify.addHook('beforeSanitizeElements', node => {
})
module.exports = {
randomString (length = 12) {
const wishlist = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
return Array.from(crypto.randomFillSync(new Uint32Array(length)))
.map(x => wishlist[x % wishlist.length])
.join('')
},
sanitizeHTML (html) {
return domPurify.sanitize(html, {
ALLOWED_TAGS: ['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'br',
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment