Skip to content

Commit

Permalink
Detect mime type in flysystem image loader
Browse files Browse the repository at this point in the history
  • Loading branch information
ojhaujjwal committed Jul 10, 2014
1 parent 627e7cd commit 8238cae
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
17 changes: 16 additions & 1 deletion src/Imagine/Loader/FlysystemLoader.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?php
namespace HtImgModule\Imagine\Loader;

use HtImgModule\Binary\Binary;
use HtImgModule\Exception;
use League\Flysystem\FilesystemInterface;
use League\Flysystem\FileNotFoundException;

/**
* Image Loader for Library, flysystem
Expand Down Expand Up @@ -29,6 +32,18 @@ public function __construct(FilesystemInterface $filesystem)
*/
public function load($path)
{
return $this->filesystem->read($path);
try {
$contents = $this->filesystem->read($path);
} catch (FileNotFoundException $e) {
throw new Exception\ImageNotFoundException(sprintf('Source image not found in "%s"', $path));
}

$mimeType = $this->filesystem->getMimeType($path);
if ($mimeType === false) {
// Mime Type could not be detected
return $contents;
}

return new Binary($contents, $mimeType);
}
}
41 changes: 39 additions & 2 deletions tests/HtImgModuleTest/Imagine/Loader/FlysystemLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,53 @@
namespace HtImgModuleTest\Imagine\Loader;

use HtImgModule\Imagine\Loader\FlysystemLoader;
use League\Flysystem\FileNotFoundException;

class FlysystemLoaderTest extends \PHPUnit_Framework_TestCase
{
public function testLoad()
public function testGetExceptionWhenImageFileCouldNotBeFound()
{
$filesystem = $this->getMock('League\Flysystem\FilesystemInterface');
$filesystem->expects($this->exactly(1))
->method('read')
->with('my/special/image')
->will($this->throwException(new FileNotFoundException('my/special/image')));
$loader = new FlysystemLoader($filesystem);

$this->setExpectedException('HtImgModule\Exception\ImageNotFoundException');
$loader->load('my/special/image');
}

public function testLoadBinary()
{
$filesystem = $this->getMock('League\Flysystem\FilesystemInterface');
$filesystem->expects($this->exactly(1))
->method('read')
->with('my/special/image')
->will($this->returnValue('some/image/content'));
$filesystem->expects($this->exactly(1))
->method('getMimeType')
->with('my/special/image')
->will($this->returnValue('image/png'));
$loader = new FlysystemLoader($filesystem);
$binary = $loader->load('my/special/image');
$this->assertInstanceOf('HtImgModule\Binary\Binary', $binary);
$this->assertEquals('some/image/content', $binary->getContent());
$this->assertEquals('image/png', $binary->getMimeType());
}

public function testLoadOnlyContentsWhenMimeTypeCouldNotDetected()
{
$filesystem = $this->getMock('League\Flysystem\FilesystemInterface');
$filesystem->expects($this->exactly(1))
->method('read')
->with('my/special/image')
->will($this->returnValue('some/image/content'));
$filesystem->expects($this->exactly(1))
->method('getMimeType')
->with('my/special/image')
->will($this->returnValue(false));
$loader = new FlysystemLoader($filesystem);
$this->assertEquals('some/image/content', $loader->load('asdfasdf'));
$this->assertEquals('some/image/content', $loader->load('my/special/image'));
}
}

0 comments on commit 8238cae

Please sign in to comment.