Updated the Formidable to work with serverless (supposedly)
This commit is contained in:
@@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
1
src/lib/formidable-serverless.d.ts
vendored
Normal file
1
src/lib/formidable-serverless.d.ts
vendored
Normal file
@@ -0,0 +1 @@
|
||||
declare module "formidable-serverless";
|
||||
@@ -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 = {
|
||||
|
||||
Reference in New Issue
Block a user