Skip to content

Commit 663c0c2

Browse files
committed
Various Fixes
- Enable mime type wildcards - Fix too big/small error messages - Fix `UploadFromUrl::extendOptions`
1 parent 12924d8 commit 663c0c2

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

src/FileFromUrlValidator.php

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,40 @@ protected function validateValue($file)
8585
switch ($file->error) {
8686
case UPLOAD_ERR_OK:
8787
if ($this->maxSize !== null && $file->size > $this->maxSize) {
88-
return [$this->tooBig, ['file' => $file->name, 'limit' => $this->getSizeLimit()]];
88+
return [
89+
$this->tooBig,
90+
[
91+
'file' => $file->name,
92+
'limit' => $this->getSizeLimit(),
93+
'formattedLimit' => Yii::$app->formatter->asShortSize($this->getSizeLimit()),
94+
]
95+
];
8996
} elseif ($this->minSize !== null && $file->size < $this->minSize) {
90-
return [$this->tooSmall, ['file' => $file->name, 'limit' => $this->minSize]];
97+
return [
98+
$this->tooSmall,
99+
[
100+
'file' => $value->name,
101+
'limit' => $this->minSize,
102+
'formattedLimit' => Yii::$app->formatter->asShortSize($this->minSize),
103+
],
104+
];
91105
} elseif (!empty($this->extensions) && !$this->validateExtension($file)) {
92106
return [$this->wrongExtension, ['file' => $file->name, 'extensions' => implode(', ', $this->extensions)]];
93-
} elseif (!empty($this->mimeTypes) && !in_array($file->type, $this->mimeTypes, false)) {
107+
} elseif (!empty($this->mimeTypes) && !$this->validateMimeType($file)) {
94108
return [$this->wrongMimeType, ['file' => $file->name, 'mimeTypes' => implode(', ', $this->mimeTypes)]];
95109
}
96-
110+
97111
return null;
98112
case UPLOAD_ERR_INI_SIZE:
99113
case UPLOAD_ERR_FORM_SIZE:
100-
return [$this->tooBig, ['file' => $file->name, 'limit' => $this->getSizeLimit()]];
114+
return [
115+
$this->tooBig,
116+
[
117+
'file' => $file->name,
118+
'limit' => $this->getSizeLimit(),
119+
'formattedLimit' => Yii::$app->formatter->asShortSize($this->getSizeLimit()),
120+
]
121+
];
101122
case UPLOAD_ERR_PARTIAL:
102123
Yii::warning('File was only partially uploaded: ' . $file->name, __METHOD__);
103124
break;
@@ -136,4 +157,22 @@ protected function validateExtension($file)
136157
}
137158
return true;
138159
}
160+
161+
private function buildMimeTypeRegexp($mask)
162+
{
163+
return '/^' . str_replace('\*', '.*', preg_quote($mask, '/')) . '$/';
164+
}
165+
166+
protected function validateMimeType($file)
167+
{
168+
foreach ($this->mimeTypes as $mimeType) {
169+
if ($mimeType === $file->type) {
170+
return true;
171+
}
172+
if (strpos($mimeType, '*') !== false && preg_match($this->buildMimeTypeRegexp($mimeType), $file->type)) {
173+
return true;
174+
}
175+
}
176+
return false;
177+
}
139178
}

src/UploadFromUrl.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,11 @@ protected static function extendOptions(array $options)
131131
$options['error'] = UPLOAD_ERR_NO_FILE;
132132
}
133133

134-
$options['name'] = pathinfo($parsed_url['path'], PATHINFO_BASENAME);
135-
$options['baseName'] = pathinfo($parsed_url['path'], PATHINFO_FILENAME);
136-
$options['extension'] = mb_strtolower(pathinfo($parsed_url['path'], PATHINFO_EXTENSION));
134+
$options['name'] = isset($parsed_url['path']) ? pathinfo($parsed_url['path'], PATHINFO_BASENAME) : '';
135+
$options['baseName'] = isset($parsed_url['path']) ? pathinfo($parsed_url['path'], PATHINFO_FILENAME) : '';
136+
$options['extension'] = isset($parsed_url['path'])
137+
? mb_strtolower(pathinfo($parsed_url['path'], PATHINFO_EXTENSION))
138+
: '';
137139
$options['size'] = isset($headers['Content-Length']) ? $headers['Content-Length'] : 0;
138140
$options['type'] = isset($headers['Content-Type'])
139141
? $headers['Content-Type']

0 commit comments

Comments
 (0)