common.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. const S3 = require('aws-sdk/clients/s3')
  2. const pageHelper = require('../../../helpers/page.js')
  3. /* global WIKI */
  4. /**
  5. * Deduce the file path given the `page` object and the object's key to the page's path.
  6. */
  7. const getFilePath = (page, pathKey) => {
  8. const fileName = `${page[pathKey]}.${pageHelper.getFileExtension(page.contentType)}`
  9. const withLocaleCode = WIKI.config.lang.namespacing && WIKI.config.lang.code !== page.localeCode
  10. return withLocaleCode ? `${page.localeCode}/${fileName}` : fileName
  11. }
  12. /**
  13. * Can be used with S3 compatible storage.
  14. */
  15. module.exports = class S3CompatibleStorage {
  16. constructor(storageName) {
  17. this.storageName = storageName
  18. }
  19. async activated() {
  20. // not used
  21. }
  22. async deactivated() {
  23. // not used
  24. }
  25. async init() {
  26. WIKI.logger.info(`(STORAGE/${this.storageName}) Initializing...`)
  27. const { accessKeyId, secretAccessKey, region, bucket, endpoint } = this.config
  28. this.s3 = new S3({
  29. accessKeyId,
  30. secretAccessKey,
  31. region,
  32. endpoint,
  33. params: { Bucket: bucket },
  34. apiVersions: '2006-03-01'
  35. })
  36. // determine if a bucket exists and you have permission to access it
  37. await this.s3.headBucket().promise()
  38. WIKI.logger.info(`(STORAGE/${this.storageName}) Initialization completed.`)
  39. }
  40. async created(page) {
  41. WIKI.logger.info(`(STORAGE/${this.storageName}) Creating file ${page.path}...`)
  42. const filePath = getFilePath(page, 'path')
  43. await this.s3.putObject({ Key: filePath, Body: page.injectMetadata() }).promise()
  44. }
  45. async updated(page) {
  46. WIKI.logger.info(`(STORAGE/${this.storageName}) Updating file ${page.path}...`)
  47. const filePath = getFilePath(page, 'path')
  48. await this.s3.putObject({ Key: filePath, Body: page.injectMetadata() }).promise()
  49. }
  50. async deleted(page) {
  51. WIKI.logger.info(`(STORAGE/${this.storageName}) Deleting file ${page.path}...`)
  52. const filePath = getFilePath(page, 'path')
  53. await this.s3.deleteObject({ Key: filePath }).promise()
  54. }
  55. async renamed(page) {
  56. WIKI.logger.info(`(STORAGE/${this.storageName}) Renaming file ${page.path} to ${page.destinationPath}...`)
  57. let sourceFilePath = `${page.path}.${page.getFileExtension()}`
  58. let destinationFilePath = `${page.destinationPath}.${page.getFileExtension()}`
  59. if (WIKI.config.lang.namespacing) {
  60. if (WIKI.config.lang.code !== page.localeCode) {
  61. sourceFilePath = `${page.localeCode}/${sourceFilePath}`
  62. }
  63. if (WIKI.config.lang.code !== page.destinationLocaleCode) {
  64. destinationFilePath = `${page.destinationLocaleCode}/${destinationFilePath}`
  65. }
  66. }
  67. await this.s3.copyObject({ CopySource: sourceFilePath, Key: destinationFilePath }).promise()
  68. await this.s3.deleteObject({ Key: sourceFilePath }).promise()
  69. }
  70. /**
  71. * ASSET UPLOAD
  72. *
  73. * @param {Object} asset Asset to upload
  74. */
  75. async assetUploaded (asset) {
  76. WIKI.logger.info(`(STORAGE/${this.storageName}) Creating new file ${asset.path}...`)
  77. await this.s3.putObject({ Key: asset.path, Body: asset.data }).promise()
  78. }
  79. /**
  80. * ASSET DELETE
  81. *
  82. * @param {Object} asset Asset to delete
  83. */
  84. async assetDeleted (asset) {
  85. WIKI.logger.info(`(STORAGE/${this.storageName}) Deleting file ${asset.path}...`)
  86. await this.s3.deleteObject({ Key: asset.path }).promise()
  87. }
  88. /**
  89. * ASSET RENAME
  90. *
  91. * @param {Object} asset Asset to rename
  92. */
  93. async assetRenamed (asset) {
  94. WIKI.logger.info(`(STORAGE/${this.storageName}) Renaming file from ${asset.path} to ${asset.destinationPath}...`)
  95. await this.s3.copyObject({ CopySource: asset.path, Key: asset.destinationPath }).promise()
  96. await this.s3.deleteObject({ Key: asset.path }).promise()
  97. }
  98. }