You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.2 KiB

  1. const Model = require('objection').Model
  2. const path = require('path')
  3. const fs = require('fs-extra')
  4. const _ = require('lodash')
  5. const yaml = require('js-yaml')
  6. const commonHelper = require('../helpers/common')
  7. /* global WIKI */
  8. /**
  9. * Storage model
  10. */
  11. module.exports = class Storage extends Model {
  12. static get tableName() { return 'storage' }
  13. static get idColumn() { return 'key' }
  14. static get jsonSchema () {
  15. return {
  16. type: 'object',
  17. required: ['key', 'isEnabled'],
  18. properties: {
  19. key: {type: 'string'},
  20. isEnabled: {type: 'boolean'},
  21. mode: {type: 'string'},
  22. config: {type: 'object'}
  23. }
  24. }
  25. }
  26. static async getTargets() {
  27. return WIKI.models.storage.query()
  28. }
  29. static async refreshTargetsFromDisk() {
  30. let trx
  31. try {
  32. const dbTargets = await WIKI.models.storage.query()
  33. // -> Fetch definitions from disk
  34. const storageDirs = await fs.readdir(path.join(WIKI.SERVERPATH, 'modules/storage'))
  35. let diskTargets = []
  36. for (let dir of storageDirs) {
  37. const def = await fs.readFile(path.join(WIKI.SERVERPATH, 'modules/storage', dir, 'definition.yml'), 'utf8')
  38. diskTargets.push(yaml.safeLoad(def))
  39. }
  40. WIKI.data.storage = diskTargets.map(target => ({
  41. ...target,
  42. props: commonHelper.parseModuleProps(target.props)
  43. }))
  44. // -> Insert new targets
  45. let newTargets = []
  46. for (let target of WIKI.data.storage) {
  47. if (!_.some(dbTargets, ['key', target.key])) {
  48. newTargets.push({
  49. key: target.key,
  50. isEnabled: false,
  51. mode: 'push',
  52. config: _.transform(target.props, (result, value, key) => {
  53. _.set(result, key, value.default)
  54. return result
  55. }, {})
  56. })
  57. } else {
  58. const targetConfig = _.get(_.find(dbTargets, ['key', target.key]), 'config', {})
  59. await WIKI.models.storage.query().patch({
  60. config: _.transform(target.props, (result, value, key) => {
  61. if (!_.has(result, key)) {
  62. _.set(result, key, value.default)
  63. }
  64. return result
  65. }, targetConfig)
  66. }).where('key', target.key)
  67. }
  68. }
  69. if (newTargets.length > 0) {
  70. trx = await WIKI.models.Objection.transaction.start(WIKI.models.knex)
  71. for (let target of newTargets) {
  72. await WIKI.models.storage.query(trx).insert(target)
  73. }
  74. await trx.commit()
  75. WIKI.logger.info(`Loaded ${newTargets.length} new storage targets: [ OK ]`)
  76. } else {
  77. WIKI.logger.info(`No new storage targets found: [ SKIPPED ]`)
  78. }
  79. } catch (err) {
  80. WIKI.logger.error(`Failed to scan or load new storage providers: [ FAILED ]`)
  81. WIKI.logger.error(err)
  82. if (trx) {
  83. trx.rollback()
  84. }
  85. }
  86. }
  87. static async pageEvent({ event, page }) {
  88. const targets = await WIKI.models.storage.query().where('isEnabled', true)
  89. if (targets && targets.length > 0) {
  90. _.forEach(targets, target => {
  91. WIKI.queue.job.syncStorage.add({
  92. event,
  93. target,
  94. page
  95. }, {
  96. removeOnComplete: true
  97. })
  98. })
  99. }
  100. }
  101. }