Răsfoiți Sursa

refactor: add getBuffer utility to enhance file handling in importExcelTags method

Dave 3 săptămâni în urmă
părinte
comite
87cfaff9cc

+ 30 - 4
apps/box-mgnt-api/src/mgnt-backend/feature/video-media/video-media.controller.ts

@@ -285,6 +285,35 @@ export class VideoMediaController {
   })
   @Post('import/excel-tags')
   async importExcelTags(@Req() req: FastifyRequest) {
+    const getBuffer = async (file: any): Promise<Buffer | undefined> => {
+      if (!file) return undefined;
+      if (Buffer.isBuffer(file)) return file;
+      if (file instanceof Uint8Array) return Buffer.from(file);
+
+      const candidate =
+        file.buffer ?? file.data ?? file.value ?? file._buf ?? undefined;
+      if (Buffer.isBuffer(candidate)) return candidate;
+      if (candidate instanceof Uint8Array) return Buffer.from(candidate);
+      if (typeof candidate === 'string') return Buffer.from(candidate);
+
+      if (typeof file.toBuffer === 'function') {
+        const buf = await file.toBuffer();
+        if (Buffer.isBuffer(buf)) return buf;
+      }
+
+      if (file.file) {
+        const chunks: Buffer[] = [];
+        for await (const chunk of file.file) {
+          chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
+        }
+        if (chunks.length > 0) {
+          return Buffer.concat(chunks);
+        }
+      }
+
+      return undefined;
+    };
+
     // fastify multipart
     const reqAny = req as any;
     const bodyFile = reqAny.body?.file;
@@ -297,10 +326,7 @@ export class VideoMediaController {
       throw new BadRequestException('No file uploaded');
     }
 
-    const buf =
-      typeof mpFile.toBuffer === 'function'
-        ? await mpFile.toBuffer()
-        : mpFile.buffer;
+    const buf = await getBuffer(mpFile);
 
     if (!buf?.length) {
       throw new BadRequestException('Empty file');