diff --git a/app/Controllers/Opd.php b/app/Controllers/Opd.php index 8a3fc69..5491c2c 100644 --- a/app/Controllers/Opd.php +++ b/app/Controllers/Opd.php @@ -6104,6 +6104,7 @@ class Opd extends BaseController { $opdRow = $this->db->table('opd_master')->where('opd_id', $opdid)->get(1)->getRowArray(); if (empty($opdRow)) { + log_message('warning', 'OPD scan upload failed: OPD not found (opd_id={opd_id})', ['opd_id' => $opdid]); return $this->response->setStatusCode(404)->setJSON(['update' => 0, 'error_text' => 'OPD not found']); } @@ -6111,6 +6112,14 @@ class Opd extends BaseController if (!is_dir($uploadsDir)) { @mkdir($uploadsDir, 0777, true); } + if (!is_dir($uploadsDir) || !is_writable($uploadsDir)) { + log_message('error', 'OPD scan upload dir issue (opd_id={opd_id}, dir={dir}, exists={exists}, writable={writable})', [ + 'opd_id' => $opdid, + 'dir' => $uploadsDir, + 'exists' => is_dir($uploadsDir) ? 'yes' : 'no', + 'writable' => is_writable($uploadsDir) ? 'yes' : 'no', + ]); + } $baseFilename = 'OPD-' . $opdid . '-' . time(); $filename = $baseFilename . '.jpg'; @@ -6130,11 +6139,19 @@ class Opd extends BaseController } $filename = $baseFilename . '.' . $ext; $fullPath = $uploadsDir . '/' . $filename; - $uploadedFile->move($uploadsDir, $filename); - $saved = is_file($fullPath); - $detectedMimeType = (string) ($uploadedFile->getClientMimeType() ?: 'image/jpeg'); - $isImageFile = 1; - $detectedExt = '.' . $ext; + try { + $uploadedFile->move($uploadsDir, $filename); + $saved = is_file($fullPath); + $detectedMimeType = (string) ($uploadedFile->getClientMimeType() ?: 'image/jpeg'); + $isImageFile = 1; + $detectedExt = '.' . $ext; + } catch (\Throwable $e) { + log_message('error', 'OPD webcam upload move failed (opd_id={opd_id}, dir={dir}, error={error})', [ + 'opd_id' => $opdid, + 'dir' => $uploadsDir, + 'error' => $e->getMessage(), + ]); + } } // Source 2: manual file upload (PDF/image). @@ -6152,11 +6169,20 @@ class Opd extends BaseController } $filename = $baseFilename . '.' . $ext; $fullPath = $uploadsDir . '/' . $filename; - $uploadedFile->move($uploadsDir, $filename); - $saved = is_file($fullPath); - $detectedMimeType = (string) ($uploadedFile->getClientMimeType() ?: ($ext === 'pdf' ? 'application/pdf' : 'image/jpeg')); - $isImageFile = $ext === 'pdf' ? 0 : 1; - $detectedExt = '.' . $ext; + try { + $uploadedFile->move($uploadsDir, $filename); + $saved = is_file($fullPath); + $detectedMimeType = (string) ($uploadedFile->getClientMimeType() ?: ($ext === 'pdf' ? 'application/pdf' : 'image/jpeg')); + $isImageFile = $ext === 'pdf' ? 0 : 1; + $detectedExt = '.' . $ext; + } catch (\Throwable $e) { + log_message('error', 'OPD userfile upload move failed (opd_id={opd_id}, ext={ext}, dir={dir}, error={error})', [ + 'opd_id' => $opdid, + 'ext' => $ext, + 'dir' => $uploadsDir, + 'error' => $e->getMessage(), + ]); + } } } @@ -6176,16 +6202,32 @@ class Opd extends BaseController $fullPath = $uploadsDir . '/' . $filename; $raw = base64_decode((string) preg_replace('#^data:image/\w+;base64,#i', '', $dataUri)); if ($raw !== false && $raw !== '') { - file_put_contents($fullPath, $raw); - $saved = is_file($fullPath); - $detectedMimeType = 'image/' . ($imgExt === 'jpg' ? 'jpeg' : $imgExt); - $isImageFile = 1; - $detectedExt = '.' . $imgExt; + try { + file_put_contents($fullPath, $raw); + $saved = is_file($fullPath); + $detectedMimeType = 'image/' . ($imgExt === 'jpg' ? 'jpeg' : $imgExt); + $isImageFile = 1; + $detectedExt = '.' . $imgExt; + } catch (\Throwable $e) { + log_message('error', 'OPD base64 upload save failed (opd_id={opd_id}, ext={ext}, file={file}, error={error})', [ + 'opd_id' => $opdid, + 'ext' => $imgExt, + 'file' => $fullPath, + 'error' => $e->getMessage(), + ]); + } } } } if (!$saved) { + log_message('error', 'OPD scan save failed: no source persisted (opd_id={opd_id}, dir={dir}, has_webcam={has_webcam}, has_userfile={has_userfile}, has_data_uri={has_data_uri})', [ + 'opd_id' => $opdid, + 'dir' => $uploadsDir, + 'has_webcam' => ($this->request->getFile('webcam') !== null) ? 1 : 0, + 'has_userfile' => ($this->request->getFile('userfile') !== null) ? 1 : 0, + 'has_data_uri' => ((string) ($this->request->getPost('webcam_data') ?? $this->request->getPost('image') ?? '') !== '') ? 1 : 0, + ]); return $this->response->setJSON([ 'update' => 0, 'error_text' => 'Unable to capture image. Please allow camera and retry.', @@ -6211,6 +6253,13 @@ class Opd extends BaseController $isImageFile, $detectedExt ); + if ($insertId <= 0) { + log_message('error', 'OPD scan upload saved but DB insert failed (opd_id={opd_id}, file={file}, public_path={public_path})', [ + 'opd_id' => $opdid, + 'file' => $fullPath, + 'public_path' => $publicPath, + ]); + } $scanType = $this->detectScanType((string) $filename); @@ -6278,6 +6327,7 @@ class Opd extends BaseController public function opd_file_last_list(int $opdid = 0) { if (!$this->db->tableExists('file_upload_data')) { + log_message('error', 'OPD scan list failed: file_upload_data table missing (opd_id={opd_id})', ['opd_id' => $opdid]); return $this->response->setBody('