Speaking endpoints and polling fixed
This commit is contained in:
@@ -12,51 +12,92 @@ export default withIronSessionApiRoute(handler, sessionOptions);
|
||||
|
||||
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (!req.session.user) {
|
||||
res.status(401).json({ ok: false });
|
||||
return;
|
||||
res.status(401).json({ ok: false });
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
const form = formidable({ keepExtensions: true });
|
||||
|
||||
|
||||
await form.parse(req, async (err: any, fields: any, files: any) => {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
res.status(500).json({ ok: false });
|
||||
return;
|
||||
}
|
||||
|
||||
if (err) {
|
||||
console.error('Error parsing form:', err);
|
||||
res.status(500).json({ ok: false, error: 'Failed to parse form data' });
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const formData = new FormData();
|
||||
|
||||
if (!fields.userId || !fields.sessionId || !fields.exerciseId || !fields.task) {
|
||||
throw new Error('Missing required fields');
|
||||
}
|
||||
|
||||
formData.append('userId', fields.userId);
|
||||
formData.append('sessionId', fields.sessionId);
|
||||
formData.append('exerciseId', fields.exerciseId);
|
||||
|
||||
Object.keys(files).forEach(fileKey => {
|
||||
const index = fileKey.split('_')[1];
|
||||
const questionKey = `question_${index}`;
|
||||
|
||||
const audioFile = files[fileKey];
|
||||
const binary = fs.readFileSync((audioFile as any).path);
|
||||
formData.append(`audio_${index}`, binary, 'audio.wav');
|
||||
|
||||
for (const fileKey of Object.keys(files)) {
|
||||
const indexMatch = fileKey.match(/^audio_(\d+)$/);
|
||||
if (!indexMatch) {
|
||||
console.warn(`Skipping invalid file key: ${fileKey}`);
|
||||
continue;
|
||||
}
|
||||
|
||||
const index = indexMatch[1];
|
||||
const questionKey = `question_${index}`;
|
||||
const audioFile = files[fileKey];
|
||||
|
||||
if (!audioFile || !audioFile.path) {
|
||||
throw new Error(`Invalid audio file for ${fileKey}`);
|
||||
}
|
||||
|
||||
if (!fields[questionKey]) {
|
||||
throw new Error(`Missing question for audio ${index}`);
|
||||
}
|
||||
|
||||
try {
|
||||
const buffer = fs.readFileSync(audioFile.path);
|
||||
formData.append(`audio_${index}`, buffer, `audio_${index}.wav`);
|
||||
formData.append(questionKey, fields[questionKey]);
|
||||
|
||||
fs.rmSync((audioFile as any).path);
|
||||
});
|
||||
|
||||
fs.rmSync(audioFile.path);
|
||||
} catch (fileError) {
|
||||
console.error(`Error processing file ${fileKey}:`, fileError);
|
||||
throw new Error(`Failed to process audio file ${index}`);
|
||||
}
|
||||
}
|
||||
|
||||
await axios.post(
|
||||
`${process.env.BACKEND_URL}/grade/speaking/${fields.task}`,
|
||||
formData,
|
||||
{
|
||||
headers: {
|
||||
...formData.getHeaders(),
|
||||
Authorization: `Bearer ${process.env.BACKEND_JWT}`,
|
||||
},
|
||||
}
|
||||
`${process.env.BACKEND_URL}/grade/speaking/${fields.task}`,
|
||||
formData,
|
||||
{
|
||||
headers: {
|
||||
...formData.getHeaders(),
|
||||
Authorization: `Bearer ${process.env.BACKEND_JWT}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
res.status(200).json({ ok: true });
|
||||
} catch (error) {
|
||||
console.error('Error processing request:', error);
|
||||
res.status(500).json({
|
||||
ok: false,
|
||||
error: 'Internal server error'
|
||||
});
|
||||
|
||||
Object.keys(files).forEach(fileKey => {
|
||||
const audioFile = files[fileKey];
|
||||
if (audioFile && audioFile.path && fs.existsSync(audioFile.path)) {
|
||||
try {
|
||||
fs.rmSync(audioFile.path);
|
||||
} catch (cleanupError) {
|
||||
console.error(`Failed to clean up temp file ${audioFile.path}:`, cleanupError);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export const config = {
|
||||
api: {
|
||||
|
||||
@@ -25,29 +25,45 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
return;
|
||||
}
|
||||
|
||||
const formData = new FormData();
|
||||
formData.append('userId', fields.userId);
|
||||
formData.append('sessionId', fields.sessionId);
|
||||
formData.append('exerciseId', fields.exerciseId);
|
||||
formData.append('question_1', fields.question);
|
||||
try {
|
||||
const formData = new FormData();
|
||||
formData.append('userId', fields.userId);
|
||||
formData.append('sessionId', fields.sessionId);
|
||||
formData.append('exerciseId', fields.exerciseId);
|
||||
formData.append('question_1', fields.question);
|
||||
|
||||
const audioFile = files.audio;
|
||||
const binary = fs.readFileSync((audioFile as any).path);
|
||||
formData.append('audio_1', binary, 'audio.wav');
|
||||
fs.rmSync((audioFile as any).path);
|
||||
|
||||
await axios.post(
|
||||
`${process.env.BACKEND_URL}/grade/speaking/2`,
|
||||
formData,
|
||||
{
|
||||
headers: {
|
||||
...formData.getHeaders(),
|
||||
Authorization: `Bearer ${process.env.BACKEND_JWT}`,
|
||||
},
|
||||
const audioFile = files.audio;
|
||||
if (!audioFile || !audioFile.path) {
|
||||
throw new Error('Audio file not found in request');
|
||||
}
|
||||
);
|
||||
|
||||
res.status(200).json({ ok: true });
|
||||
const buffer = fs.readFileSync(audioFile.path);
|
||||
formData.append('audio_1', buffer, 'audio_1.wav');
|
||||
fs.rmSync(audioFile.path);
|
||||
|
||||
await axios.post(
|
||||
`${process.env.BACKEND_URL}/grade/speaking/2`,
|
||||
formData,
|
||||
{
|
||||
headers: {
|
||||
...formData.getHeaders(),
|
||||
Authorization: `Bearer ${process.env.BACKEND_JWT}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
res.status(200).json({ ok: true });
|
||||
} catch (error) {
|
||||
console.error('Error:', error);
|
||||
if (files.audio?.path && fs.existsSync(files.audio.path)) {
|
||||
try {
|
||||
fs.rmSync(files.audio.path);
|
||||
} catch (e) {
|
||||
console.error('Failed to cleanup file:', e);
|
||||
}
|
||||
}
|
||||
res.status(500).json({ ok: false });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user