gcs.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. const logger = require('@alias/logger')('growi:service:fileUploaderAws');
  2. const urljoin = require('url-join');
  3. const { Storage } = require('@google-cloud/storage');
  4. let _instance;
  5. module.exports = function(crowi) {
  6. const Uploader = require('./uploader');
  7. const { configManager } = crowi;
  8. const lib = new Uploader(crowi);
  9. function getGcsBucket() {
  10. return configManager.getConfig('crowi', 'gcs:bucket');
  11. }
  12. function getGcsInstance(isUploadable) {
  13. if (!isUploadable) {
  14. throw new Error('GCS is not configured.');
  15. }
  16. if (_instance == null) {
  17. const keyFilename = configManager.getConfig('crowi', 'gcs:apiKeyJsonPath');
  18. // see https://googleapis.dev/nodejs/storage/latest/Storage.html
  19. _instance = new Storage({ keyFilename });
  20. }
  21. return _instance;
  22. }
  23. function getFilePathOnStorage(attachment) {
  24. const namespace = configManager.getConfig('crowi', 'gcs:uploadNamespace');
  25. // const namespace = null;
  26. const dirName = (attachment.page != null)
  27. ? 'attachment'
  28. : 'user';
  29. const filePath = urljoin(namespace || '', dirName, attachment.fileName);
  30. return filePath;
  31. }
  32. lib.isValidUploadSettings = function() {
  33. return this.configManager.getConfig('crowi', 'gcs:apiKeyJsonPath') != null
  34. && this.configManager.getConfig('crowi', 'gcs:bucket') != null;
  35. };
  36. lib.deleteFile = async function(attachment) {
  37. const filePath = getFilePathOnStorage(attachment);
  38. return lib.deleteFileByFilePath(filePath);
  39. };
  40. lib.deleteFileByFilePath = async function(filePath) {
  41. const gcs = getGcsInstance(this.getIsUploadable());
  42. const myBucket = gcs.bucket(getGcsBucket());
  43. // TODO: ensure not to throw error even when the file does not exist
  44. return myBucket.file(filePath).delete();
  45. };
  46. lib.uploadFile = function(fileStream, attachment) {
  47. logger.debug(`File uploading: fileName=${attachment.fileName}`);
  48. const gcs = getGcsInstance(this.getIsUploadable());
  49. const myBucket = gcs.bucket(getGcsBucket());
  50. const filePath = getFilePathOnStorage(attachment);
  51. const options = {
  52. destination: filePath,
  53. };
  54. return myBucket.upload(fileStream.path, options);
  55. };
  56. /**
  57. * Find data substance
  58. *
  59. * @param {Attachment} attachment
  60. * @return {stream.Readable} readable stream
  61. */
  62. lib.findDeliveryFile = async function(attachment) {
  63. const gcs = getGcsInstance(this.getIsUploadable());
  64. const myBucket = gcs.bucket(getGcsBucket());
  65. const filePath = getFilePathOnStorage(attachment);
  66. let stream;
  67. try {
  68. stream = myBucket.file(filePath).createReadStream();
  69. }
  70. catch (err) {
  71. logger.error(err);
  72. throw new Error(`Coudn't get file from AWS for the Attachment (${attachment._id.toString()})`);
  73. }
  74. // return stream.Readable
  75. return stream;
  76. };
  77. /**
  78. * check the file size limit
  79. *
  80. * In detail, the followings are checked.
  81. * - per-file size limit (specified by MAX_FILE_SIZE)
  82. */
  83. lib.checkLimit = async(uploadFileSize) => {
  84. const maxFileSize = crowi.configManager.getConfig('crowi', 'app:maxFileSize');
  85. const gcsTotalLimit = crowi.configManager.getConfig('crowi', 'app:fileUploadTotalLimit');
  86. return lib.doCheckLimit(uploadFileSize, maxFileSize, gcsTotalLimit);
  87. };
  88. return lib;
  89. };