Using new max folder size validation rule class

parent fd593a4f
......@@ -3,13 +3,14 @@
namespace Modules\Media\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Modules\Media\Validators\MaxFolderSizeRule;
class UploadMediaRequest extends FormRequest
{
public function rules()
{
return [
'file' => ['required', 'max_size'],
'file' => ['required', new MaxFolderSizeRule()],
];
}
......@@ -17,33 +18,4 @@ class UploadMediaRequest extends FormRequest
{
return true;
}
public function messages()
{
$bytes = config('asgard.media.config.max-total-size');
$size = $this->formatBytes($bytes);
return [
'file.max_size' => trans('media::media.validation.max_size', ['size' => $size]),
];
}
public function formatBytes($bytes, $precision = 2)
{
$units = [
trans('media::media.file-sizes.B'),
trans('media::media.file-sizes.KB'),
trans('media::media.file-sizes.MB'),
trans('media::media.file-sizes.GB'),
trans('media::media.file-sizes.TB'),
];
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
$bytes /= pow(1024, $pow);
return round($bytes, $precision) . ' ' . $units[$pow];
}
}
<?php
/**
* Platform.
* @User nicolaswidart
* @Date 10/09/2017
* @Time 13:22
*/
namespace Modules\Media\Tests;
use Illuminate\Container\Container;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Http\UploadedFile;
use Illuminate\Validation\Factory as Validator;
use Illuminate\Translation\FileLoader;
use Illuminate\Translation\Translator;
use Modules\Media\Validators\MaxFolderSizeRule;
final class MaxFolderSizeRuleTest extends MediaTestCase
{
public function test_it_creates_instance_of_folder_size_validator()
{
$obj = new MaxFolderSizeRule();
$this->assertInstanceOf(MaxFolderSizeRule::class, $obj);
}
/** @test */
public function it_validates_max_folder_size_is_valid()
{
$this->app['config']->set('asgard.media.config.max-total-size', 1000); // Mocked folder size: 510
$validator = $this->buildValidator(UploadedFile::fake()->image('avatar.jpg'));
$this->assertTrue($validator->passes());
}
/** @test */
public function it_validates_max_folder_size_is_invalid()
{
$this->app['config']->set('asgard.media.config.max-total-size', 100); // Mocked folder size: 510
$validator = $this->buildValidator(UploadedFile::fake()->image('avatar.jpg'));
$this->assertFalse($validator->passes());
}
public function buildValidator($directorySize)
{
$app = new Container();
$app->singleton('app', 'Illuminate\Container\Container');
$translator = new Translator(new FileLoader(new Filesystem(), null), 'en');
$validator = (new Validator($translator))->make(['file' => $directorySize], [
'file' => ['required', new MaxFolderSizeRule()],
]);
return $validator;
}
}
<?php
namespace Modules\Media\Validators;
use Illuminate\Contracts\Validation\Rule;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class MaxFolderSizeRule implements Rule
{
/**
* Determine if the validation rule passes.
* @param string $attribute
* @param UploadedFile $value
* @return bool
*/
public function passes($attribute, $value)
{
$mediaPath = public_path(config('asgard.media.config.files-path'));
$folderSize = $this->getDirSize($mediaPath);
preg_match('/([0-9]+)/', $folderSize, $match);
return ($match[0] + $value->getSize()) < config('asgard.media.config.max-total-size');
}
/**
* Get the validation error message.
* @return string
*/
public function message()
{
$bytes = config('asgard.media.config.max-total-size');
$size = $this->formatBytes($bytes);
return trans('media::media.validation.max_size', ['size' => $size]);
}
/**
* Get the directory size
* @param string $directory
* @return int
*/
public function getDirSize($directory) : int
{
$size = 0;
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file) {
$size += $file->getSize();
}
return $size;
}
private function formatBytes($bytes, $precision = 2)
{
$units = [
trans('media::media.file-sizes.B'),
trans('media::media.file-sizes.KB'),
trans('media::media.file-sizes.MB'),
trans('media::media.file-sizes.GB'),
trans('media::media.file-sizes.TB'),
];
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
$bytes /= pow(1024, $pow);
return round($bytes, $precision) . ' ' . $units[$pow];
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment