Updated the Formidable to work with serverless (supposedly)
This commit is contained in:
@@ -24,7 +24,7 @@
|
|||||||
"eslint-config-next": "13.1.6",
|
"eslint-config-next": "13.1.6",
|
||||||
"firebase": "9.19.1",
|
"firebase": "9.19.1",
|
||||||
"formidable": "^3.5.0",
|
"formidable": "^3.5.0",
|
||||||
"formidable-serverless": "^1.1.1",
|
"formidable-serverless": "git://github.com/JoeRoddy/formidable-serverless.git",
|
||||||
"framer-motion": "^9.0.2",
|
"framer-motion": "^9.0.2",
|
||||||
"iron-session": "^6.3.1",
|
"iron-session": "^6.3.1",
|
||||||
"lodash": "^4.17.21",
|
"lodash": "^4.17.21",
|
||||||
|
|||||||
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 {withIronSessionApiRoute} from "iron-session/next";
|
||||||
import {sessionOptions} from "@/lib/session";
|
import {sessionOptions} from "@/lib/session";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import formidable from "formidable";
|
import formidable from "formidable-serverless";
|
||||||
import PersistentFile from "formidable/PersistentFile";
|
|
||||||
import {getStorage, ref, uploadBytes} from "firebase/storage";
|
import {getStorage, ref, uploadBytes} from "firebase/storage";
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
import {app} from "@/firebase";
|
import {app} from "@/firebase";
|
||||||
@@ -20,25 +19,26 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
const storage = getStorage(app);
|
const storage = getStorage(app);
|
||||||
|
|
||||||
const form = formidable({keepExtensions: true, uploadDir: "./"});
|
const form = formidable({keepExtensions: true, uploadDir: "./"});
|
||||||
const [fields, files] = await form.parse(req);
|
await form.parse(req, async (err: any, fields: any, files: any) => {
|
||||||
const audioFile = (files.audio as unknown as PersistentFile[])[0];
|
const audioFile = files.audio;
|
||||||
const audioFileRef = ref(storage, `speaking_recordings/${(audioFile as any).newFilename}`);
|
const audioFileRef = ref(storage, `speaking_recordings/${(audioFile as any).path.replace("upload_", "")}`);
|
||||||
|
|
||||||
const binary = fs.readFileSync((audioFile as any).filepath).buffer;
|
const binary = fs.readFileSync((audioFile as any).path).buffer;
|
||||||
const snapshot = await uploadBytes(audioFileRef, binary);
|
const snapshot = await uploadBytes(audioFileRef, binary);
|
||||||
|
|
||||||
const backendRequest = await axios.post(
|
const backendRequest = await axios.post(
|
||||||
`${process.env.BACKEND_URL}/speaking_task_1`,
|
`${process.env.BACKEND_URL}/speaking_task_1`,
|
||||||
{question: (fields.question as string[]).join(""), answer: snapshot.metadata.fullPath},
|
{question: fields.question, answer: snapshot.metadata.fullPath},
|
||||||
{
|
{
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${process.env.BACKEND_JWT}`,
|
Authorization: `Bearer ${process.env.BACKEND_JWT}`,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
);
|
||||||
);
|
|
||||||
|
|
||||||
fs.rmSync((audioFile as any).filepath);
|
fs.rmSync((audioFile as any).path);
|
||||||
res.status(200).json({...backendRequest.data, fullPath: snapshot.metadata.fullPath});
|
res.status(200).json({...backendRequest.data, fullPath: snapshot.metadata.fullPath});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export const config = {
|
export const config = {
|
||||||
|
|||||||
Reference in New Issue
Block a user