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

Commit 67c7889

Browse files
committed
update: functions to handle actions with image
1 parent e28884e commit 67c7889

File tree

3 files changed

+84
-15
lines changed

3 files changed

+84
-15
lines changed

src/services/deleteImage.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import supabase from '@/services/supabaseClient';
33
/**
44
* Deletes an image from Supabase storage based on its public URL.
55
* @param imageUrl - The public URL of the image to delete.
6-
* @param bucketName - The name of the Supabase storage bucket (default: 'gift-thumbnail').
6+
* @param bucketName - The name of the Supabase storage bucket.
77
* @throws Will throw an error if the deletion fails.
88
*/
9-
export const deleteGiftThumbnail = async (
9+
export const deleteImageFromBucket = async (
1010
imageUrl: string,
11-
bucketName: string = 'gift-thumbnail',
11+
bucketName: string,
1212
): Promise<void> => {
1313
try {
1414
const fileName = imageUrl.split('/').pop();
@@ -30,3 +30,27 @@ export const deleteGiftThumbnail = async (
3030
throw new Error('Failed to delete image');
3131
}
3232
};
33+
34+
/**
35+
* Deletes a gift thumbnail from Supabase storage.
36+
* @param imageUrl - The public URL of the gift thumbnail to delete.
37+
* @param bucketName - The name of the Supabase storage bucket (default: 'gift-thumbnail').
38+
*/
39+
export const deleteGiftThumbnail = async (
40+
imageUrl: string,
41+
bucketName: string = 'gift-thumbnail',
42+
): Promise<void> => {
43+
return deleteImageFromBucket(imageUrl, bucketName);
44+
};
45+
46+
/**
47+
* Deletes a recipient avatar from Supabase storage.
48+
* @param imageUrl - The public URL of the recipient avatar to delete.
49+
* @param bucketName - The name of the Supabase storage bucket (default: 'recipient-avatar').
50+
*/
51+
export const deleteRecipientAvatar = async (
52+
imageUrl: string,
53+
bucketName: string = 'recipient-avatar',
54+
): Promise<void> => {
55+
return deleteImageFromBucket(imageUrl, bucketName);
56+
};

src/services/updateImage.ts

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,58 @@
1-
import { deleteGiftThumbnail } from '@/services/deleteImage';
2-
import { uploadGiftThumbnail } from '@/services/uploadImage';
1+
import { deleteImageFromBucket } from '@/services/deleteImage';
2+
import { uploadFileToBucket } from '@/services/uploadImage';
33

44
/**
55
* Updates an image in Supabase storage by deleting the old image and uploading a new one.
66
* @param oldImageUrl - The public URL of the old image to delete.
77
* @param newImageUri - The URI of the new image to upload.
8-
* @param bucketName - The name of the Supabase storage bucket (default: 'gift-thumbnail').
8+
* @param bucketName - The name of the Supabase storage bucket.
99
* @returns The public URL of the new image.
1010
*/
11-
export const updateGiftThumbnail = async (
11+
export const updateImageInBucket = async (
1212
oldImageUrl: string,
1313
newImageUri: string,
14-
bucketName: string = 'gift-thumbnail',
14+
bucketName: string,
1515
): Promise<string> => {
1616
try {
1717
if (oldImageUrl) {
18-
await deleteGiftThumbnail(oldImageUrl, bucketName);
18+
await deleteImageFromBucket(oldImageUrl, bucketName);
1919
}
2020

21-
const newImageUrl = await uploadGiftThumbnail(newImageUri, bucketName);
21+
const newImageUrl = await uploadFileToBucket(newImageUri, bucketName);
2222

2323
return newImageUrl;
2424
} catch (error) {
2525
console.error('Error updating image:', error);
2626
throw new Error('Failed to update image');
2727
}
2828
};
29+
30+
/**
31+
* Updates a gift thumbnail in Supabase storage.
32+
* @param oldImageUrl - The public URL of the old gift thumbnail to delete.
33+
* @param newImageUri - The URI of the new gift thumbnail to upload.
34+
* @param bucketName - The name of the Supabase storage bucket (default: 'gift-thumbnail').
35+
* @returns The public URL of the new gift thumbnail.
36+
*/
37+
export const updateGiftThumbnail = async (
38+
oldImageUrl: string,
39+
newImageUri: string,
40+
bucketName: string = 'gift-thumbnail',
41+
): Promise<string> => {
42+
return updateImageInBucket(oldImageUrl, newImageUri, bucketName);
43+
};
44+
45+
/**
46+
* Updates a recipient avatar in Supabase storage.
47+
* @param oldImageUrl - The public URL of the old recipient avatar to delete.
48+
* @param newImageUri - The URI of the new recipient avatar to upload.
49+
* @param bucketName - The name of the Supabase storage bucket (default: 'recipient-avatar').
50+
* @returns The public URL of the new recipient avatar.
51+
*/
52+
export const updateRecipientAvatar = async (
53+
oldImageUrl: string,
54+
newImageUri: string,
55+
bucketName: string = 'recipient-avatar',
56+
): Promise<string> => {
57+
return updateImageInBucket(oldImageUrl, newImageUri, bucketName);
58+
};

src/services/uploadImage.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import * as FileSystem from 'expo-file-system';
22

33
import supabase from '@/services/supabaseClient';
44

5-
export const uploadGiftThumbnail = async (
5+
export const uploadFileToBucket = async (
66
uri: string,
7-
bucketName: string = 'gift-thumbnail',
7+
bucketName: string,
8+
contentType: string = 'image/jpeg',
89
): Promise<string> => {
910
try {
1011
const fileName = uri.split('/').pop();
@@ -24,7 +25,7 @@ export const uploadGiftThumbnail = async (
2425
const { data, error } = await supabase.storage
2526
.from(bucketName)
2627
.upload(fileName, arrayBuffer, {
27-
contentType: 'image/jpeg',
28+
contentType,
2829
cacheControl: '3600',
2930
upsert: true,
3031
});
@@ -45,7 +46,21 @@ export const uploadGiftThumbnail = async (
4546

4647
return publicUrlData.publicUrl;
4748
} catch (error) {
48-
console.error('Error uploading image:', error);
49-
throw new Error('Failed to upload image');
49+
console.error('Error uploading file:', error);
50+
throw new Error('Failed to upload file');
5051
}
5152
};
53+
54+
export const uploadGiftThumbnail = async (
55+
uri: string,
56+
bucketName: string = 'gift-thumbnail',
57+
): Promise<string> => {
58+
return uploadFileToBucket(uri, bucketName);
59+
};
60+
61+
export const uploadRecipientAvatar = async (
62+
uri: string,
63+
bucketName: string = 'recipient-avatar',
64+
): Promise<string> => {
65+
return uploadFileToBucket(uri, bucketName);
66+
};

0 commit comments

Comments
 (0)