diff --git a/package.json b/package.json index 6f504b18..c46e4d55 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "eslint-config-next": "13.1.6", "firebase": "9.19.1", "formidable": "^3.5.0", - "formidable-serverless": "^1.1.1", + "formidable-serverless": "git://github.com/JoeRoddy/formidable-serverless.git", "framer-motion": "^9.0.2", "iron-session": "^6.3.1", "lodash": "^4.17.21", @@ -59,4 +59,4 @@ "postcss": "^8.4.21", "tailwindcss": "^3.2.4" } -} +} \ No newline at end of file diff --git a/src/lib/formidable-serverless.d.ts b/src/lib/formidable-serverless.d.ts new file mode 100644 index 00000000..f711ed59 --- /dev/null +++ b/src/lib/formidable-serverless.d.ts @@ -0,0 +1 @@ +declare module "formidable-serverless"; diff --git a/src/pages/api/evaluate/speaking.ts b/src/pages/api/evaluate/speaking.ts index 1241aae9..e5fac9b6 100644 --- a/src/pages/api/evaluate/speaking.ts +++ b/src/pages/api/evaluate/speaking.ts @@ -3,8 +3,7 @@ import type {NextApiRequest, NextApiResponse} from "next"; import {withIronSessionApiRoute} from "iron-session/next"; import {sessionOptions} from "@/lib/session"; import axios from "axios"; -import formidable from "formidable"; -import PersistentFile from "formidable/PersistentFile"; +import formidable from "formidable-serverless"; import {getStorage, ref, uploadBytes} from "firebase/storage"; import fs from "fs"; import {app} from "@/firebase"; @@ -20,25 +19,26 @@ async function handler(req: NextApiRequest, res: NextApiResponse) { const storage = getStorage(app); const form = formidable({keepExtensions: true, uploadDir: "./"}); - const [fields, files] = await form.parse(req); - const audioFile = (files.audio as unknown as PersistentFile[])[0]; - const audioFileRef = ref(storage, `speaking_recordings/${(audioFile as any).newFilename}`); + await form.parse(req, async (err: any, fields: any, files: any) => { + const audioFile = files.audio; + const audioFileRef = ref(storage, `speaking_recordings/${(audioFile as any).path.replace("upload_", "")}`); - const binary = fs.readFileSync((audioFile as any).filepath).buffer; - const snapshot = await uploadBytes(audioFileRef, binary); + const binary = fs.readFileSync((audioFile as any).path).buffer; + const snapshot = await uploadBytes(audioFileRef, binary); - const backendRequest = await axios.post( - `${process.env.BACKEND_URL}/speaking_task_1`, - {question: (fields.question as string[]).join(""), answer: snapshot.metadata.fullPath}, - { - headers: { - Authorization: `Bearer ${process.env.BACKEND_JWT}`, + const backendRequest = await axios.post( + `${process.env.BACKEND_URL}/speaking_task_1`, + {question: fields.question, answer: snapshot.metadata.fullPath}, + { + headers: { + Authorization: `Bearer ${process.env.BACKEND_JWT}`, + }, }, - }, - ); + ); - fs.rmSync((audioFile as any).filepath); - res.status(200).json({...backendRequest.data, fullPath: snapshot.metadata.fullPath}); + fs.rmSync((audioFile as any).path); + res.status(200).json({...backendRequest.data, fullPath: snapshot.metadata.fullPath}); + }); } export const config = {