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.

104 lines
3.0 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 jsonSchema () {
  14. return {
  15. type: 'object',
  16. required: ['key', 'title', 'isEnabled'],
  17. properties: {
  18. id: {type: 'integer'},
  19. key: {type: 'string'},
  20. title: {type: 'string'},
  21. isEnabled: {type: 'boolean'},
  22. mode: {type: 'string'},
  23. config: {type: 'object'}
  24. }
  25. }
  26. }
  27. static async getTargets() {
  28. return WIKI.db.storage.query()
  29. }
  30. static async refreshTargetsFromDisk() {
  31. try {
  32. const dbTargets = await WIKI.db.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. // -> Insert new targets
  41. let newTargets = []
  42. _.forEach(diskTargets, target => {
  43. if (!_.some(dbTargets, ['key', target.key])) {
  44. newTargets.push({
  45. key: target.key,
  46. title: target.title,
  47. isEnabled: false,
  48. mode: 'push',
  49. config: _.transform(target.props, (result, value, key) => {
  50. if (_.isPlainObject(value)) {
  51. let cfgValue = {
  52. type: value.type.toLowerCase(),
  53. value: !_.isNil(value.default) ? value.default : commonHelper.getTypeDefaultValue(value.type)
  54. }
  55. if (_.isArray(value.enum)) {
  56. cfgValue.enum = value.enum
  57. }
  58. _.set(result, key, cfgValue)
  59. } else {
  60. _.set(result, key, {
  61. type: value.toLowerCase(),
  62. value: commonHelper.getTypeDefaultValue(value)
  63. })
  64. }
  65. return result
  66. }, {})
  67. })
  68. }
  69. })
  70. if (newTargets.length > 0) {
  71. await WIKI.db.storage.query().insert(newTargets)
  72. WIKI.logger.info(`Loaded ${newTargets.length} new storage targets: [ OK ]`)
  73. } else {
  74. WIKI.logger.info(`No new storage targets found: [ SKIPPED ]`)
  75. }
  76. } catch (err) {
  77. WIKI.logger.error(`Failed to scan or load new storage providers: [ FAILED ]`)
  78. WIKI.logger.error(err)
  79. }
  80. }
  81. static async pageEvent(event, page) {
  82. const targets = await WIKI.db.storage.query().where('isEnabled', true)
  83. if (targets && targets.length > 0) {
  84. _.forEach(targets, target => {
  85. WIKI.queue.job.syncStorage.add({
  86. event,
  87. target,
  88. page
  89. }, {
  90. removeOnComplete: true
  91. })
  92. })
  93. }
  94. }
  95. }