All files / services files.service.ts

100% Statements 9/9
75% Branches 3/4
100% Functions 2/2
100% Lines 9/9

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                                              6x 6x 6x   6x           5x           6x   4x   4x 4x    
/**
 * Files API service
 * Handles file upload and download operations
 */
 
import { apiClient } from '@/lib/api';
 
export interface FileMetadata {
  filename: string;
  size: number;
  mime_type: string;
  path: string;
}
 
export interface FileUploadResponse {
  success: boolean;
  file: FileMetadata;
  message: string;
}
 
/**
 * Upload a file to storage
 */
export const uploadFile = async (file: File): Promise<FileMetadata> => {
  const formData = new FormData();
  formData.append('file', file);
 
  const response = await apiClient.post<FileUploadResponse>('/files/upload', formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  });
 
  return response.data.file;
};
 
/**
 * Get file download URL
 */
export const getFileDownloadUrl = (filePath: string): string => {
  // Use the API client's base URL
  const baseUrl = import.meta.env.VITE_API_BASE_URL || '/api/v1';
  // Ensure we have the full URL for file downloads
  const apiUrl = baseUrl.startsWith('http') ? baseUrl : `${window.location.origin}${baseUrl}`;
  return `${apiUrl}/files/${filePath}`;
};