diff --git a/src/Components/DataPath.php b/src/Components/DataPath.php index 0ad74d975..6e6020ca2 100644 --- a/src/Components/DataPath.php +++ b/src/Components/DataPath.php @@ -44,6 +44,17 @@ class DataPath extends Path implements DataPathInterface 'data' => '', ]; + protected static $topLevelMimeTypes = [ + 'application' => 1, + 'audio' => 1, + 'example' => 1, + 'image' => 1, + 'message' => 1, + 'model' => 1, + 'multipart' => 1, + 'text' => 1, + 'video' => 1, + ]; /** * @inheritdoc @@ -207,9 +218,10 @@ public static function createFromPath($path) throw new RuntimeException(sprintf('The specified file `%s` is not readable', $path)); } - $data = file_get_contents($path); - $res = (new \finfo(FILEINFO_MIME))->file($path); - return new static($res.';'.static::BINARY_PARAMETER.','.base64_encode($data)); + $res = str_replace(' ', '', (new \finfo(FILEINFO_MIME))->file($path)); + $path = $res.';'.static::BINARY_PARAMETER.','.base64_encode(file_get_contents($path)); + + return new static($path); } /** @@ -265,7 +277,7 @@ protected function validate($path) $this->filterData($matches['data'], $extracts['isBinaryData']); return [ - 'mimetype' => $mimeType, + 'mimetype' => strtolower($mimeType), 'parameters' => explode(';', $extracts['parameters']), 'isBinaryData' => $extracts['isBinaryData'], 'data' => $matches['data'], @@ -281,7 +293,12 @@ protected function validate($path) */ protected function filterMimeType($mimeType) { - if (!preg_match(',^[a-z-\/+]+$,i', $mimeType)) { + $res = explode('/', strtolower($mimeType), 2); + $type = array_shift($res); + $subtype = array_shift($res); + if (!isset(static::$topLevelMimeTypes[$type]) + || !preg_match(',^[a-z+][a-z0-9\.]+$,i', $subtype) + ) { throw new InvalidArgumentException(sprintf('invalid mimeType, `%s`', $mimeType)); } }