diff --git a/app/Controllers/Home.php b/app/Controllers/Home.php index 95e2d98..05a6d8a 100644 --- a/app/Controllers/Home.php +++ b/app/Controllers/Home.php @@ -259,8 +259,33 @@ class Home extends BaseController } $file = $this->request->getFile('audio'); - if ($file === null || ! $file->isValid()) { - return $this->response->setStatusCode(400)->setJSON(['error' => 'No valid audio file received']); + if ($file === null) { + // Fallback key for compatibility with alternate STT clients. + $file = $this->request->getFile('file'); + } + + if ($file === null) { + return $this->response->setStatusCode(400)->setJSON([ + 'error' => 'No audio file received', + 'expected_field' => 'audio', + ]); + } + + if (! $file->isValid()) { + return $this->response->setStatusCode(400)->setJSON([ + 'error' => 'Invalid uploaded audio file', + 'upload_error_code' => $file->getError(), + 'upload_error' => $file->getErrorString(), + 'upload_max_filesize' => (string) ini_get('upload_max_filesize'), + 'post_max_size' => (string) ini_get('post_max_size'), + ]); + } + + $fileSize = (int) $file->getSize(); + if ($fileSize <= 0) { + return $this->response->setStatusCode(400)->setJSON([ + 'error' => 'Empty audio received', + ]); } $lang = (string) ($this->request->getPost('lang') ?? 'en-IN'); @@ -294,7 +319,7 @@ class Home extends BaseController ->setJSON(['error' => 'STT server unreachable', 'detail' => $curlErr]); } - $decoded = json_decode($body, true); + $decoded = json_decode((string) $body, true); if ($decoded === null) { return $this->response->setStatusCode(502) ->setJSON(['error' => 'Invalid response from STT server']); diff --git a/app/Views/billing/opd_prescription_basic.php b/app/Views/billing/opd_prescription_basic.php index 2532835..eb44cd2 100644 --- a/app/Views/billing/opd_prescription_basic.php +++ b/app/Views/billing/opd_prescription_basic.php @@ -1207,6 +1207,11 @@ medicalSttTarget = ''; setMedicalMicButtonState(''); + if (!blob || !blob.size || blob.size < 1024) { + $('.jsError').removeClass('text-success text-muted').addClass('text-danger').text('Recording too short. Please record for at least 1-2 seconds.'); + return; + } + sendMedicalAudioToServer(blob, targetForTranscript).then(function(transcript) { appendTranscriptToField(targetForTranscript, transcript); $('.jsError').removeClass('text-danger text-muted').addClass('text-success').text('Medical speech text added.'); @@ -1361,6 +1366,11 @@ stopComplaintsMediaStream(); setComplaintsMicListening(false); + if (!blob || !blob.size || blob.size < 1024) { + $('.jsError').removeClass('text-success text-muted').addClass('text-danger').text('Recording too short. Please record for at least 1-2 seconds.'); + return; + } + sendComplaintsAudioToServer(blob).then(function(transcript) { appendTranscriptToComplaints(transcript); $('.jsError').removeClass('text-danger text-muted').addClass('text-success').text('Complaints speech text added.');