-
Notifications
You must be signed in to change notification settings - Fork 140
/
UploadedFile.php
184 lines (152 loc) · 4.56 KB
/
UploadedFile.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
namespace Rakit\Validation\Rules;
use Rakit\Validation\Helper;
use Rakit\Validation\MimeTypeGuesser;
use Rakit\Validation\Rule;
use Rakit\Validation\Rules\Interfaces\BeforeValidate;
class UploadedFile extends Rule implements BeforeValidate
{
use Traits\FileTrait, Traits\SizeTrait;
/** @var string */
protected $message = "The :attribute is not valid uploaded file";
/** @var string|int */
protected $maxSize = null;
/** @var string|int */
protected $minSize = null;
/** @var array */
protected $allowedTypes = [];
/**
* Given $params and assign $this->params
*
* @param array $params
* @return self
*/
public function fillParameters(array $params): Rule
{
$this->minSize(array_shift($params));
$this->maxSize(array_shift($params));
$this->fileTypes($params);
return $this;
}
/**
* Given $size and set the max size
*
* @param string|int $size
* @return self
*/
public function maxSize($size): Rule
{
$this->params['max_size'] = $size;
return $this;
}
/**
* Given $size and set the min size
*
* @param string|int $size
* @return self
*/
public function minSize($size): Rule
{
$this->params['min_size'] = $size;
return $this;
}
/**
* Given $min and $max then set the range size
*
* @param string|int $min
* @param string|int $max
* @return self
*/
public function sizeBetween($min, $max): Rule
{
$this->minSize($min);
$this->maxSize($max);
return $this;
}
/**
* Given $types and assign $this->params
*
* @param mixed $types
* @return self
*/
public function fileTypes($types): Rule
{
if (is_string($types)) {
$types = explode('|', $types);
}
$this->params['allowed_types'] = $types;
return $this;
}
/**
* {@inheritDoc}
*/
public function beforeValidate()
{
$attribute = $this->getAttribute();
// We only resolve uploaded file value
// from complex attribute such as 'files.photo', 'images.*', 'images.foo.bar', etc.
if (!$attribute->isUsingDotNotation()) {
return;
}
$keys = explode(".", $attribute->getKey());
$firstKey = array_shift($keys);
$firstKeyValue = $this->validation->getValue($firstKey);
$resolvedValue = $this->resolveUploadedFileValue($firstKeyValue);
// Return original value if $value can't be resolved as uploaded file value
if (!$resolvedValue) {
return;
}
$this->validation->setValue($firstKey, $resolvedValue);
}
/**
* Check the $value is valid
*
* @param mixed $value
* @return bool
*/
public function check($value): bool
{
$minSize = $this->parameter('min_size');
$maxSize = $this->parameter('max_size');
$allowedTypes = $this->parameter('allowed_types');
if ($allowedTypes) {
$or = $this->validation ? $this->validation->getTranslation('or') : 'or';
$this->setParameterText('allowed_types', Helper::join(Helper::wraps($allowedTypes, "'"), ', ', ", {$or} "));
}
// below is Required rule job
if (!$this->isValueFromUploadedFiles($value) or $value['error'] == UPLOAD_ERR_NO_FILE) {
return true;
}
if (!$this->isUploadedFile($value)) {
return false;
}
// just make sure there is no error
if ($value['error']) {
return false;
}
if ($minSize) {
$bytesMinSize = $this->getBytesSize($minSize);
if ($value['size'] < $bytesMinSize) {
$this->setMessage('The :attribute file is too small, minimum size is :min_size');
return false;
}
}
if ($maxSize) {
$bytesMaxSize = $this->getBytesSize($maxSize);
if ($value['size'] > $bytesMaxSize) {
$this->setMessage('The :attribute file is too large, maximum size is :max_size');
return false;
}
}
if (!empty($allowedTypes)) {
$guesser = new MimeTypeGuesser;
$ext = $guesser->getExtension($value['type']);
unset($guesser);
if (!in_array($ext, $allowedTypes)) {
$this->setMessage('The :attribute file type must be :allowed_types');
return false;
}
}
return true;
}
}