Skip to content
This repository was archived by the owner on May 5, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add: services for handle action with image on supabase
  • Loading branch information
htnghia1423 committed Apr 29, 2025
commit ce8f38c7d04caae432840d47088085e76dd35cef
32 changes: 32 additions & 0 deletions src/services/deleteImage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import supabase from '@/services/supabaseClient';

/**
* Deletes an image from Supabase storage based on its public URL.
* @param imageUrl - The public URL of the image to delete.
* @param bucketName - The name of the Supabase storage bucket (default: 'gift-thumbnail').
* @throws Will throw an error if the deletion fails.
*/
export const deleteGiftThumbnail = async (
imageUrl: string,
bucketName: string = 'gift-thumbnail',
): Promise<void> => {
try {
const fileName = imageUrl.split('/').pop();
if (!fileName) {
console.error('Invalid image URL');
throw new Error('Invalid image URL');
}

const { error } = await supabase.storage
.from(bucketName)
.remove([fileName]);

if (error) {
console.error('Error deleting file from Supabase:', error.message);
throw error;
}
} catch (error) {
console.error('Error deleting image:', error);
throw new Error('Failed to delete image');
}
};
28 changes: 28 additions & 0 deletions src/services/updateImage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { deleteGiftThumbnail } from '@/services/deleteImage';
import { uploadGiftThumbnail } from '@/services/uploadImage';

/**
* Updates an image in Supabase storage by deleting the old image and uploading a new one.
* @param oldImageUrl - The public URL of the old image to delete.
* @param newImageUri - The URI of the new image to upload.
* @param bucketName - The name of the Supabase storage bucket (default: 'gift-thumbnail').
* @returns The public URL of the new image.
*/
export const updateGiftThumbnail = async (
oldImageUrl: string,
newImageUri: string,
bucketName: string = 'gift-thumbnail',
): Promise<string> => {
try {
if (oldImageUrl) {
await deleteGiftThumbnail(oldImageUrl, bucketName);
}

const newImageUrl = await uploadGiftThumbnail(newImageUri, bucketName);

return newImageUrl;
} catch (error) {
console.error('Error updating image:', error);
throw new Error('Failed to update image');
}
};
51 changes: 51 additions & 0 deletions src/services/uploadImage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import * as FileSystem from 'expo-file-system';

import supabase from '@/services/supabaseClient';

export const uploadGiftThumbnail = async (
uri: string,
bucketName: string = 'gift-thumbnail',
): Promise<string> => {
try {
const fileName = uri.split('/').pop();
if (!fileName) {
console.error('Invalid file name');
throw new Error('Invalid file name');
}

const fileInfo = await FileSystem.readAsStringAsync(uri, {
encoding: FileSystem.EncodingType.Base64,
});

const arrayBuffer = Uint8Array.from(atob(fileInfo), (c) =>
c.charCodeAt(0),
).buffer;

const { data, error } = await supabase.storage
.from(bucketName)
.upload(fileName, arrayBuffer, {
contentType: 'image/jpeg',
cacheControl: '3600',
upsert: true,
});

if (error) {
console.error('Error uploading file to Supabase:', error.message);
throw error;
}

const { data: publicUrlData } = supabase.storage
.from(bucketName)
.getPublicUrl(fileName);

if (!publicUrlData?.publicUrl) {
console.error('Failed to retrieve public URL');
throw new Error('Failed to retrieve public URL');
}

return publicUrlData.publicUrl;
} catch (error) {
console.error('Error uploading image:', error);
throw new Error('Failed to upload image');
}
};