فهرست منبع

feat(s3): enhance upload size resolution and logging for content types

Dave 1 ماه پیش
والد
کامیت
658d694c02
1فایلهای تغییر یافته به همراه14 افزوده شده و 1 حذف شده
  1. 14 1
      apps/box-mgnt-api/src/mgnt-backend/feature/s3/s3.service.ts

+ 14 - 1
apps/box-mgnt-api/src/mgnt-backend/feature/s3/s3.service.ts

@@ -63,6 +63,7 @@ export class S3Service {
     const key = `${folder}/${Date.now()}_${Math.random().toString(36).slice(2, 8)}.${ext}`;
 
     const maxSize = this.resolveMaxSize(contentType);
+    this.logger.log(`maxSize for upload: ${maxSize} bytes`);
 
     const { url, fields } = await createPresignedPost(this.s3, {
       Bucket: this.bucket,
@@ -81,13 +82,25 @@ export class S3Service {
   }
 
   private resolveMaxSize(contentType: string): number {
+    this.logger.log(`Resolving max size for content type: ${contentType}`);
+    const BASE64_OVERHEAD_RATIO = 4 / 3; // ≈1.333
+    const SAFETY_MARGIN = 1.05; // extra 5% for headers / future changes
+
     const fromEnvMB = (env: string, fallbackMB: number) => {
       const val = Number(process.env[env]);
       return (isNaN(val) ? fallbackMB : val) * 1024 * 1024;
     };
 
+    // ✅ your image payload is base64 text
+    if (contentType === 'text/plain') {
+      const rawMb = Number(process.env.UPLOAD_LIMIT_IMAGE ?? 10);
+      const effectiveMb = rawMb * (4 / 3) * 1.05;
+      return Math.ceil(effectiveMb * 1024 * 1024);
+    }
     if (contentType.startsWith('image/')) {
-      return fromEnvMB('UPLOAD_LIMIT_IMAGE', 20);
+      const rawMb = Number(process.env.UPLOAD_LIMIT_IMAGE ?? 10);
+      const effectiveMb = rawMb * BASE64_OVERHEAD_RATIO * SAFETY_MARGIN;
+      return Math.ceil(effectiveMb * 1024 * 1024);
     }
     if (contentType === 'video/mp4') {
       return fromEnvMB('UPLOAD_LIMIT_VIDEO', 100);