Fix asset export error handling for empty directories
CI/CD Pipeline / deploy (push) Successful in 24s
CI/CD Pipeline / promote_stable (push) Successful in 2s

This commit is contained in:
2026-05-16 16:36:40 +02:00
parent d4d7934c89
commit f650bb7c86
+23 -9
View File
@@ -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);
}
}
}