소스 검색

refactor upload Added a body-file fallback in upload handlers

Dave 1 개월 전
부모
커밋
9e9a65a4e5

+ 6 - 1
apps/box-mgnt-api/src/mgnt-backend/feature/ads/ads.controller.ts

@@ -115,7 +115,12 @@ export class AdsController {
     @Param() { id }: MongoIdParamDto,
     @Req() req: FastifyRequest,
   ) {
-    const mpFile = await (req as any).file();
+    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();
+    }
 
     if (!mpFile) {
       throw new BadRequestException('No file uploaded');

+ 6 - 1
apps/box-mgnt-api/src/mgnt-backend/feature/ads/image/ads-image.controller.ts

@@ -15,7 +15,12 @@ export class AdsImageController {
 
   @Post(':id/cover')
   async uploadCover(@Param('id') id: string, @Req() req: FastifyRequest) {
-    const mpFile = await (req as any).file();
+    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();
+    }
 
     if (!mpFile) {
       throw new BadRequestException('No file uploaded');

+ 8 - 1
apps/box-mgnt-api/src/mgnt-backend/feature/uploader/uploader.controller.ts

@@ -55,7 +55,14 @@ export class UploaderController {
     },
   })
   async upload(@Req() req: FastifyRequest, @Body() body: UploadRequestDto) {
-    const mpFile: MultipartFile | undefined = await (req as any).file();
+    const reqAny = req as any;
+    const bodyFile = reqAny.body?.file;
+    let mpFile: MultipartFile | undefined = Array.isArray(bodyFile)
+      ? bodyFile[0]
+      : bodyFile;
+    if (!mpFile && reqAny.isMultipart?.()) {
+      mpFile = await reqAny.file();
+    }
     if (!mpFile) {
       throw new BadRequestException('File is required');
     }

+ 6 - 1
apps/box-mgnt-api/src/mgnt-backend/feature/video-media/video-media.controller.ts

@@ -242,7 +242,12 @@ export class VideoMediaController {
   })
   @Post(':id/cover')
   async updateCover(@Param('id') id: string, @Req() req: FastifyRequest) {
-    const mpFile = await (req as any).file();
+    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();
+    }
 
     if (!mpFile) {
       throw new BadRequestException('No file uploaded');