Skip to content

Commit

Permalink
Merge pull request #785 from robfrawley/bugfix-filesystem-loader
Browse files Browse the repository at this point in the history
Cleanup FileSystemLoader (Followup to #775)
  • Loading branch information
lsmith77 authored Sep 6, 2016
2 parents 73901c1 + f5f9689 commit 0cd8678
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
8 changes: 2 additions & 6 deletions Binary/Loader/FileSystemLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(
$this->mimeTypeGuesser = $mimeTypeGuesser;
$this->extensionGuesser = $extensionGuesser;

if (!($realRootPath = realpath($rootPath))) {
if (empty($rootPath) || !($realRootPath = realpath($rootPath))) {
throw new InvalidArgumentException(sprintf('Root image path not resolvable "%s"', $rootPath));
}

Expand All @@ -50,18 +50,14 @@ public function __construct(
*/
public function find($path)
{
if (!($absolutePath = realpath($this->rootPath.DIRECTORY_SEPARATOR.ltrim($path, DIRECTORY_SEPARATOR)))) {
if (!($absolutePath = realpath($this->rootPath.DIRECTORY_SEPARATOR.$path))) {
throw new NotLoadableException(sprintf('Source image not resolvable "%s"', $path));
}

if (0 !== strpos($absolutePath, $this->rootPath)) {
throw new NotLoadableException(sprintf('Source image invalid "%s" as it is outside of the defined root path', $absolutePath));
}

if (false == file_exists($absolutePath)) {
throw new NotLoadableException(sprintf('Source image not found in "%s"', $absolutePath));
}

$mimeType = $this->mimeTypeGuesser->guess($absolutePath);

return new FileBinary(
Expand Down
14 changes: 14 additions & 0 deletions Tests/Binary/Loader/FileSystemLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ public function testCouldBeConstructedWithExpectedArguments()
);
}

public function testThrowExceptionIfRootPathIsEmpty()
{
$this->setExpectedException(
'Liip\ImagineBundle\Exception\InvalidArgumentException',
'Root image path not resolvable'
);

new FileSystemLoader(
MimeTypeGuesser::getInstance(),
ExtensionGuesser::getInstance(),
''
);
}

public function testThrowExceptionIfRootPathDoesNotExist()
{
$this->setExpectedException(
Expand Down

0 comments on commit 0cd8678

Please sign in to comment.