storage.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. const S3 = require('aws-sdk/clients/s3')
  2. const stream = require('node:stream')
  3. const util = require('node:util')
  4. const pipeline = util.promisify(stream.pipeline)
  5. const _ = require('lodash')
  6. const pageHelper = require('../../../helpers/page.js')
  7. /**
  8. * Deduce the file path given the `page` object and the object's key to the page's path.
  9. */
  10. const getFilePath = (page, pathKey) => {
  11. const fileName = `${page[pathKey]}.${pageHelper.getFileExtension(page.contentType)}`
  12. const withLocaleCode = WIKI.config.lang.namespacing && WIKI.config.lang.code !== page.localeCode
  13. return withLocaleCode ? `${page.localeCode}/${fileName}` : fileName
  14. }
  15. /**
  16. * Can be used with S3 compatible storage.
  17. */
  18. module.exports = class S3CompatibleStorage {
  19. constructor(storageName) {
  20. this.storageName = storageName
  21. }
  22. async activated() {
  23. // not used
  24. }
  25. async deactivated() {
  26. // not used
  27. }
  28. async init() {
  29. WIKI.logger.info(`(STORAGE/${this.storageName}) Initializing...`)
  30. const { accessKeyId, secretAccessKey, bucket } = this.config
  31. const s3Config = {
  32. accessKeyId,
  33. secretAccessKey,
  34. params: { Bucket: bucket },
  35. apiVersions: '2006-03-01'
  36. }
  37. if (!_.isNil(this.config.region)) {
  38. s3Config.region = this.config.region
  39. }
  40. if (!_.isNil(this.config.endpoint)) {
  41. s3Config.endpoint = this.config.endpoint
  42. }
  43. if (!_.isNil(this.config.sslEnabled)) {
  44. s3Config.sslEnabled = this.config.sslEnabled
  45. }
  46. if (!_.isNil(this.config.s3ForcePathStyle)) {
  47. s3Config.s3ForcePathStyle = this.config.s3ForcePathStyle
  48. }
  49. if (!_.isNil(this.config.s3BucketEndpoint)) {
  50. s3Config.s3BucketEndpoint = this.config.s3BucketEndpoint
  51. }
  52. this.s3 = new S3(s3Config)
  53. // determine if a bucket exists and you have permission to access it
  54. await this.s3.headBucket().promise()
  55. WIKI.logger.info(`(STORAGE/${this.storageName}) Initialization completed.`)
  56. }
  57. async created(page) {
  58. WIKI.logger.info(`(STORAGE/${this.storageName}) Creating file ${page.path}...`)
  59. const filePath = getFilePath(page, 'path')
  60. await this.s3.putObject({ Key: filePath, Body: page.injectMetadata() }).promise()
  61. }
  62. async updated(page) {
  63. WIKI.logger.info(`(STORAGE/${this.storageName}) Updating file ${page.path}...`)
  64. const filePath = getFilePath(page, 'path')
  65. await this.s3.putObject({ Key: filePath, Body: page.injectMetadata() }).promise()
  66. }
  67. async deleted(page) {
  68. WIKI.logger.info(`(STORAGE/${this.storageName}) Deleting file ${page.path}...`)
  69. const filePath = getFilePath(page, 'path')
  70. await this.s3.deleteObject({ Key: filePath }).promise()
  71. }
  72. async renamed(page) {
  73. WIKI.logger.info(`(STORAGE/${this.storageName}) Renaming file ${page.path} to ${page.destinationPath}...`)
  74. let sourceFilePath = getFilePath(page, 'path')
  75. let destinationFilePath = getFilePath(page, 'destinationPath')
  76. if (WIKI.config.lang.namespacing) {
  77. if (WIKI.config.lang.code !== page.localeCode) {
  78. sourceFilePath = `${page.localeCode}/${sourceFilePath}`
  79. }
  80. if (WIKI.config.lang.code !== page.destinationLocaleCode) {
  81. destinationFilePath = `${page.destinationLocaleCode}/${destinationFilePath}`
  82. }
  83. }
  84. await this.s3.copyObject({ CopySource: sourceFilePath, Key: destinationFilePath }).promise()
  85. await this.s3.deleteObject({ Key: sourceFilePath }).promise()
  86. }
  87. /**
  88. * ASSET UPLOAD
  89. *
  90. * @param {Object} asset Asset to upload
  91. */
  92. async assetUploaded (asset) {
  93. WIKI.logger.info(`(STORAGE/${this.storageName}) Creating new file ${asset.path}...`)
  94. await this.s3.putObject({ Key: asset.path, Body: asset.data }).promise()
  95. }
  96. /**
  97. * ASSET DELETE
  98. *
  99. * @param {Object} asset Asset to delete
  100. */
  101. async assetDeleted (asset) {
  102. WIKI.logger.info(`(STORAGE/${this.storageName}) Deleting file ${asset.path}...`)
  103. await this.s3.deleteObject({ Key: asset.path }).promise()
  104. }
  105. /**
  106. * ASSET RENAME
  107. *
  108. * @param {Object} asset Asset to rename
  109. */
  110. async assetRenamed (asset) {
  111. WIKI.logger.info(`(STORAGE/${this.storageName}) Renaming file from ${asset.path} to ${asset.destinationPath}...`)
  112. await this.s3.copyObject({ CopySource: asset.path, Key: asset.destinationPath }).promise()
  113. await this.s3.deleteObject({ Key: asset.path }).promise()
  114. }
  115. async getLocalLocation () {
  116. }
  117. /**
  118. * HANDLERS
  119. */
  120. async exportAll() {
  121. WIKI.logger.info(`(STORAGE/${this.storageName}) Exporting all content to the cloud provider...`)
  122. // -> Pages
  123. await pipeline(
  124. WIKI.db.knex.column('path', 'localeCode', 'title', 'description', 'contentType', 'content', 'isPublished', 'updatedAt', 'createdAt').select().from('pages').where({
  125. isPrivate: false
  126. }).stream(),
  127. new stream.Transform({
  128. objectMode: true,
  129. transform: async (page, enc, cb) => {
  130. const filePath = getFilePath(page, 'path')
  131. WIKI.logger.info(`(STORAGE/${this.storageName}) Adding page ${filePath}...`)
  132. await this.s3.putObject({ Key: filePath, Body: pageHelper.injectPageMetadata(page) }).promise()
  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/${this.storageName}) Adding asset ${filename}...`)
  146. await this.s3.putObject({ Key: filename, Body: asset.data }).promise()
  147. cb()
  148. }
  149. })
  150. )
  151. WIKI.logger.info(`(STORAGE/${this.storageName}) All content has been pushed to the cloud provider.`)
  152. }
  153. }