storage.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. const fs = require('fs-extra')
  2. const path = require('path')
  3. /**
  4. * Get file extension based on content type
  5. */
  6. const getFileExtension = (contentType) => {
  7. switch (contentType) {
  8. case 'markdown':
  9. return 'md'
  10. case 'html':
  11. return 'html'
  12. default:
  13. return 'txt'
  14. }
  15. }
  16. module.exports = {
  17. async activated() {
  18. // not used
  19. },
  20. async deactivated() {
  21. // not used
  22. },
  23. async init() {
  24. WIKI.logger.info('(STORAGE/DISK) Initializing...')
  25. await fs.ensureDir(this.config.path)
  26. WIKI.logger.info('(STORAGE/DISK) Initialization completed.')
  27. },
  28. async sync() {
  29. // not used
  30. },
  31. async created(page) {
  32. WIKI.logger.info(`(STORAGE/DISK) Creating file ${page.path}...`)
  33. const filePath = path.join(this.config.path, `${page.path}.${getFileExtension(page.contentType)}`)
  34. await fs.outputFile(filePath, page.injectMetadata(), 'utf8')
  35. },
  36. async updated(page) {
  37. WIKI.logger.info(`(STORAGE/DISK) Updating file ${page.path}...`)
  38. const filePath = path.join(this.config.path, `${page.path}.${getFileExtension(page.contentType)}`)
  39. await fs.outputFile(filePath, page.injectMetadata(), 'utf8')
  40. },
  41. async deleted(page) {
  42. WIKI.logger.info(`(STORAGE/DISK) Deleting file ${page.path}...`)
  43. const filePath = path.join(this.config.path, `${page.path}.${getFileExtension(page.contentType)}`)
  44. await fs.unlink(filePath)
  45. },
  46. async renamed(page) {
  47. WIKI.logger.info(`(STORAGE/DISK) Renaming file ${page.sourcePath} to ${page.destinationPath}...`)
  48. const sourceFilePath = path.join(this.config.path, `${page.sourcePath}.${getFileExtension(page.contentType)}`)
  49. const destinationFilePath = path.join(this.config.path, `${page.destinationPath}.${getFileExtension(page.contentType)}`)
  50. await fs.move(sourceFilePath, destinationFilePath, { overwrite: true })
  51. }
  52. }