I have figured a possible solution? However, I'm unsure if it makes sense to do so or just seems redundant?
But I have created an API route to effectively collect the data; then send it to a controller to separate the formData
and create different variables for the files and the payload; then I pass that controller data down to the server action.
This does seem to be working.
The API route looks something like this:
// api/upload/route.ts
import { handleFileUpload } from "@/app/controllers/upload.controller";
export async function POST(req: any) {
return handleFileUpload(req)
}
The controller looks something like this:
// controllers/upload.controller.ts
import { NextResponse } from "next/server"
import { uploadFileToS3 } from "@/app/actions/s3File.ts";
export const handleFileUpload = async (req: any) => {
try {
const form = await req.formData()
const files = form.getAll("files") as any
const payloadRAW = form.get("payload")
const {
uploadDestination,
mediaID,
bucket,
uploadEndpoint,
pathname,
redirectPath
} = JSON.parse(payloadRAW)
const payload = {
uploadDestination,
mediaID,
bucket,
uploadEndpoint,
pathname,
redirectPath
} as any
const uploadURL = await uploadFileToS3(files, payload);
return NextResponse.json({ message: "success", uploadURL });
} catch (error) {
console.error("Error uploading file:", error);
return NextResponse.json({ message: "failure", reason: error.message });
}
}
I then reconfigured my server action to take in the Files: File[]
instead of the formData: FormData
, along with some other general changes.