Explorar el Código

refactor: enhance file handling in VideoMediaController to improve multipart file processing and error handling

Dave hace 3 semanas
padre
commit
7607af19a9

+ 27 - 7
apps/box-mgnt-api/src/mgnt-backend/feature/video-media/video-media.controller.ts

@@ -318,19 +318,39 @@ export class VideoMediaController {
     // fastify multipart
     const reqAny = req as any;
     const bodyFile = reqAny.body?.file;
-    let mpFile: MultipartFile | undefined = Array.isArray(bodyFile)
+    const mpFile: MultipartFile | undefined = Array.isArray(bodyFile)
       ? bodyFile[0]
       : bodyFile;
-    if (!mpFile && reqAny.isMultipart?.()) {
-      mpFile = await reqAny.file();
+
+    let buf = await getBuffer(mpFile);
+
+    if (
+      !buf?.length &&
+      reqAny.isMultipart?.() &&
+      typeof reqAny.file === 'function'
+    ) {
+      const part = await reqAny.file();
+      buf = await getBuffer(part);
     }
-    if (!mpFile) {
-      throw new BadRequestException('No file uploaded');
+
+    if (
+      !buf?.length &&
+      reqAny.isMultipart?.() &&
+      typeof reqAny.parts === 'function'
+    ) {
+      for await (const part of reqAny.parts()) {
+        if (part?.type === 'file' && part?.fieldname === 'file') {
+          buf = await getBuffer(part);
+          if (buf?.length) break;
+        }
+      }
     }
 
-    const buf = await getBuffer(mpFile);
+    if (!buf) {
+      throw new BadRequestException('No file uploaded');
+    }
 
-    if (!buf?.length) {
+    if (!buf.length) {
       throw new BadRequestException('Empty file');
     }