Explorar el Código

refactor: update importExcelTags method to handle Buffer or Uint8Array and improve error handling

Dave hace 3 semanas
padre
commit
18a12db5a1

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

@@ -11,8 +11,6 @@ import {
   Req,
   Res,
   BadRequestException,
-  UploadedFile,
-  UseInterceptors,
 } from '@nestjs/common';
 import type { FastifyReply, FastifyRequest } from 'fastify';
 import {
@@ -26,8 +24,6 @@ import {
   ApiNotFoundResponse,
   ApiBadRequestResponse,
 } from '@nestjs/swagger';
-import { FileInterceptor } from '@nestjs/platform-express';
-import * as multer from 'multer';
 import { VideoMediaService } from './video-media.service';
 import {
   VideoMediaListQueryDto,

+ 6 - 5
apps/box-mgnt-api/src/mgnt-backend/feature/video-media/video-media.service.ts

@@ -301,12 +301,13 @@ export class VideoMediaService {
     return Buffer.isBuffer(buffer) ? buffer : Buffer.from(buffer);
   }
 
-  async importExcelTags(file: Buffer) {
-    if (!file?.buffer) {
+  async importExcelTags(file: Buffer | Uint8Array) {
+    const buffer = Buffer.isBuffer(file) ? file : Buffer.from(file ?? []);
+    if (!buffer.length) {
       throw new BadRequestException('No file uploaded');
     }
 
-    const workbook = XLSX.read(file.buffer, { type: 'buffer' });
+    const workbook = XLSX.read(buffer, { type: 'buffer' });
     const sheetName = workbook.SheetNames[0];
     const sheet = sheetName ? workbook.Sheets[sheetName] : undefined;
 
@@ -352,6 +353,8 @@ export class VideoMediaService {
         continue;
       }
 
+      candidateRows += 1;
+
       const id = this.normalizeMongoIdToString(idRaw).trim();
       if (!this.isObjectIdString(id)) {
         skippedInvalidId += 1;
@@ -359,8 +362,6 @@ export class VideoMediaService {
         continue;
       }
 
-      candidateRows += 1;
-
       for (const tag of tags) {
         uniqueTagNames.add(tag);
       }