All files / src/hooks/useFileUpload useFileUpload.ts

92.17% Statements 106/115
70.83% Branches 34/48
92.85% Functions 13/14
92.17% Lines 106/115

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376                                                                                                                            12x 12x             1x   1x     1x             12x             278x   278x 278x 278x   278x   8x 8x 8x   8x 7x           278x     11x 2x       2x       9x 1x 1x 1x     8x         278x     4x     278x     11x 11x 11x     11x 3x         8x 8x         7x   1x 1x                 1x     1x       1x     7x         7x 7x 7x 7x 7x   7x 1x 1x 1x       7x 5x 4x 4x       1x 5x   5x         5x     7x 1x     7x 1x     7x       7x 7x 3x   7x           3x 3x 3x   3x 1x                   1x 1x 1x 1x         1x     2x 1x               1x             1x     1x 1x               1x         1x                                     4x 4x   4x 4x           1x 1x             1x     1x         1x     3x 3x     3x 1x 1x           3x         278x 2x 2x 2x     278x                
/**
 * useFileUpload Hook
 * Story 1.16.3: Generic File Upload Service
 * ADR-002: Generic File Upload Service Architecture
 *
 * Reusable hook for file uploads using the generic logo upload service
 * Supports three-phase upload flow:
 * 1. Generate presigned URL from backend
 * 2. Upload file directly to S3
 * 3. Confirm upload with backend
 *
 * Can be used for any file upload: company logos, user profile pictures, event banners, etc.
 */
 
import { useState, useCallback } from 'react';
import apiClient from '@/services/api/apiClient';
import { formatErrorForDisplay } from '@/utils/errorHandling/errorMessages';
 
interface UseFileUploadOptions {
  maxFileSize?: number; // in bytes, default 5MB
  allowedTypes?: string[]; // MIME types, default: PNG, JPEG, SVG
  onUploadSuccess?: (data: UploadSuccessData) => void;
  onUploadError?: (error: UploadError) => void;
  uploadEndpoint?: string; // endpoint for presigned URL, default: '/logos/presigned-url'
}
 
interface UploadSuccessData {
  uploadId: string;
  tempFileUrl?: string;
}
 
/**
 * Granular error types so the UI and support team can tell which step of the
 * three-phase upload broke. The legacy 'UPLOAD_FAILED' and 'NETWORK_ERROR'
 * values are kept for backwards compatibility with existing callers.
 */
type UploadErrorType =
  | 'INVALID_FILE_TYPE'
  | 'FILE_TOO_LARGE'
  | 'BACKEND_PRESIGN_FAILED' // Phase 1: backend rejected the presigned-URL request
  | 'S3_NETWORK_ERROR' // Phase 2: PUT never reached S3 (CORS / network / blocker)
  | 'S3_TIMEOUT' // Phase 2: PUT timed out
  | 'S3_REJECTED' // Phase 2: S3 returned non-2xx
  | 'S3_ABORTED' // Phase 2: user or browser aborted
  | 'BACKEND_CONFIRM_FAILED' // Phase 3: backend confirm failed
  | 'UPLOAD_FAILED' // generic fallback (kept for callers)
  | 'NETWORK_ERROR'; // legacy alias (kept for callers)
 
interface UploadError {
  type: UploadErrorType;
  message: string;
}
 
interface PresignedUrlResponse {
  uploadUrl: string;
  fileId: string;
  s3Key: string; // S3 key for constructing CloudFront URL
  fileExtension: string;
  expiresInMinutes: number;
  requiredHeaders: Record<string, string>;
}
 
const DEFAULT_MAX_FILE_SIZE = 5 * 1024 * 1024; // 5MB
const DEFAULT_ALLOWED_TYPES = ['image/png', 'image/jpeg', 'image/svg+xml'];
 
/**
 * Get CloudFront CDN URL based on environment
 * Returns the CloudFront domain for accessing uploaded files
 */
function getCloudFrontUrl(): string {
  const hostname = window.location.hostname;
 
  Eif (hostname === 'localhost' || hostname === '127.0.0.1') {
    // Local development - files served from MinIO
    // Include bucket name in URL for MinIO path-style access
    return 'http://localhost:8450/batbern-development-company-logos';
  }
 
  // Production (batbern.ch or www.batbern.ch)
  return 'https://cdn.batbern.ch';
}
 
