Sfoglia il codice sorgente

refactor: enhance importExcelTags method to support multipart file handling and improve error checks

Dave 3 settimane fa
parent
commit
20aaa3e3c8

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

@@ -286,11 +286,25 @@ export class VideoMediaController {
   @Post('import/excel-tags')
   async importExcelTags(@Req() req: FastifyRequest) {
     // fastify multipart
-    const part = await (req as any).file();
-    if (!part) throw new BadRequestException('No file uploaded');
+    const reqAny = req as any;
+    const bodyFile = reqAny.body?.file;
+    let mpFile = Array.isArray(bodyFile) ? bodyFile[0] : bodyFile;
+    if (!mpFile && reqAny.isMultipart?.()) {
+      mpFile = await reqAny.file();
+    }
 
-    const buf = await part.toBuffer();
-    if (!buf?.length) throw new BadRequestException('Empty file');
+    if (!mpFile) {
+      throw new BadRequestException('No file uploaded');
+    }
+
+    const buf =
+      typeof mpFile.toBuffer === 'function'
+        ? await mpFile.toBuffer()
+        : mpFile.buffer;
+
+    if (!buf?.length) {
+      throw new BadRequestException('Empty file');
+    }
 
     return this.videoMediaService.importExcelTags(buf);
   }