( blob: Blob, fileName: string, workflowType: string )
| 54 | } |
| 55 | |
| 56 | export async function uploadAndSaveFile( |
| 57 | blob: Blob, |
| 58 | fileName: string, |
| 59 | workflowType: string |
| 60 | ): Promise<FileRecord | null> { |
| 61 | try { |
| 62 | const session = isSupabaseConfigured() |
| 63 | ? (await supabase.auth.getSession()).data.session |
| 64 | : null; |
| 65 | if (isSupabaseConfigured() && !session?.access_token) { |
| 66 | console.warn("[fileService] Skipping file upload because user is not authenticated"); |
| 67 | return null; |
| 68 | } |
| 69 | |
| 70 | const sanitizedFileName = sanitizeFileName(fileName, workflowType); |
| 71 | console.log(`[fileService] Original filename: ${fileName}`); |
| 72 | console.log(`[fileService] Sanitized filename: ${sanitizedFileName}`); |
| 73 | |
| 74 | const formData = new FormData(); |
| 75 | formData.append('file', blob, sanitizedFileName); |
| 76 | formData.append('workflow_type', workflowType); |
| 77 | |
| 78 | const response = await backendFetch('/api/v1/files/upload', { |
| 79 | method: 'POST', |
| 80 | body: formData, |
| 81 | }); |
| 82 | |
| 83 | if (!response.ok) { |
| 84 | const errorText = await response.text(); |
| 85 | console.error("[fileService] Failed to upload file:", errorText); |
| 86 | return null; |
| 87 | } |
| 88 | |
| 89 | const data = await response.json(); |
| 90 | |
| 91 | if (!data.success) { |
| 92 | console.error("[fileService] Upload failed:", data); |
| 93 | return null; |
| 94 | } |
| 95 | |
| 96 | return { |
| 97 | id: data.file_path, // Use file path as ID |
| 98 | file_name: data.file_name, |
| 99 | file_size: data.file_size, |
| 100 | workflow_type: data.workflow_type, |
| 101 | created_at: data.created_at, |
| 102 | download_url: data.file_path, // Backend will serve via /outputs |
| 103 | }; |
| 104 | } catch (err) { |
| 105 | console.error("[fileService] Error uploading file:", err); |
| 106 | return null; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Get all file records for the current user. |
no test coverage detected