Procházet zdrojové kódy

modify gcs uploadAttachment to use stream

Futa Arai před 1 rokem
rodič
revize
a6a96078e3

+ 3 - 3
apps/app/src/server/service/file-uploader/aws/index.ts

@@ -1,4 +1,4 @@
-import type { ReadStream } from 'fs';
+import type { Readable } from 'stream';
 
 
 import type { GetObjectCommandInput, HeadObjectCommandInput } from '@aws-sdk/client-s3';
 import type { GetObjectCommandInput, HeadObjectCommandInput } from '@aws-sdk/client-s3';
 import {
 import {
@@ -157,7 +157,7 @@ class AwsFileUploader extends AbstractFileUploader {
   /**
   /**
    * @inheritdoc
    * @inheritdoc
    */
    */
-  override async uploadAttachment(readStream: ReadStream, attachment: IAttachmentDocument): Promise<void> {
+  override async uploadAttachment(readable: Readable, attachment: IAttachmentDocument): Promise<void> {
     if (!this.getIsUploadable()) {
     if (!this.getIsUploadable()) {
       throw new Error('AWS is not configured.');
       throw new Error('AWS is not configured.');
     }
     }
@@ -172,7 +172,7 @@ class AwsFileUploader extends AbstractFileUploader {
     await s3.send(new PutObjectCommand({
     await s3.send(new PutObjectCommand({
       Bucket: getS3Bucket(),
       Bucket: getS3Bucket(),
       Key: filePath,
       Key: filePath,
-      Body: readStream,
+      Body: readable,
       ACL: getS3PutObjectCannedAcl(),
       ACL: getS3PutObjectCannedAcl(),
       // put type and the file name for reference information when uploading
       // put type and the file name for reference information when uploading
       ContentType: contentHeaders.contentType?.value.toString(),
       ContentType: contentHeaders.contentType?.value.toString(),

+ 7 - 5
apps/app/src/server/service/file-uploader/gcs/index.ts

@@ -1,4 +1,5 @@
-import type { ReadStream } from 'fs';
+import type { Readable } from 'stream';
+import { pipeline } from 'stream/promises';
 
 
 import { Storage } from '@google-cloud/storage';
 import { Storage } from '@google-cloud/storage';
 import axios from 'axios';
 import axios from 'axios';
@@ -109,7 +110,7 @@ class GcsFileUploader extends AbstractFileUploader {
   /**
   /**
    * @inheritdoc
    * @inheritdoc
    */
    */
-  override async uploadAttachment(readStream: ReadStream, attachment: IAttachmentDocument): Promise<void> {
+  override async uploadAttachment(readable: Readable, attachment: IAttachmentDocument): Promise<void> {
     if (!this.getIsUploadable()) {
     if (!this.getIsUploadable()) {
       throw new Error('GCS is not configured.');
       throw new Error('GCS is not configured.');
     }
     }
@@ -121,11 +122,12 @@ class GcsFileUploader extends AbstractFileUploader {
     const filePath = getFilePathOnStorage(attachment);
     const filePath = getFilePathOnStorage(attachment);
     const contentHeaders = new ContentHeaders(attachment);
     const contentHeaders = new ContentHeaders(attachment);
 
 
-    await myBucket.upload(readStream.path.toString(), {
-      destination: filePath,
+    const file = myBucket.file(filePath);
+
+    await pipeline(readable, file.createWriteStream({
       // put type and the file name for reference information when uploading
       // put type and the file name for reference information when uploading
       contentType: contentHeaders.contentType?.value.toString(),
       contentType: contentHeaders.contentType?.value.toString(),
-    });
+    }));
   }
   }
 
 
   /**
   /**

+ 0 - 1
apps/app/src/server/service/file-uploader/gridfs.ts

@@ -1,4 +1,3 @@
-import type { ReadStream } from 'fs';
 import { Readable } from 'stream';
 import { Readable } from 'stream';
 import util from 'util';
 import util from 'util';
 
 

+ 0 - 1
apps/app/src/server/service/file-uploader/local.ts

@@ -1,4 +1,3 @@
-import type { ReadStream } from 'fs';
 import type { Writable } from 'stream';
 import type { Writable } from 'stream';
 import { Readable } from 'stream';
 import { Readable } from 'stream';
 import { pipeline } from 'stream/promises';
 import { pipeline } from 'stream/promises';