storage.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. const { BlobServiceClient, StorageSharedKeyCredential } = require('@azure/storage-blob')
  2. const pageHelper = require('../../../helpers/page.js')
  3. /* global WIKI */
  4. const getFilePath = (page, pathKey) => {
  5. const fileName = `${page[pathKey]}.${pageHelper.getFileExtension(page.contentType)}`
  6. const withLocaleCode = WIKI.config.lang.namespacing && WIKI.config.lang.code !== page.localeCode
  7. return withLocaleCode ? `${page.localeCode}/${fileName}` : fileName
  8. }
  9. module.exports = {
  10. async activated() {
  11. },
  12. async deactivated() {
  13. },
  14. async init() {
  15. WIKI.logger.info(`(STORAGE/AZURE) Initializing...`)
  16. const { accountName, accountKey, containerName } = this.config
  17. this.client = new BlobServiceClient(
  18. `https://${accountName}.blob.core.windows.net`,
  19. new StorageSharedKeyCredential(accountName, accountKey)
  20. )
  21. this.container = this.client.getContainerClient(containerName)
  22. try {
  23. await this.container.create()
  24. } catch (err) {
  25. if (err.statusCode !== 409) {
  26. WIKI.logger.warn(err)
  27. throw err
  28. }
  29. }
  30. WIKI.logger.info(`(STORAGE/AZURE) Initialization completed.`)
  31. },
  32. async created (page) {
  33. WIKI.logger.info(`(STORAGE/AZURE) Creating file ${page.path}...`)
  34. const filePath = getFilePath(page, 'path')
  35. const pageContent = page.injectMetadata()
  36. const blockBlobClient = this.container.getBlockBlobClient(filePath)
  37. await blockBlobClient.upload(pageContent, pageContent.length, { tier: this.config.storageTier })
  38. },
  39. async updated (page) {
  40. WIKI.logger.info(`(STORAGE/AZURE) Updating file ${page.path}...`)
  41. const filePath = getFilePath(page, 'path')
  42. const pageContent = page.injectMetadata()
  43. const blockBlobClient = this.container.getBlockBlobClient(filePath)
  44. await blockBlobClient.upload(pageContent, pageContent.length, { tier: this.config.storageTier })
  45. },
  46. async deleted (page) {
  47. WIKI.logger.info(`(STORAGE/AZURE) Deleting file ${page.path}...`)
  48. const filePath = getFilePath(page, 'path')
  49. const blockBlobClient = this.container.getBlockBlobClient(filePath)
  50. await blockBlobClient.delete({
  51. deleteSnapshots: 'include'
  52. })
  53. },
  54. async renamed(page) {
  55. WIKI.logger.info(`(STORAGE/${this.storageName}) Renaming file ${page.path} to ${page.destinationPath}...`)
  56. let sourceFilePath = getFilePath(page, 'path')
  57. let destinationFilePath = getFilePath(page, 'destinationPath')
  58. if (WIKI.config.lang.namespacing) {
  59. if (WIKI.config.lang.code !== page.localeCode) {
  60. sourceFilePath = `${page.localeCode}/${sourceFilePath}`
  61. }
  62. if (WIKI.config.lang.code !== page.destinationLocaleCode) {
  63. destinationFilePath = `${page.destinationLocaleCode}/${destinationFilePath}`
  64. }
  65. }
  66. const sourceBlockBlobClient = this.container.getBlockBlobClient(sourceFilePath)
  67. const destBlockBlobClient = this.container.getBlockBlobClient(destinationFilePath)
  68. await destBlockBlobClient.syncCopyFromURL(sourceBlockBlobClient.url)
  69. await sourceBlockBlobClient.delete({
  70. deleteSnapshots: 'include'
  71. })
  72. },
  73. /**
  74. * ASSET UPLOAD
  75. *
  76. * @param {Object} asset Asset to upload
  77. */
  78. async assetUploaded (asset) {
  79. WIKI.logger.info(`(STORAGE/AZURE) Creating new file ${asset.path}...`)
  80. const blockBlobClient = this.container.getBlockBlobClient(asset.path)
  81. await blockBlobClient.upload(asset.data, asset.data.length, { tier: this.config.storageTier })
  82. },
  83. /**
  84. * ASSET DELETE
  85. *
  86. * @param {Object} asset Asset to delete
  87. */
  88. async assetDeleted (asset) {
  89. WIKI.logger.info(`(STORAGE/AZURE) Deleting file ${asset.path}...`)
  90. const blockBlobClient = this.container.getBlockBlobClient(asset.path)
  91. await blockBlobClient.delete({
  92. deleteSnapshots: 'include'
  93. })
  94. },
  95. /**
  96. * ASSET RENAME
  97. *
  98. * @param {Object} asset Asset to rename
  99. */
  100. async assetRenamed (asset) {
  101. WIKI.logger.info(`(STORAGE/AZURE) Renaming file from ${asset.path} to ${asset.destinationPath}...`)
  102. const sourceBlockBlobClient = this.container.getBlockBlobClient(asset.path)
  103. const destBlockBlobClient = this.container.getBlockBlobClient(asset.destinationPath)
  104. await destBlockBlobClient.syncCopyFromURL(sourceBlockBlobClient.url)
  105. await sourceBlockBlobClient.delete({
  106. deleteSnapshots: 'include'
  107. })
  108. }
  109. }