wikijs-fork/server/models/storage.js

111 lines
3.2 KiB
JavaScript
Raw Normal View History

2018-06-26 02:04:47 +00:00
const Model = require('objection').Model
const path = require('path')
const fs = require('fs-extra')
2018-06-26 02:04:47 +00:00
const _ = require('lodash')
const yaml = require('js-yaml')
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' }
2018-09-08 19:49:36 +00:00
static get idColumn() { return 'key' }
2018-06-26 02:04:47 +00:00
static get jsonSchema () {
return {
type: 'object',
2018-08-04 21:27:55 +00:00
required: ['key', 'isEnabled'],
2018-06-26 02:04:47 +00:00
properties: {
key: {type: 'string'},
isEnabled: {type: 'boolean'},
mode: {type: 'string'},
config: {type: 'object'}
}
}
}
static async getTargets() {
return WIKI.models.storage.query()
2018-06-26 02:04:47 +00:00
}
static async refreshTargetsFromDisk() {
let trx
2018-06-26 02:04:47 +00:00
try {
const dbTargets = await WIKI.models.storage.query()
// -> 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))
}
WIKI.data.storage = diskTargets.map(target => ({
...target,
2018-08-04 21:27:55 +00:00
props: commonHelper.parseModuleProps(target.props)
}))
// -> Insert new targets
2018-06-26 02:04:47 +00:00
let newTargets = []
for (let target of WIKI.data.storage) {
2018-06-26 02:04:47 +00:00
if (!_.some(dbTargets, ['key', target.key])) {
newTargets.push({
key: target.key,
isEnabled: false,
mode: 'push',
config: _.transform(target.props, (result, value, key) => {
_.set(result, key, value.default)
2018-06-26 02:04:47 +00:00
return result
}, {})
})
} else {
const targetConfig = _.get(_.find(dbTargets, ['key', target.key]), 'config', {})
await WIKI.models.storage.query().patch({
config: _.transform(target.props, (result, value, key) => {
if (!_.has(result, key)) {
_.set(result, key, value.default)
}
return result
}, targetConfig)
}).where('key', target.key)
2018-06-26 02:04:47 +00:00
}
}
2018-06-26 02:04:47 +00:00
if (newTargets.length > 0) {
trx = await WIKI.models.Objection.transaction.start(WIKI.models.knex)
for (let target of newTargets) {
await WIKI.models.storage.query(trx).insert(target)
}
await trx.commit()
2018-06-26 02:04:47 +00:00
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)
if (trx) {
trx.rollback()
}
2018-06-26 02:04:47 +00:00
}
}
static async pageEvent({ event, page }) {
const targets = await WIKI.models.storage.query().where('isEnabled', true)
if (targets && targets.length > 0) {
_.forEach(targets, target => {
WIKI.queue.job.syncStorage.add({
event,
target,
page
}, {
removeOnComplete: true
})
})
}
}
2018-06-26 02:04:47 +00:00
}