From f650bb7c867d00487dae665e652ca65e4f3db044 Mon Sep 17 00:00:00 2001 From: tracer Date: Sat, 16 May 2026 16:36:40 +0200 Subject: [PATCH] Fix asset export error handling for empty directories --- app/Http/Controllers/AssetController.php | 32 +++++++++++++++++------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/app/Http/Controllers/AssetController.php b/app/Http/Controllers/AssetController.php index f181f67..cc9af28 100644 --- a/app/Http/Controllers/AssetController.php +++ b/app/Http/Controllers/AssetController.php @@ -17,6 +17,11 @@ class AssetController extends Controller } $storagePublic = storage_path('app/public'); + + if (!is_dir($storagePublic)) { + mkdir($storagePublic, 0775, true); + } + $zip = new ZipArchive(); $zipPath = storage_path('exports/assets-'.date('YmdHis').'.zip'); @@ -24,11 +29,13 @@ class AssetController extends Controller mkdir(storage_path('exports'), 0775, true); } - if ($zip->open($zipPath, ZipArchive::CREATE) === true) { - $this->addDirectoryToZip($zip, $storagePublic, ''); - $zip->close(); + if ($zip->open($zipPath, ZipArchive::CREATE) !== true) { + abort(500, 'Failed to create ZIP archive'); } + $this->addDirectoryToZip($zip, $storagePublic, ''); + $zip->close(); + return response()->download($zipPath)->deleteFileAfterSend(true); } @@ -80,7 +87,14 @@ class AssetController extends Controller private function addDirectoryToZip(ZipArchive $zip, string $dir, string $zipPath): void { - $files = scandir($dir); + if (!is_dir($dir)) { + return; + } + + $files = @scandir($dir); + if ($files === false) { + return; + } foreach ($files as $file) { if ($file === '.' || $file === '..') { @@ -88,13 +102,13 @@ class AssetController extends Controller } $path = $dir.DIRECTORY_SEPARATOR.$file; - $zipPath = $zipPath === '' ? $file : $zipPath.'/'.$file; + $newZipPath = $zipPath === '' ? $file : $zipPath.'/'.$file; if (is_dir($path)) { - $zip->addEmptyDir($zipPath); - $this->addDirectoryToZip($zip, $path, $zipPath); - } else { - $zip->addFile($path, $zipPath); + $zip->addEmptyDir($newZipPath); + $this->addDirectoryToZip($zip, $path, $newZipPath); + } elseif (is_file($path)) { + $zip->addFile($path, $newZipPath); } } }