fix(backup): handle PostTooLargeException gracefully and add client-side file size guard

This commit is contained in:
Ray1111-aa
2026-10-28 11:59:42 +07:00
parent ee83b9d189
commit 03af709a12
3 changed files with 43 additions and 3 deletions
@@ -60,7 +60,35 @@ class BackupController extends Controller
$log->file_exists = File::exists($fullPath); $log->file_exists = File::exists($fullPath);
}); });
return view('admin.backups.index', compact('backupLogs', 'backups', 'stats', 'tableGroups')); // Ambil info batas upload server PHP saat ini
$uploadMax = ini_get('upload_max_filesize') ?: '256M';
$postMax = ini_get('post_max_size') ?: '256M';
$maxUploadBytes = min($this->parseIniBytes($uploadMax), $this->parseIniBytes($postMax));
$maxUploadHuman = $this->backupService->formatBytes($maxUploadBytes);
return view('admin.backups.index', compact('backupLogs', 'backups', 'stats', 'tableGroups', 'maxUploadBytes', 'maxUploadHuman'));
}
/**
* Konversi notasi string PHP INI (misal 256M, 1G) ke byte integer.
*/
protected function parseIniBytes(string $val): int
{
$val = trim($val);
$last = strtolower($val[strlen($val) - 1] ?? '');
$num = (int) $val;
switch ($last) {
case 'g':
$num *= 1024 * 1024 * 1024;
break;
case 'm':
$num *= 1024 * 1024;
break;
case 'k':
$num *= 1024;
break;
}
return $num > 0 ? $num : 268435456; // Fallback 256 MB
} }
/** /**
+5 -1
View File
@@ -18,5 +18,9 @@ return Application::configure(basePath: dirname(__DIR__))
]); ]);
}) })
->withExceptions(function (Exceptions $exceptions): void { ->withExceptions(function (Exceptions $exceptions): void {
// $exceptions->render(function (\Illuminate\Http\Exceptions\PostTooLargeException $e, $request) {
$postMax = ini_get('post_max_size') ?: '256M';
$uploadMax = ini_get('upload_max_filesize') ?: '256M';
return back()->with('error', "Ukuran file yang Anda kirim terlalu besar dan melebihi batas konfigurasi server PHP (post_max_size: {$postMax}, upload_max_filesize: {$uploadMax}). Harap naikkan batas konfigurasi php.ini atau gunakan file yang lebih kecil.");
});
})->create(); })->create();
@@ -444,7 +444,7 @@
</div> </div>
<p class="text-xs font-bold text-slate-800" id="directRestoreFileName">Tarik &amp; Letakkan file .kmh di sini</p> <p class="text-xs font-bold text-slate-800" id="directRestoreFileName">Tarik &amp; Letakkan file .kmh di sini</p>
<p class="text-[11px] text-slate-400 mt-1">atau <span class="text-amber-700 font-semibold underline">klik untuk memilih dari komputer Anda</span></p> <p class="text-[11px] text-slate-400 mt-1">atau <span class="text-amber-700 font-semibold underline">klik untuk memilih dari komputer Anda</span></p>
<p class="text-[10px] text-slate-400 mt-1.5">Format didukung: .kmh terenkripsi (Maks. 200 MB)</p> <p class="text-[10px] text-slate-400 mt-1.5">Format didukung: .kmh terenkripsi (Batas upload server: {{ $maxUploadHuman ?? '256 MB' }})</p>
</div> </div>
<div id="directRestoreFileDisplay" class="hidden mt-1 p-3 rounded-xl bg-amber-50 border border-amber-200 text-left"> <div id="directRestoreFileDisplay" class="hidden mt-1 p-3 rounded-xl bg-amber-50 border border-amber-200 text-left">
@@ -752,6 +752,14 @@
function handleDirectRestoreFileChange(input) { function handleDirectRestoreFileChange(input) {
if (input.files && input.files[0]) { if (input.files && input.files[0]) {
const file = input.files[0]; const file = input.files[0];
const maxBytes = {{ $maxUploadBytes ?? 268435456 }};
if (file.size > maxBytes) {
const fileMb = (file.size / (1024 * 1024)).toFixed(1);
const limitMb = (maxBytes / (1024 * 1024)).toFixed(0);
alert(`Peringatan: Ukuran file (${fileMb} MB) melebihi batas upload PHP server (${limitMb} MB). Mohon sesuaikan pengaturan php.ini server Anda.`);
input.value = '';
return;
}
const sizeHuman = (file.size / (1024 * 1024)).toFixed(2) + ' MB'; const sizeHuman = (file.size / (1024 * 1024)).toFixed(2) + ' MB';
document.getElementById('directRestorePillName').textContent = file.name; document.getElementById('directRestorePillName').textContent = file.name;
document.getElementById('directRestorePillSize').textContent = sizeHuman; document.getElementById('directRestorePillSize').textContent = sizeHuman;