Updated the Formidable to work with serverless (supposedly)

This commit is contained in:
Tiago Ribeiro
2023-07-21 13:37:41 +01:00
parent eae0a4ae4e
commit ea41875e36
3 changed files with 20 additions and 19 deletions

View File

@@ -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
View File

@@ -0,0 +1 @@
declare module "formidable-serverless";

View File

@@ -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,16 +19,16 @@ 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}`,
@@ -37,8 +36,9 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
}, },
); );
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 = {