Просмотр исходного кода

WIP
refs GW-232: GCS にアップロードできる
- FB対応 シングルトン対応

Otani Haruhiko 6 лет назад
Родитель
Сommit
b323f73132
2 измененных файлов с 9 добавлено и 12 удалено
  1. 8 11
      src/server/service/file-uploader/gcp.js
  2. 1 1
      src/server/service/file-uploader/uploader.js

+ 8 - 11
src/server/service/file-uploader/gcp.js

@@ -13,20 +13,17 @@ module.exports = function(crowi) {
     return configManager.getConfig('crowi', 'gcs:bucket');
     return configManager.getConfig('crowi', 'gcs:bucket');
   }
   }
 
 
-  function GCSFactory(isUploadable) {
-
+  function getGcsInstance(isUploadable) {
     if (!isUploadable) {
     if (!isUploadable) {
       throw new Error('GCP is not configured.');
       throw new Error('GCP is not configured.');
     }
     }
-
-    return new Storage();
+    if (this.gcsInstance == null) {
+      this.gcsInstance = new Storage();
+    }
+    return this.gcsInstance;
   }
   }
 
 
   function getFilePathOnStorage(attachment) {
   function getFilePathOnStorage(attachment) {
-    if (attachment.filePath != null) { // backward compatibility for v3.3.x or below
-      return attachment.filePath;
-    }
-
     const dirName = (attachment.page != null)
     const dirName = (attachment.page != null)
       ? 'attachment'
       ? 'attachment'
       : 'user';
       : 'user';
@@ -41,7 +38,7 @@ module.exports = function(crowi) {
   };
   };
 
 
   lib.deleteFileByFilePath = async function(filePath) {
   lib.deleteFileByFilePath = async function(filePath) {
-    const gcs = GCSFactory(this.getIsUploadable());
+    const gcs = getGcsInstance(this.getIsUploadable());
     const myBucket = gcs.bucket(getGcsBucket());
     const myBucket = gcs.bucket(getGcsBucket());
 
 
     // TODO: ensure not to throw error even when the file does not exist
     // TODO: ensure not to throw error even when the file does not exist
@@ -52,7 +49,7 @@ module.exports = function(crowi) {
   lib.uploadFile = function(fileStream, attachment) {
   lib.uploadFile = function(fileStream, attachment) {
     logger.debug(`File uploading: fileName=${attachment.fileName}`);
     logger.debug(`File uploading: fileName=${attachment.fileName}`);
 
 
-    const gcs = GCSFactory(this.getIsUploadable());
+    const gcs = getGcsInstance(this.getIsUploadable());
     const myBucket = gcs.bucket(getGcsBucket());
     const myBucket = gcs.bucket(getGcsBucket());
     const filePath = getFilePathOnStorage(attachment);
     const filePath = getFilePathOnStorage(attachment);
     const options = {
     const options = {
@@ -69,7 +66,7 @@ module.exports = function(crowi) {
    * @return {stream.Readable} readable stream
    * @return {stream.Readable} readable stream
    */
    */
   lib.findDeliveryFile = async function(attachment) {
   lib.findDeliveryFile = async function(attachment) {
-    const gcs = GCSFactory(this.getIsUploadable());
+    const gcs = getGcsInstance(this.getIsUploadable());
     const myBucket = gcs.bucket(getGcsBucket());
     const myBucket = gcs.bucket(getGcsBucket());
     const filePath = getFilePathOnStorage(attachment);
     const filePath = getFilePathOnStorage(attachment);
 
 

+ 1 - 1
src/server/service/file-uploader/uploader.js

@@ -20,7 +20,7 @@ class Uploader {
         || !this.configManager.getConfig('crowi', 'aws:bucket'))) {
         || !this.configManager.getConfig('crowi', 'aws:bucket'))) {
       return false;
       return false;
     }
     }
-    // method が gcs かつ、gcs:apiKeyJsonPath と gcs:bucket のどちらかが設定されていなければ false
+    // When method is gcs and gcs:api Key JsonPath or gcs:bucket is set is false
     if (method === 'gsc' && (
     if (method === 'gsc' && (
       !this.configManager.getConfig('crowi', 'gcs:apiKeyJsonPath')
       !this.configManager.getConfig('crowi', 'gcs:apiKeyJsonPath')
         || !this.configManager.getConfig('crowi', 'gcs:bucket'))
         || !this.configManager.getConfig('crowi', 'gcs:bucket'))