storage.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. const fs = require('fs-extra')
  2. const path = require('node:path')
  3. const tar = require('tar-fs')
  4. const zlib = require('node:zlib')
  5. const stream = require('node:stream')
  6. const _ = require('lodash')
  7. const util = require('node:util')
  8. const pipeline = util.promisify(stream.pipeline)
  9. const moment = require('moment')
  10. const pageHelper = require('../../../helpers/page')
  11. const commonDisk = require('./common')
  12. module.exports = {
  13. async activated() {
  14. // not used
  15. },
  16. async deactivated() {
  17. // not used
  18. },
  19. async init() {
  20. WIKI.logger.info('(STORAGE/DISK) Initializing...')
  21. await fs.ensureDir(this.config.path)
  22. WIKI.logger.info('(STORAGE/DISK) Initialization completed.')
  23. },
  24. async sync({ manual } = { manual: false }) {
  25. if (this.config.createDailyBackups || manual) {
  26. const dirPath = path.join(this.config.path, manual ? '_manual' : '_daily')
  27. await fs.ensureDir(dirPath)
  28. const dateFilename = moment().format(manual ? 'YYYYMMDD-HHmmss' : 'DD')
  29. WIKI.logger.info(`(STORAGE/DISK) Creating backup archive...`)
  30. await pipeline(
  31. tar.pack(this.config.path, {
  32. ignore: (filePath) => {
  33. return filePath.indexOf('_daily') >= 0 || filePath.indexOf('_manual') >= 0
  34. }
  35. }),
  36. zlib.createGzip(),
  37. fs.createWriteStream(path.join(dirPath, `wiki-${dateFilename}.tar.gz`))
  38. )
  39. WIKI.logger.info('(STORAGE/DISK) Backup archive created successfully.')
  40. }
  41. },
  42. async created(page) {
  43. WIKI.logger.info(`(STORAGE/DISK) Creating file [${page.localeCode}] ${page.path}...`)
  44. let fileName = `${page.path}.${pageHelper.getFileExtension(page.contentType)}`
  45. if (WIKI.config.lang.code !== page.localeCode) {
  46. fileName = `${page.localeCode}/${fileName}`
  47. }
  48. const filePath = path.join(this.config.path, fileName)
  49. await fs.outputFile(filePath, page.injectMetadata(), 'utf8')
  50. },
  51. async updated(page) {
  52. WIKI.logger.info(`(STORAGE/DISK) Updating file [${page.localeCode}] ${page.path}...`)
  53. let fileName = `${page.path}.${pageHelper.getFileExtension(page.contentType)}`
  54. if (WIKI.config.lang.code !== page.localeCode) {
  55. fileName = `${page.localeCode}/${fileName}`
  56. }
  57. const filePath = path.join(this.config.path, fileName)
  58. await fs.outputFile(filePath, page.injectMetadata(), 'utf8')
  59. },
  60. async deleted(page) {
  61. WIKI.logger.info(`(STORAGE/DISK) Deleting file [${page.localeCode}] ${page.path}...`)
  62. let fileName = `${page.path}.${pageHelper.getFileExtension(page.contentType)}`
  63. if (WIKI.config.lang.code !== page.localeCode) {
  64. fileName = `${page.localeCode}/${fileName}`
  65. }
  66. const filePath = path.join(this.config.path, fileName)
  67. await fs.unlink(filePath)
  68. },
  69. async renamed(page) {
  70. WIKI.logger.info(`(STORAGE/DISK) Renaming file [${page.localeCode}] ${page.path} to [${page.destinationLocaleCode}] ${page.destinationPath}...`)
  71. let sourceFilePath = `${page.path}.${pageHelper.getFileExtension(page.contentType)}`
  72. let destinationFilePath = `${page.destinationPath}.${pageHelper.getFileExtension(page.contentType)}`
  73. if (WIKI.config.lang.namespacing) {
  74. if (WIKI.config.lang.code !== page.localeCode) {
  75. sourceFilePath = `${page.localeCode}/${sourceFilePath}`
  76. }
  77. if (WIKI.config.lang.code !== page.destinationLocaleCode) {
  78. destinationFilePath = `${page.destinationLocaleCode}/${destinationFilePath}`
  79. }
  80. }
  81. await fs.move(path.join(this.config.path, sourceFilePath), path.join(this.config.path, destinationFilePath), { overwrite: true })
  82. },
  83. /**
  84. * ASSET UPLOAD
  85. *
  86. * @param {Object} asset Asset to upload
  87. */
  88. async assetUploaded (asset) {
  89. WIKI.logger.info(`(STORAGE/DISK) Creating new file ${asset.path}...`)
  90. await fs.outputFile(path.join(this.config.path, asset.path), asset.data)
  91. },
  92. /**
  93. * ASSET DELETE
  94. *
  95. * @param {Object} asset Asset to delete
  96. */
  97. async assetDeleted (asset) {
  98. WIKI.logger.info(`(STORAGE/DISK) Deleting file ${asset.path}...`)
  99. await fs.remove(path.join(this.config.path, asset.path))
  100. },
  101. /**
  102. * ASSET RENAME
  103. *
  104. * @param {Object} asset Asset to rename
  105. */
  106. async assetRenamed (asset) {
  107. WIKI.logger.info(`(STORAGE/DISK) Renaming file from ${asset.path} to ${asset.destinationPath}...`)
  108. await fs.move(path.join(this.config.path, asset.path), path.join(this.config.path, asset.destinationPath), { overwrite: true })
  109. },
  110. async getLocalLocation (asset) {
  111. return path.join(this.config.path, asset.path)
  112. },
  113. /**
  114. * HANDLERS
  115. */
  116. async dump() {
  117. WIKI.logger.info(`(STORAGE/DISK) Dumping all content to disk...`)
  118. // -> Pages
  119. await pipeline(
  120. WIKI.db.knex.column('path', 'localeCode', 'title', 'description', 'contentType', 'content', 'isPublished', 'updatedAt', 'createdAt').select().from('pages').where({
  121. isPrivate: false
  122. }).stream(),
  123. new stream.Transform({
  124. objectMode: true,
  125. transform: async (page, enc, cb) => {
  126. let fileName = `${page.path}.${pageHelper.getFileExtension(page.contentType)}`
  127. if (WIKI.config.lang.code !== page.localeCode) {
  128. fileName = `${page.localeCode}/${fileName}`
  129. }
  130. WIKI.logger.info(`(STORAGE/DISK) Dumping page ${fileName}...`)
  131. const filePath = path.join(this.config.path, fileName)
  132. await fs.outputFile(filePath, pageHelper.injectPageMetadata(page), 'utf8')
  133. cb()
  134. }
  135. })
  136. )
  137. // -> Assets
  138. const assetFolders = await WIKI.db.assetFolders.getAllPaths()
  139. await pipeline(
  140. WIKI.db.knex.column('filename', 'folderId', 'data').select().from('assets').join('assetData', 'assets.id', '=', 'assetData.id').stream(),
  141. new stream.Transform({
  142. objectMode: true,
  143. transform: async (asset, enc, cb) => {
  144. const filename = (asset.folderId && asset.folderId > 0) ? `${_.get(assetFolders, asset.folderId)}/${asset.filename}` : asset.filename
  145. WIKI.logger.info(`(STORAGE/DISK) Dumping asset ${filename}...`)
  146. await fs.outputFile(path.join(this.config.path, filename), asset.data)
  147. cb()
  148. }
  149. })
  150. )
  151. WIKI.logger.info('(STORAGE/DISK) All content was dumped to disk successfully.')
  152. },
  153. async backup() {
  154. return this.sync({ manual: true })
  155. },
  156. async importAll() {
  157. WIKI.logger.info(`(STORAGE/DISK) Importing all content from local disk folder to the DB...`)
  158. await commonDisk.importFromDisk({
  159. fullPath: this.config.path,
  160. moduleName: 'DISK'
  161. })
  162. WIKI.logger.info('(STORAGE/DISK) Import completed.')
  163. }
  164. }