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');
|
$storagePublic = storage_path('app/public');
|
||||||
|
|
||||||
|
if (!is_dir($storagePublic)) {
|
||||||
|
mkdir($storagePublic, 0775, true);
|
||||||
|
}
|
||||||
|
|
||||||
$zip = new ZipArchive();
|
$zip = new ZipArchive();
|
||||||
$zipPath = storage_path('exports/assets-'.date('YmdHis').'.zip');
|
$zipPath = storage_path('exports/assets-'.date('YmdHis').'.zip');
|
||||||
|
|
||||||
@@ -24,11 +29,13 @@ class AssetController extends Controller
|
|||||||
mkdir(storage_path('exports'), 0775, true);
|
mkdir(storage_path('exports'), 0775, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($zip->open($zipPath, ZipArchive::CREATE) === true) {
|
if ($zip->open($zipPath, ZipArchive::CREATE) !== true) {
|
||||||
$this->addDirectoryToZip($zip, $storagePublic, '');
|
abort(500, 'Failed to create ZIP archive');
|
||||||
$zip->close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->addDirectoryToZip($zip, $storagePublic, '');
|
||||||
|
$zip->close();
|
||||||
|
|
||||||
return response()->download($zipPath)->deleteFileAfterSend(true);
|
return response()->download($zipPath)->deleteFileAfterSend(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,7 +87,14 @@ class AssetController extends Controller
|
|||||||
|
|
||||||
private function addDirectoryToZip(ZipArchive $zip, string $dir, string $zipPath): void
|
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) {
|
foreach ($files as $file) {
|
||||||
if ($file === '.' || $file === '..') {
|
if ($file === '.' || $file === '..') {
|
||||||
@@ -88,13 +102,13 @@ class AssetController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
$path = $dir.DIRECTORY_SEPARATOR.$file;
|
$path = $dir.DIRECTORY_SEPARATOR.$file;
|
||||||
$zipPath = $zipPath === '' ? $file : $zipPath.'/'.$file;
|
$newZipPath = $zipPath === '' ? $file : $zipPath.'/'.$file;
|
||||||
|
|
||||||
if (is_dir($path)) {
|
if (is_dir($path)) {
|
||||||
$zip->addEmptyDir($zipPath);
|
$zip->addEmptyDir($newZipPath);
|
||||||
$this->addDirectoryToZip($zip, $path, $zipPath);
|
$this->addDirectoryToZip($zip, $path, $newZipPath);
|
||||||
} else {
|
} elseif (is_file($path)) {
|
||||||
$zip->addFile($path, $zipPath);
|
$zip->addFile($path, $newZipPath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user