File tree Expand file tree Collapse file tree 2 files changed +8
-9
lines changed
Expand file tree Collapse file tree 2 files changed +8
-9
lines changed Original file line number Diff line number Diff line change @@ -37,13 +37,7 @@ export async function uploadImage(
3737 }
3838 const ext = mime . split ( "/" ) [ 1 ] ;
3939
40- let filename : string ;
41- if ( fileUrl ) {
42- const { fileName } = extractFilePathAndNameFromUrl ( fileUrl ) ;
43- filename = fileName ;
44- } else {
45- filename = `${ uuidv4 ( ) } .${ ext } ` ;
46- }
40+ const filename :string = `${ uuidv4 ( ) } .${ ext } ` ;
4741
4842 const filePath = `${ folder } /${ filename } ` ;
4943
@@ -66,6 +60,10 @@ export async function uploadImage(
6660 throw new ApiError ( "Failed to get public URL" , 500 ) ;
6761 }
6862
63+ if ( fileUrl ) {
64+ await deleteImage ( supabase , fileUrl ) ;
65+ }
66+
6967 return urlData . publicUrl ;
7068}
7169
Original file line number Diff line number Diff line change @@ -20,7 +20,7 @@ describe("imageUtils", () => {
2020 storageMock = {
2121 from : jest . fn ( ) . mockReturnThis ( ) ,
2222 upload : jest . fn ( ) ,
23- getPublicUrl : jest . fn ( ) ,
23+ getPublicUrl : jest . fn ( ) ,
2424 remove : jest . fn ( ) ,
2525 } ;
2626
@@ -61,6 +61,7 @@ describe("imageUtils", () => {
6161 storageMock . getPublicUrl . mockReturnValue ( {
6262 data : { publicUrl : "https://public.url/existing.png" } ,
6363 } ) ;
64+ storageMock . remove . mockReturnValue ( { error :null } ) ;
6465
6566 const url = await uploadImage (
6667 mockSupabase as SupabaseClient ,
@@ -70,7 +71,7 @@ describe("imageUtils", () => {
7071 ) ;
7172 // Should use filename 'existing.png' instead of generating with uuid
7273 expect ( storageMock . upload ) . toHaveBeenCalledWith (
73- "folder/existing .png" ,
74+ "folder/test-uuid .png" ,
7475 dummyFile . buffer ,
7576 { contentType : dummyFile . mimetype , upsert : true } ,
7677 ) ;
You can’t perform that action at this time.
0 commit comments