From a9c803a5a4653b92718deb75643232db42f61e27 Mon Sep 17 00:00:00 2001 From: farhantock Date: Wed, 28 Feb 2024 10:16:02 +0700 Subject: [PATCH] handle when Directory not found --- app/Http/Controllers/Controller.php | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.php index e320f72..2a59ecd 100644 --- a/app/Http/Controllers/Controller.php +++ b/app/Http/Controllers/Controller.php @@ -15,6 +15,7 @@ use Illuminate\Support\Facades\Http; use Laravel\Lumen\Routing\Controller as BaseController; use RecursiveIteratorIterator; use RecursiveDirectoryIterator; +use Exception; class Controller extends BaseController { @@ -60,13 +61,25 @@ class Controller extends BaseController { $directory = 'assets/' . urldecode($company_name); + if (!is_dir($directory)) { + return "Directory not found"; + } + $size = 0; - foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file) { - $size += $file->getSize(); + + + try { + foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file) { + $size += $file->getSize(); + } + } catch (Exception $e) { + return "Error while calculating size: " . $e->getMessage(); } + return round($size / 1048576, 2); } + protected function setLimitsStorage($company, $dokumen, $name, $initPath, $destinatePath) { $totalSize = 0;