2018-06-26 02:04:47 +00:00
|
|
|
const Model = require('objection').Model
|
|
|
|
const path = require('path')
|
2018-07-08 05:12:43 +00:00
|
|
|
const fs = require('fs-extra')
|
2018-06-26 02:04:47 +00:00
|
|
|
const _ = require('lodash')
|
2018-07-08 05:12:43 +00:00
|
|
|
const yaml = require('js-yaml')
|
2018-07-01 23:50:42 +00:00
|
|
|
const commonHelper = require('../../helpers/common')
|
2018-06-26 02:04:47 +00:00
|
|
|
|
|
|
|
/* global WIKI */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Storage model
|
|
|
|
*/
|
|
|
|
module.exports = class Storage extends Model {
|
|
|
|
static get tableName() { return 'storage' }
|
|
|
|
|
|
|
|
static get jsonSchema () {
|
|
|
|
return {
|
|
|
|
type: 'object',
|
|
|
|
required: ['key', 'title', 'isEnabled'],
|
|
|
|
|
|
|
|
properties: {
|
|
|
|
id: {type: 'integer'},
|
|
|
|
key: {type: 'string'},
|
|
|
|
title: {type: 'string'},
|
|
|
|
isEnabled: {type: 'boolean'},
|
|
|
|
mode: {type: 'string'},
|
|
|
|
config: {type: 'object'}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static async getTargets() {
|
|
|
|
return WIKI.db.storage.query()
|
|
|
|
}
|
|
|
|
|
|
|
|
static async refreshTargetsFromDisk() {
|
|
|
|
try {
|
|
|
|
const dbTargets = await WIKI.db.storage.query()
|
2018-07-08 05:12:43 +00:00
|
|
|
|
|
|
|
// -> Fetch definitions from disk
|
|
|
|
const storageDirs = await fs.readdir(path.join(WIKI.SERVERPATH, 'modules/storage'))
|
|
|
|
let diskTargets = []
|
|
|
|
for (let dir of storageDirs) {
|
|
|
|
const def = await fs.readFile(path.join(WIKI.SERVERPATH, 'modules/storage', dir, 'definition.yml'), 'utf8')
|
|
|
|
diskTargets.push(yaml.safeLoad(def))
|
|
|
|
}
|
|
|
|
|
|
|
|
// -> Insert new targets
|
2018-06-26 02:04:47 +00:00
|
|
|
let newTargets = []
|
2018-07-08 05:12:43 +00:00
|
|
|
_.forEach(diskTargets, target => {
|
2018-06-26 02:04:47 +00:00
|
|
|
if (!_.some(dbTargets, ['key', target.key])) {
|
|
|
|
newTargets.push({
|
|
|
|
key: target.key,
|
|
|
|
title: target.title,
|
|
|
|
isEnabled: false,
|
|
|
|
mode: 'push',
|
2018-07-01 23:50:42 +00:00
|
|
|
config: _.transform(target.props, (result, value, key) => {
|
|
|
|
if (_.isPlainObject(value)) {
|
|
|
|
let cfgValue = {
|
2018-07-08 05:12:43 +00:00
|
|
|
type: value.type.toLowerCase(),
|
|
|
|
value: !_.isNil(value.default) ? value.default : commonHelper.getTypeDefaultValue(value.type)
|
2018-07-01 23:50:42 +00:00
|
|
|
}
|
|
|
|
if (_.isArray(value.enum)) {
|
|
|
|
cfgValue.enum = value.enum
|
|
|
|
}
|
|
|
|
_.set(result, key, cfgValue)
|
|
|
|
} else {
|
|
|
|
_.set(result, key, {
|
2018-07-08 05:12:43 +00:00
|
|
|
type: value.toLowerCase(),
|
2018-07-01 23:50:42 +00:00
|
|
|
value: commonHelper.getTypeDefaultValue(value)
|
|
|
|
})
|
|
|
|
}
|
2018-06-26 02:04:47 +00:00
|
|
|
return result
|
|
|
|
}, {})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if (newTargets.length > 0) {
|
|
|
|
await WIKI.db.storage.query().insert(newTargets)
|
|
|
|
WIKI.logger.info(`Loaded ${newTargets.length} new storage targets: [ OK ]`)
|
|
|
|
} else {
|
|
|
|
WIKI.logger.info(`No new storage targets found: [ SKIPPED ]`)
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
WIKI.logger.error(`Failed to scan or load new storage providers: [ FAILED ]`)
|
|
|
|
WIKI.logger.error(err)
|
|
|
|
}
|
|
|
|
}
|
2018-07-22 20:25:39 +00:00
|
|
|
|
2018-07-23 01:13:01 +00:00
|
|
|
static async pageEvent(event, page) {
|
2018-07-22 20:25:39 +00:00
|
|
|
const targets = await WIKI.db.storage.query().where('isEnabled', true)
|
|
|
|
if (targets && targets.length > 0) {
|
|
|
|
_.forEach(targets, target => {
|
|
|
|
WIKI.queue.job.syncStorage.add({
|
2018-07-23 01:13:01 +00:00
|
|
|
event,
|
2018-07-22 20:25:39 +00:00
|
|
|
target,
|
|
|
|
page
|
|
|
|
}, {
|
|
|
|
removeOnComplete: true
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2018-06-26 02:04:47 +00:00
|
|
|
}
|