Skip to content
This repository was archived by the owner on May 5, 2025. It is now read-only.

Commit ce8f38c

Browse files
committed
add: services for handle action with image on supabase
1 parent ef98deb commit ce8f38c

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

src/services/deleteImage.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import supabase from '@/services/supabaseClient';
2+
3+
/**
4+
* Deletes an image from Supabase storage based on its public URL.
5+
* @param imageUrl - The public URL of the image to delete.
6+
* @param bucketName - The name of the Supabase storage bucket (default: 'gift-thumbnail').
7+
* @throws Will throw an error if the deletion fails.
8+
*/
9+
export const deleteGiftThumbnail = async (
10+
imageUrl: string,
11+
bucketName: string = 'gift-thumbnail',
12+
): Promise<void> => {
13+
try {
14+
const fileName = imageUrl.split('/').pop();
15+
if (!fileName) {
16+
console.error('Invalid image URL');
17+
throw new Error('Invalid image URL');
18+
}
19+
20+
const { error } = await supabase.storage
21+
.from(bucketName)
22+
.remove([fileName]);
23+
24+
if (error) {
25+
console.error('Error deleting file from Supabase:', error.message);
26+
throw error;
27+
}
28+
} catch (error) {
29+
console.error('Error deleting image:', error);
30+
throw new Error('Failed to delete image');
31+
}
32+
};

src/services/updateImage.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { deleteGiftThumbnail } from '@/services/deleteImage';
2+
import { uploadGiftThumbnail } from '@/services/uploadImage';
3+
4+
/**
5+
* Updates an image in Supabase storage by deleting the old image and uploading a new one.
6+
* @param oldImageUrl - The public URL of the old image to delete.
7+
* @param newImageUri - The URI of the new image to upload.
8+
* @param bucketName - The name of the Supabase storage bucket (default: 'gift-thumbnail').
9+
* @returns The public URL of the new image.
10+
*/
11+
export const updateGiftThumbnail = async (
12+
oldImageUrl: string,
13+
newImageUri: string,
14+
bucketName: string = 'gift-thumbnail',
15+
): Promise<string> => {
16+
try {
17+
if (oldImageUrl) {
18+
await deleteGiftThumbnail(oldImageUrl, bucketName);
19+
}
20+
21+
const newImageUrl = await uploadGiftThumbnail(newImageUri, bucketName);
22+
23+
return newImageUrl;
24+
} catch (error) {
25+
console.error('Error updating image:', error);
26+
throw new Error('Failed to update image');
27+
}
28+
};

src/services/uploadImage.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import * as FileSystem from 'expo-file-system';
2+
3+
import supabase from '@/services/supabaseClient';
4+
5+
export const uploadGiftThumbnail = async (
6+
uri: string,
7+
bucketName: string = 'gift-thumbnail',
8+
): Promise<string> => {
9+
try {
10+
const fileName = uri.split('/').pop();
11+
if (!fileName) {
12+
console.error('Invalid file name');
13+
throw new Error('Invalid file name');
14+
}
15+
16+
const fileInfo = await FileSystem.readAsStringAsync(uri, {
17+
encoding: FileSystem.EncodingType.Base64,
18+
});
19+
20+
const arrayBuffer = Uint8Array.from(atob(fileInfo), (c) =>
21+
c.charCodeAt(0),
22+
).buffer;
23+
24+
const { data, error } = await supabase.storage
25+
.from(bucketName)
26+
.upload(fileName, arrayBuffer, {
27+
contentType: 'image/jpeg',
28+
cacheControl: '3600',
29+
upsert: true,
30+
});
31+
32+
if (error) {
33+
console.error('Error uploading file to Supabase:', error.message);
34+
throw error;
35+
}
36+
37+
const { data: publicUrlData } = supabase.storage
38+
.from(bucketName)
39+
.getPublicUrl(fileName);
40+
41+
if (!publicUrlData?.publicUrl) {
42+
console.error('Failed to retrieve public URL');
43+
throw new Error('Failed to retrieve public URL');
44+
}
45+
46+
return publicUrlData.publicUrl;
47+
} catch (error) {
48+
console.error('Error uploading image:', error);
49+
throw new Error('Failed to upload image');
50+
}
51+
};

0 commit comments

Comments
 (0)