I’m trying to upload files from disk without setting up and endpoint or getting from the client but when I use the utapi functions, they do not work directly with the file path or the buffer gotten with fs.readFile(path).
How do I transform the file so I would be able to upload using utapi.uploadFiles(files).
I’m working with Nextjs 14.
Here is where I instantiated the UTAPI:
const utapi = new UTApi({
token: process.env.UPLOADTHING_TOKEN!,
});
Here is the function I’m using it from:
export async function UploadPowerpointToUploadThing(
fileBuffer: Buffer,
fileName: string
): Promise<UploadFileResult[]> {
try {
const file = new File([fileBuffer], fileName, {
type: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
});
console.log("THIS IS UTAPI FILE BEFORE UPLOAD", file);
const response = await utapi.uploadFiles([file]);
console.log("THIS IS UTAPI RESPONSE AFTER UPLOAD", response);
if (!response?.[0].data?.url) {
throw new Error("Upload failed - No URL returned");
}
return response;
} catch (error) {
console.error(error);
throw new Error("Failed to upload powerpoint to uploadthing");
}
}