Finallyyyyyy finished the whole Speaking flow along with the solution page

This commit is contained in:
Tiago Ribeiro
2023-07-14 14:15:07 +01:00
parent 2c10a203a5
commit 121ac8ba4d
10 changed files with 206 additions and 76 deletions

View File

@@ -20,26 +20,25 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
const storage = getStorage(app);
const form = formidable({keepExtensions: true, uploadDir: "./"});
form.parse(req, (err, fields, files) => {
const audioFile = (files.audio as unknown as PersistentFile[])[0];
const audioFileRef = ref(storage, `speaking_recordings/${(audioFile as any).newFilename}`);
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}`);
const binary = fs.readFileSync((audioFile as any).filepath).buffer;
uploadBytes(audioFileRef, binary).then(async (snapshot) => {
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 binary = fs.readFileSync((audioFile as any).filepath).buffer;
const snapshot = await uploadBytes(audioFileRef, binary);
fs.rmSync((audioFile as any).filepath);
res.status(200).json({...backendRequest.data, fullPath: snapshot.metadata.fullPath});
});
});
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}`,
},
},
);
fs.rmSync((audioFile as any).filepath);
res.status(200).json({...backendRequest.data, fullPath: snapshot.metadata.fullPath});
}
export const config = {

View File

@@ -4,19 +4,23 @@ import {withIronSessionApiRoute} from "iron-session/next";
import {sessionOptions} from "@/lib/session";
import {getDownloadURL, getStorage, ref} from "firebase/storage";
import {app} from "@/firebase";
import axios from "axios";
// export default withIronSessionApiRoute(handler, sessionOptions);
export default handler;
export default withIronSessionApiRoute(handler, sessionOptions);
async function handler(req: NextApiRequest, res: NextApiResponse) {
// if (!req.session.user) {
// res.status(401).json({ok: false});
// return;
// }
if (!req.session.user) {
res.status(401).json({ok: false});
return;
}
const storage = getStorage(app);
const {path} = req.body as {path: string};
const pathReference = ref(storage, path);
getDownloadURL(pathReference).then((url) => res.status(200).json({url}));
const url = await getDownloadURL(pathReference);
const response = await axios.get(url, {responseType: "arraybuffer"});
res.status(200).send(response.data);
}