-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dc7c3ec
commit 74e9e7d
Showing
3 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace App\Rules; | ||
|
||
namespace TantHammar\LaravelRules\Rules; | ||
|
||
use Illuminate\Contracts\Validation\Rule; | ||
use Storage; | ||
|
||
/** | ||
* $value must be an array with files | ||
* Default max is 5 MB (5000Kb) | ||
*/ | ||
class MaxTotalFileSize implements Rule | ||
{ | ||
public function __construct( | ||
public int $maxKb = 5000 | ||
) {} | ||
|
||
public function passes($attribute, $value): bool | ||
{ | ||
if(!is_array($value)) { | ||
return false; | ||
} | ||
|
||
if(blank($value)) { | ||
return true; | ||
} | ||
|
||
$total_size = array_reduce($value, static function ($sum, $file) { | ||
// each item is UploadedFile Object | ||
$sum += Storage::size((Storage::path($file))); | ||
return $sum; | ||
}); | ||
|
||
return $total_size <= ($this->maxKb * 1024); | ||
} | ||
|
||
public function message(): string | ||
{ | ||
return __('laravel-rules::messages.total-file-sizes', ['kb' => $this->maxKb]); | ||
} | ||
} |