|
1 | 1 | import { supabase } from './client'; |
2 | 2 |
|
3 | | -export const uploadFile = async (file: File, path: string) => { |
4 | | - // 1. Upload file to 'files' bucket (ensure this bucket exists in Supabase or change name) |
5 | | - // Using upsert: false to avoid overwriting unless intended, but path usually includes unique ID. |
6 | | - const { data, error } = await supabase.storage |
7 | | - .from('files') |
8 | | - .upload(path, file, { |
9 | | - cacheControl: '3600', |
10 | | - upsert: false, |
11 | | - contentType: file.type, |
12 | | - // Supabase technically handles disposition via download/transform usually, |
13 | | - // but ensuring type is key. |
14 | | - }); |
| 3 | +export const uploadFile = async (file: File, path: string, retries = 3) => { |
| 4 | + let lastError: any; |
| 5 | + |
| 6 | + for (let attempt = 0; attempt < retries; attempt++) { |
| 7 | + try { |
| 8 | + // 1. Upload file to 'files' bucket (ensure this bucket exists in Supabase or change name) |
| 9 | + const { data, error } = await supabase.storage |
| 10 | + .from('files') |
| 11 | + .upload(path, file, { |
| 12 | + cacheControl: '3600', |
| 13 | + upsert: false, |
| 14 | + contentType: file.type || 'application/octet-stream', |
| 15 | + }); |
15 | 16 |
|
16 | | - if (error) { |
17 | | - throw error; |
18 | | - } |
| 17 | + if (error) { |
| 18 | + console.error(`Supabase upload error (attempt ${attempt + 1}/${retries}):`, error); |
| 19 | + lastError = error; |
| 20 | + |
| 21 | + // If it's a duplicate file error, try with upsert |
| 22 | + if (error.message?.includes('already exists') || error.message?.includes('duplicate')) { |
| 23 | + const { data: upsertData, error: upsertError } = await supabase.storage |
| 24 | + .from('files') |
| 25 | + .upload(path, file, { |
| 26 | + cacheControl: '3600', |
| 27 | + upsert: true, |
| 28 | + contentType: file.type || 'application/octet-stream', |
| 29 | + }); |
| 30 | + |
| 31 | + if (upsertError) { |
| 32 | + throw new Error(`Upload failed: ${upsertError.message}`); |
| 33 | + } |
| 34 | + |
| 35 | + // Success with upsert |
| 36 | + const { data: publicUrlData } = supabase.storage |
| 37 | + .from('files') |
| 38 | + .getPublicUrl(path); |
| 39 | + return publicUrlData.publicUrl; |
| 40 | + } |
| 41 | + |
| 42 | + // Retry on network errors |
| 43 | + if (attempt < retries - 1) { |
| 44 | + await new Promise(resolve => setTimeout(resolve, 1000 * (attempt + 1))); |
| 45 | + continue; |
| 46 | + } |
| 47 | + |
| 48 | + throw new Error(`Upload failed: ${error.message || 'Unknown error'}`); |
| 49 | + } |
19 | 50 |
|
20 | | - // 2. Get Public URL |
21 | | - const { data: publicUrlData } = supabase.storage |
22 | | - .from('files') |
23 | | - .getPublicUrl(path); |
| 51 | + // 2. Get Public URL |
| 52 | + const { data: publicUrlData } = supabase.storage |
| 53 | + .from('files') |
| 54 | + .getPublicUrl(path); |
24 | 55 |
|
25 | | - return publicUrlData.publicUrl; |
| 56 | + return publicUrlData.publicUrl; |
| 57 | + } catch (err: any) { |
| 58 | + lastError = err; |
| 59 | + if (attempt < retries - 1) { |
| 60 | + console.log(`Retrying upload for ${path} (attempt ${attempt + 2}/${retries})`); |
| 61 | + await new Promise(resolve => setTimeout(resolve, 1000 * (attempt + 1))); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + throw lastError || new Error('Upload failed after retries'); |
26 | 67 | }; |
27 | 68 |
|
28 | 69 | export const deleteFile = async (path: string) => { |
|
0 commit comments