Jelajahi Sumber

refactor: improve file handling in updateCover method to support multiple file uploads

Dave 3 minggu lalu
induk
melakukan
b35b486c54

+ 3 - 11
apps/box-mgnt-api/src/mgnt-backend/feature/video-media/video-media.controller.ts

@@ -226,7 +226,7 @@ export class VideoMediaController {
   @Post(':id/cover')
   async updateCover(@Param('id') id: string, @Req() req: FastifyRequest) {
     const reqAny = req as any;
-    const bodyFile = reqAny.body?.file;
+    const bodyFile = reqAny.body?.file ?? reqAny.body?.files;
     let mpFile = Array.isArray(bodyFile) ? bodyFile[0] : bodyFile;
     if (!mpFile && reqAny.isMultipart?.()) {
       mpFile = await reqAny.file();
@@ -324,20 +324,12 @@ export class VideoMediaController {
 
     let buf = await getBuffer(mpFile);
 
-    if (
-      !buf?.length &&
-      reqAny.isMultipart?.() &&
-      typeof reqAny.file === 'function'
-    ) {
+    if (!buf?.length && typeof reqAny.file === 'function') {
       const part = await reqAny.file();
       buf = await getBuffer(part);
     }
 
-    if (
-      !buf?.length &&
-      reqAny.isMultipart?.() &&
-      typeof reqAny.parts === 'function'
-    ) {
+    if (!buf?.length && typeof reqAny.parts === 'function') {
       for await (const part of reqAny.parts()) {
         if (part?.type === 'file' && part?.fieldname === 'file') {
           buf = await getBuffer(part);