storage.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. const Model = require('objection').Model
  2. const autoload = require('auto-load')
  3. const path = require('path')
  4. const _ = require('lodash')
  5. /* global WIKI */
  6. /**
  7. * Storage model
  8. */
  9. module.exports = class Storage extends Model {
  10. static get tableName() { return 'storage' }
  11. static get jsonSchema () {
  12. return {
  13. type: 'object',
  14. required: ['key', 'title', 'isEnabled'],
  15. properties: {
  16. id: {type: 'integer'},
  17. key: {type: 'string'},
  18. title: {type: 'string'},
  19. isEnabled: {type: 'boolean'},
  20. mode: {type: 'string'},
  21. config: {type: 'object'}
  22. }
  23. }
  24. }
  25. static async getTargets() {
  26. return WIKI.db.storage.query()
  27. }
  28. static async refreshTargetsFromDisk() {
  29. try {
  30. const dbTargets = await WIKI.db.storage.query()
  31. const diskTargets = autoload(path.join(WIKI.SERVERPATH, 'modules/storage'))
  32. let newTargets = []
  33. _.forOwn(diskTargets, (target, targetKey) => {
  34. if (!_.some(dbTargets, ['key', target.key])) {
  35. newTargets.push({
  36. key: target.key,
  37. title: target.title,
  38. isEnabled: false,
  39. mode: 'push',
  40. config: _.reduce(target.props, (result, value, key) => {
  41. _.set(result, value, '')
  42. return result
  43. }, {})
  44. })
  45. }
  46. })
  47. if (newTargets.length > 0) {
  48. await WIKI.db.storage.query().insert(newTargets)
  49. WIKI.logger.info(`Loaded ${newTargets.length} new storage targets: [ OK ]`)
  50. } else {
  51. WIKI.logger.info(`No new storage targets found: [ SKIPPED ]`)
  52. }
  53. } catch (err) {
  54. WIKI.logger.error(`Failed to scan or load new storage providers: [ FAILED ]`)
  55. WIKI.logger.error(err)
  56. }
  57. }
  58. }