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.

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