Skip to content

Commit 1e5ebcb

Browse files
authored
Merge pull request #59 from call-0f-code/member-Profile-update-fix
refactor image update logic in uploadImg function
2 parents 22e59aa + e2ec074 commit 1e5ebcb

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

src/utils/imageUtils.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff 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

tests/imageUtils.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff 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
);

0 commit comments

Comments
 (0)