1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Slince\Upload\Constraint; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
6
|
|
|
|
7
|
|
|
class SizeConstraint implements ConstraintInterface |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var string |
11
|
|
|
*/ |
12
|
|
|
protected $errorMessageTemplate = 'File size should between {minSize} and {maxSize}'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Maximum file size |
16
|
|
|
* @var int |
17
|
|
|
*/ |
18
|
|
|
protected $maxSize; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Minimum file size |
22
|
|
|
* @var int |
23
|
|
|
*/ |
24
|
|
|
protected $minSize; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Limit file size (use "B", "K", M", or "G") |
28
|
|
|
* @param int|string|null $minSize |
29
|
|
|
* @param int|string|null $maxSize |
30
|
|
|
*/ |
31
|
|
|
public function __construct($minSize = null, $maxSize = null) |
32
|
|
|
{ |
33
|
|
|
if ($minSize !== null) { |
34
|
|
|
$this->minSize = static::humanReadableToBytes($minSize); |
35
|
|
|
} |
36
|
|
|
if ($maxSize !== null) { |
37
|
|
|
$this->maxSize = static::humanReadableToBytes($maxSize); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
public function validate(UploadedFile $file): bool |
45
|
|
|
{ |
46
|
|
|
$size = method_exists($file, 'getClientSize') ? |
47
|
|
|
$file->getClientSize() : $file->getSize(); |
|
|
|
|
48
|
|
|
|
49
|
|
|
return ($this->maxSize === null || $size <= $this->maxSize) |
50
|
|
|
&& ($this->minSize === null || $size >= $this->minSize); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
public function getErrorMessage(UploadedFile $file): string |
57
|
|
|
{ |
58
|
|
|
return str_replace(['{minSize}', '{maxSize}'], [$this->minSize, $this->maxSize], $this->errorMessageTemplate); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
|
public function setErrorMessage(string $messageTemplate): void |
65
|
|
|
{ |
66
|
|
|
$this->errorMessageTemplate = $messageTemplate; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Convert human-readable file size (e.g. "10K" or "3M") into bytes |
71
|
|
|
* @link https://github.com/brandonsavage/Upload/blob/master/src/Upload/File.php#L446 |
72
|
|
|
* @param string $input |
73
|
|
|
* @return int |
74
|
|
|
*/ |
75
|
|
|
public static function humanReadableToBytes(string $input): int |
76
|
|
|
{ |
77
|
|
|
$number = (int)$input; |
78
|
|
|
$units = [ |
79
|
|
|
'b' => 1, |
80
|
|
|
'k' => 1024, |
81
|
|
|
'm' => 1048576, |
82
|
|
|
'g' => 1073741824 |
83
|
|
|
]; |
84
|
|
|
$unit = strtolower(substr($input, -1)); |
85
|
|
|
if (isset($units[$unit])) { |
86
|
|
|
$number *= $units[$unit]; |
87
|
|
|
} |
88
|
|
|
return $number; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.