Ver Fonte

fix upload stream is missing error

Dave há 1 mês atrás
pai
commit
6fd8b26d6c

+ 17 - 2
apps/box-mgnt-api/src/mgnt-backend/feature/uploader/uploader.service.ts

@@ -8,7 +8,7 @@ import { SysConfigReaderService } from '@box/core/sys-config/sys-config-reader.s
 import * as path from 'path';
 import { createReadStream, createWriteStream } from 'fs';
 import { mkdir, rm } from 'fs/promises';
-import { Transform, pipeline as pipelineCallback } from 'stream';
+import { Readable, Transform, pipeline as pipelineCallback } from 'stream';
 import { promisify } from 'util';
 import { PutObjectCommand, S3Client } from '@aws-sdk/client-s3';
 
@@ -204,7 +204,22 @@ export class UploaderService {
     maxBytes: number,
     rootPath: string,
   ): Promise<string> {
-    const source = file.file as NodeJS.ReadableStream;
+    let source = file.file as NodeJS.ReadableStream | undefined;
+    if (!source) {
+      const maybeBuffer =
+        (file as any).buffer ??
+        (file as any).value ??
+        (file as any).data ??
+        undefined;
+      if (Buffer.isBuffer(maybeBuffer)) {
+        source = Readable.from(maybeBuffer);
+      } else if (typeof (file as any).toBuffer === 'function') {
+        const buffer = await (file as any).toBuffer();
+        if (Buffer.isBuffer(buffer)) {
+          source = Readable.from(buffer);
+        }
+      }
+    }
     if (!source) {
       throw new BadRequestException('Upload stream is missing');
     }