Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
Platform
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Platform
Commits
71e1f9a8
Unverified
Commit
71e1f9a8
authored
Sep 10, 2017
by
Nicolas Widart
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Using new max folder size validation rule class
parent
fd593a4f
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
130 additions
and
30 deletions
+130
-30
UploadMediaRequest.php
Modules/Media/Http/Requests/UploadMediaRequest.php
+2
-30
MaxFolderSizeRuleTest.php
Modules/Media/Tests/MaxFolderSizeRuleTest.php
+55
-0
MaxFolderSizeRule.php
Modules/Media/Validators/MaxFolderSizeRule.php
+73
-0
No files found.
Modules/Media/Http/Requests/UploadMediaRequest.php
View file @
71e1f9a8
...
...
@@ -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
];
}
}
Modules/Media/Tests/MaxFolderSizeRuleTest.php
0 → 100644
View file @
71e1f9a8
<?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
;
}
}
Modules/Media/Validators/MaxFolderSizeRule.php
0 → 100644
View file @
71e1f9a8
<?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
];
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment