Add default metadata & make replaceFile function

This commit is contained in:
2025-06-01 16:36:29 -05:00
parent dc7cec8539
commit f49488123b
33 changed files with 194 additions and 51 deletions

View File

@ -31,7 +31,6 @@ export type UploadStorageProps = {
export type ReplaceStorageProps = {
bucket: string;
prevPath: string;
path: string;
file: File;
options?: {
cacheControl?: string;
@ -137,21 +136,22 @@ export const uploadFile = async ({
export const replaceFile = async ({
bucket,
prevPath,
path,
file,
options = {},
}: ReplaceStorageProps): Promise<Result<string>> => {
try {
options.upsert = true;
const supabase = await createServerClient();
//const deleteFileData = await deleteFile({
//bucket,
//path: [...prevPath],
//});
const { data, error } = await supabase.storage
.from(bucket)
.update(path, file, options);
//.update(path, file, options);
.update(prevPath, file, options);
if (error) throw error;
if (!data?.path) throw new Error('No path returned from upload');
const deleteFileData = await deleteFile({
bucket,
path: [...prevPath],
});
return { success: true, data: data.path };
} catch (error) {
return {

View File

@ -1,7 +1,7 @@
'use client'
import { useState, useRef } from 'react';
import { deleteFile, uploadFile } from '@/lib/actions';
import { deleteFile, replaceFile, uploadFile } from '@/lib/actions';
import { toast } from 'sonner';
import { useAuth } from '@/components/context/auth';
import { resizeImage } from '@/lib/hooks';
@ -43,6 +43,21 @@ export const useFileUpload = () => {
console.error('Error deleting file:', deleteResult.error);
throw new Error(deleteResult.error || `Failed to delete ${prevPath}`);
} else console.log('Delete sucessful!')
console.log('Deleted file path: ', deleteResult.data)
const updateResult = await replaceFile({
bucket,
prevPath: prevPath,
file,
options: {
contentType: file.type,
},
});
if (!updateResult.success) {
console.error('Error updating file:', updateResult.error);
} else {
console.log('We used the new update function hopefully it worked!');
return { success: true, path: updateResult.data };
}
}
let fileToUpload = file;