feat: admin storage - save + list

This commit is contained in:
NGPixel
2018-06-25 22:04:47 -04:00
parent 2500d8b054
commit bb102c3356
11 changed files with 311 additions and 40 deletions

View File

@@ -95,6 +95,7 @@ exports.up = knex => {
table.string('key').notNullable().unique()
table.string('title').notNullable()
table.boolean('isEnabled').notNullable().defaultTo(false)
table.enum('mode', ['sync', 'push', 'pull']).notNullable().defaultTo('push')
table.jsonb('config')
})
// TAGS --------------------------------

View File

@@ -0,0 +1,64 @@
const Model = require('objection').Model
const autoload = require('auto-load')
const path = require('path')
const _ = require('lodash')
/* 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()
const diskTargets = autoload(path.join(WIKI.SERVERPATH, 'modules/storage'))
let newTargets = []
_.forOwn(diskTargets, (target, targetKey) => {
if (!_.some(dbTargets, ['key', target.key])) {
newTargets.push({
key: target.key,
title: target.title,
isEnabled: false,
mode: 'push',
config: _.reduce(target.props, (result, value, key) => {
_.set(result, value, '')
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)
}
}
}

View File

@@ -0,0 +1,48 @@
const _ = require('lodash')
const graphHelper = require('../../helpers/graph')
/* global WIKI */
module.exports = {
Query: {
async storage() { return {} }
},
Mutation: {
async storage() { return {} }
},
StorageQuery: {
async targets(obj, args, context, info) {
let targets = await WIKI.db.storage.getTargets()
targets = targets.map(stg => ({
...stg,
config: _.transform(stg.config, (res, value, key) => {
res.push({ key, value })
}, [])
}))
if (args.filter) { targets = graphHelper.filter(targets, args.filter) }
if (args.orderBy) { targets = graphHelper.orderBy(targets, args.orderBy) }
return targets
}
},
StorageMutation: {
async updateTargets(obj, args, context) {
try {
for (let tgt of args.targets) {
await WIKI.db.storage.query().patch({
isEnabled: tgt.isEnabled,
mode: tgt.mode,
config: _.reduce(tgt.config, (result, value, key) => {
_.set(result, value.key, value.value)
return result
}, {})
}).where('key', tgt.key)
}
return {
responseResult: graphHelper.generateSuccess('Storage targets updated successfully')
}
} catch (err) {
return graphHelper.generateError(err)
}
}
}
}

View File

@@ -0,0 +1,51 @@
# ===============================================
# STORAGE
# ===============================================
extend type Query {
storage: StorageQuery
}
extend type Mutation {
storage: StorageMutation
}
# -----------------------------------------------
# QUERIES
# -----------------------------------------------
type StorageQuery {
targets(
filter: String
orderBy: String
): [StorageTarget]
}
# -----------------------------------------------
# MUTATIONS
# -----------------------------------------------
type StorageMutation {
updateTargets(
targets: [StorageTargetInput]
): DefaultResponse
}
# -----------------------------------------------
# TYPES
# -----------------------------------------------
type StorageTarget {
isEnabled: Boolean!
key: String!
title: String!
mode: String
config: [KeyValuePair]
}
input StorageTargetInput {
isEnabled: Boolean!
key: String!
mode: String!
config: [KeyValuePairInput]
}

View File

@@ -1,7 +1,7 @@
module.exports = {
key: 'digitalocean',
title: 'DigialOcean Spaces',
props: [],
props: ['accessKeyId', 'accessSecret', 'region', 'bucket'],
activate() {
},

View File

@@ -319,6 +319,9 @@ module.exports = () => {
await WIKI.db.editors.refreshEditorsFromDisk()
await WIKI.db.editors.query().patch({ isEnabled: true }).where('key', 'markdown')
// Load storage targets
await WIKI.db.storage.refreshTargetsFromDisk()
// Create root administrator
WIKI.logger.info('Creating root administrator...')
await WIKI.db.users.query().delete().where({