Add default metadata & make replaceFile function
This commit is contained in:
@ -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 {
|
||||
|
@ -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;
|
||||
|
Reference in New Issue
Block a user