Fix asset export error handling for empty directories
This commit is contained in:
@@ -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,10 +29,12 @@ class AssetController extends Controller
|
||||
mkdir(storage_path('exports'), 0775, true);
|
||||
}
|
||||
|
||||
if ($zip->open($zipPath, ZipArchive::CREATE) === true) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user