Skip to content

Commit

Permalink
Resolve issue #21
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Sep 28, 2015
1 parent 2ab1478 commit 3e662d3
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/Components/DataPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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'],
Expand All @@ -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));
}
}
Expand Down

0 comments on commit 3e662d3

Please sign in to comment.