common.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. }