export const useFileUpload = (options: UseFileUploadOptions = {}) => {
  const {
    maxFileSize = DEFAULT_MAX_FILE_SIZE,
    allowedTypes = DEFAULT_ALLOWED_TYPES,
    onUploadSuccess,
    onUploadError,
    uploadEndpoint = '/logos/presigned-url', // Default to logos for backward compatibility
  } = options;
 
  const [isUploading, setIsUploading] = useState(false);
  const [uploadProgress, setUploadProgress] = useState(0);
  const [error, setError] = useState<string | null>(null);
 
  const handleError = useCallback(
    (errorType: UploadError['type'], errorMessage: string) => {
      setError(errorMessage);
      setIsUploading(false);
      setUploadProgress(0);
 
      if (onUploadError) {
        onUploadError({ type: errorType, message: errorMessage });
      }
    },
    [onUploadError]
  );
 
  const validateFile = useCallback(
    (file: File): boolean => {
      // Validate file type
      if (!allowedTypes.includes(file.type)) {
        handleError(
          'INVALID_FILE_TYPE',
          `Invalid file type. Allowed types: ${allowedTypes.join(', ')}`
        );
        return false;
      }
 
      // Validate file size
      if (file.size > maxFileSize) {
        const maxSizeMB = (maxFileSize / (1024 * 1024)).toFixed(2);
        handleError('FILE_TOO_LARGE', `File size must be less than ${maxSizeMB}MB`);
        return false;
      }
 
      return true;
    },
    [allowedTypes, maxFileSize, handleError]
  );
 
  const calculateChecksum = async (file: File): Promise<string> => {
    // Simple checksum calculation using file metadata
    // In production, you might want to calculate actual SHA-256
    return `${file.name}-${file.size}-${file.lastModified}`;
  };
 
  const uploadFile = useCallback(
    async (file: File): Promise<string | null> => {
      // Reset state
      setError(null);
      setUploadProgress(0);
      setIsUploading(true);
 
      // Validate file
      if (!validateFile(file)) {
        return null;
      }
 
      // ── Phase 1: Request presigned URL from backend ─────────────────────────
      let presigned: PresignedUrlResponse;
      try {
        const presignedResponse = await apiClient.post<PresignedUrlResponse>(uploadEndpoint, {
          fileName: file.name,
          fileSize: file.size,
          mimeType: file.type,
        });
        presigned = presignedResponse.data;
      } catch (err) {
        const formatted = formatErrorForDisplay(err);
        console.error('[FileUpload] Phase 1 (presigned URL request) failed', {
          fileName: file.name,
          fileSize: file.size,
          mimeType: file.type,
          endpoint: uploadEndpoint,
          statusCode: formatted.statusCode,
          serverMessage: formatted.message,
          correlationId: formatted.correlationId,
        });
        const correlationSuffix = formatted.correlationId
          ? ` (id: ${formatted.correlationId})`
          : '';
        handleError(
          'BACKEND_PRESIGN_FAILED',
          `Could not request upload URL: ${formatted.message}${correlationSuffix}`
        );
        return null;
      }
 
      const { uploadUrl, fileId, s3Key, fileExtension, requiredHeaders } = presigned;
 
      // ── Phase 2: Upload file directly to S3 using the presigned URL ─────────
      // The PUT bypasses our backend entirely. Errors here are S3-side or
      // browser-side (CORS, ad-blocker, firewall, VPN, DNS, offline).
      const S3_UPLOAD_TIMEOUT_MS = 120_000; // 2 min — generous for slow connections
      try {
        await new Promise<void>((resolve, reject) => {
          const xhr = new XMLHttpRequest();
          xhr.timeout = S3_UPLOAD_TIMEOUT_MS;
 
          xhr.upload.addEventListener('progress', (event) => {
            Eif (event.lengthComputable) {
              const percentComplete = Math.round((event.loaded / event.total) * 100);
              setUploadProgress(percentComplete);
            }
          });
 
          xhr.addEventListener('load', () => {
            if (xhr.status >= 200 && xhr.status < 300) {
              resolve();
              return;
            }
            // S3 returned a non-2xx — pull the XML error body so we know WHY
            // (AccessDenied, EntityTooLarge, signature mismatch, expired, …).
            const bodyPreview = (xhr.responseText ?? '').slice(0, 500);
            const error = new Error('S3_REJECTED');
            // Stash diagnostics on the error for the outer catch.
            Object.assign(error, {
              s3Status: xhr.status,
              s3StatusText: xhr.statusText,
              s3Body: bodyPreview,
            });
            reject(error);
          });
 
          xhr.addEventListener('error', () => {
            reject(new Error('S3_NETWORK_ERROR'));
          });
 
          xhr.addEventListener('timeout', () => {
            reject(new Error('S3_TIMEOUT'));
          });
 
          xhr.addEventListener('abort', () => {
            reject(new Error('S3_ABORTED'));
          });
 
          xhr.open('PUT', uploadUrl);
          Object.entries(requiredHeaders).forEach(([key, value]) => {
            xhr.setRequestHeader(key, value);
          });
          xhr.send(file);
        });
      } catch (err) {
        // Strip the query string from the upload URL before logging — it
        // contains the signed credential and we don't want it in browser
        // consoles or shipped log aggregators.
        const safeUploadUrl = uploadUrl.split('?')[0];
        const errCode = err instanceof Error ? err.message : 'S3_UPLOAD_UNKNOWN';
        const diag = err as Error & { s3Status?: number; s3StatusText?: string; s3Body?: string };
 
        if (errCode === 'S3_REJECTED') {
          console.error('[FileUpload] Phase 2 (S3 PUT) rejected by S3', {
            fileName: file.name,
            fileSize: file.size,
            mimeType: file.type,
            uploadUrl: safeUploadUrl,
            uploadId: fileId,
            s3Status: diag.s3Status,
            s3StatusText: diag.s3StatusText,
            s3Body: diag.s3Body,
          });
          const status = diag.s3Status ?? 0;
          const statusText = diag.s3StatusText ? ` ${diag.s3StatusText}` : '';
          const bodyHint = diag.s3Body ? ` — ${diag.s3Body}` : '';
          handleError(
            'S3_REJECTED',
            `S3 rejected the upload (HTTP ${status}${statusText})${bodyHint}. ` +
              `Upload ID: ${fileId}.`
          );
          return null;
        }
 
        if (errCode === 'S3_NETWORK_ERROR') {
          console.error('[FileUpload] Phase 2 (S3 PUT) network error — no response received', {
            fileName: file.name,
            fileSize: file.size,
            mimeType: file.type,
            uploadUrl: safeUploadUrl,
            uploadId: fileId,
            online: typeof navigator !== 'undefined' ? navigator.onLine : 'unknown',
          });
          handleError(
            'S3_NETWORK_ERROR',
            'The upload could not reach S3. This is usually a network problem ' +
              '(offline / VPN / DNS), a CORS rejection, or a browser extension / ' +
              'firewall blocking the request. Open DevTools → Network for the failing ' +
              `PUT to see the exact cause, then retry. Upload ID: ${fileId}.`
          );
          return null;
        }
 
        Eif (errCode === 'S3_TIMEOUT') {
          console.error('[FileUpload] Phase 2 (S3 PUT) timed out', {
            fileName: file.name,
            fileSize: file.size,
            mimeType: file.type,
            uploadUrl: safeUploadUrl,
            uploadId: fileId,
            timeoutMs: S3_UPLOAD_TIMEOUT_MS,
          });
          handleError(
            'S3_TIMEOUT',
            `Upload timed out after ${S3_UPLOAD_TIMEOUT_MS / 1000}s. ` +
              `Check your connection and retry. Upload ID: ${fileId}.`
          );
          return null;
        }
 
        if (errCode === 'S3_ABORTED') {
          console.warn('[FileUpload] Phase 2 (S3 PUT) aborted', {
            fileName: file.name,
            uploadId: fileId,
          });
          handleError('S3_ABORTED', `Upload aborted. Upload ID: ${fileId}.`);
          return null;
        }
 
        // Unknown — preserve original message for forensics
        console.error('[FileUpload] Phase 2 (S3 PUT) failed with unknown error', err);
        handleError('UPLOAD_FAILED', `Upload failed: ${errCode}. Upload ID: ${fileId}.`);
        return null;
      }
 
      // ── Phase 3: Confirm upload with backend ────────────────────────────────
      try {
        const checksum = await calculateChecksum(file);
        // Extract base path from uploadEndpoint (/logos/presigned-url -> /logos)
        const basePath = uploadEndpoint.split('/').slice(0, -1).join('/');
        await apiClient.post(`${basePath}/${fileId}/confirm`, {
          fileId,
          fileExtension,
          checksum,
        });
      } catch (err) {
        const formatted = formatErrorForDisplay(err);
        console.error('[FileUpload] Phase 3 (confirm) failed — file IS in S3 but unconfirmed', {
          fileName: file.name,
          uploadId: fileId,
          statusCode: formatted.statusCode,
          serverMessage: formatted.message,
          correlationId: formatted.correlationId,
        });
        const correlationSuffix = formatted.correlationId
          ? ` (id: ${formatted.correlationId})`
          : '';
        handleError(
          'BACKEND_CONFIRM_FAILED',
          `Upload reached S3 but the server could not confirm it: ${formatted.message}${correlationSuffix}. ` +
            `Upload ID: ${fileId}.`
        );
        return null;
      }
 
      setUploadProgress(100);
      setIsUploading(false);
 
      // Notify success with CloudFront URL for display
      if (onUploadSuccess) {
        const cloudFrontUrl = getCloudFrontUrl();
        onUploadSuccess({
          uploadId: fileId,
          tempFileUrl: `${cloudFrontUrl}/${s3Key}`, // Use CloudFront URL instead of S3 direct URL
        });
      }
 
      return fileId;
    },
    [validateFile, handleError, onUploadSuccess, uploadEndpoint]
  );
 
  const reset = useCallback(() => {
    setIsUploading(false);
    setUploadProgress(0);
    setError(null);
  }, []);
 
  return {
    uploadFile,
    isUploading,
    uploadProgress,
    error,
    reset,
  };
};