Skip to content

Commit c7ed758

Browse files
authored
Update storage.ts
1 parent bb81f53 commit c7ed758

1 file changed

Lines changed: 61 additions & 20 deletions

File tree

src/lib/supabase/storage.ts

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,69 @@
11
import { supabase } from './client';
22

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+
});
1516

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+
}
1950

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);
2455

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');
2667
};
2768

2869
export const deleteFile = async (path: string) => {

0 commit comments

Comments
 (0)