Skip to content

Commit

Permalink
Merge pull request #7 from mirko-pagliai/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
mirko-pagliai authored Oct 24, 2016
2 parents 3950128 + 4a2e093 commit c14870a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
25 changes: 18 additions & 7 deletions src/Utility/ThumbCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,17 +316,26 @@ public function save(array $options = [])
}

//Checks for formats supported by GD driver
if (Configure::read('Thumbs.driver') === 'gd' && !in_array($options['format'], ['gif', 'jpg', 'png'])) {
if (Configure::read('Thumbs.driver') === 'gd' &&
!in_array($options['format'], ['gif', 'jpg', 'png'])
) {
throw new InternalErrorException(
__d('thumber', 'The {0} driver can\'t decode the `{1}` format', 'GD', $options['format'])
);
}

$this->arguments[] = Configure::read('Thumbs.driver');

$imageInstance = (new ImageManager([
'driver' => Configure::read('Thumbs.driver'),
]))->make($this->path);
//Tries to create the image instance
try {
$imageInstance = (new ImageManager([
'driver' => Configure::read('Thumbs.driver'),
]))->make($this->path);
} catch (\Intervention\Image\Exception\NotReadableException $e) {
throw new InternalErrorException(
__d('thumber', 'Unable to read image from file `{0}`', str_replace(APP, null, $this->path))
);
}

//Calls each callback
foreach ($this->callbacks as $callback) {
Expand All @@ -336,12 +345,14 @@ public function save(array $options = [])
$content = $imageInstance->encode($options['format'], $options['quality']);
$imageInstance->destroy();

//@codingStandardsIgnoreLine
if (!@file_put_contents($target, $content)) {
if (!is_writable(dirname($target))) {
throw new InternalErrorException(
__d('thumber', 'Can\'t write the file `{0}`', str_replace(APP, null, $target))
__d('thumber', 'The directory `{0}` is not writeable', str_replace(APP, null, dirname($target)))
);
}

//Writes
file_put_contents($target, $content);
}

//Resets arguments and callbacks
Expand Down
14 changes: 13 additions & 1 deletion tests/TestCase/Utility/ThumbCreatorSaveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ public function tearDown()
}
}

/**
* Test for `save()` method, using an invalid file as input
* @expectedException Cake\Network\Exception\InternalErrorException
* @expectedExceptionMessage File `config/bootstrap.php` not readable
* @ŧest
*/
public function testSaveFromInvalidFile()
{
(new ThumbCreator(APP . 'config' . DS . 'bootstrap.php'))
->resize(200)->save(['format' => 'jpg']);
}

/**
* Test for `save()` method. It tests the thumbnails is created only if it
* does not exist
Expand Down Expand Up @@ -134,7 +146,7 @@ public function testSaveInvalidTargetFormat()
* Test for `save()` method, using the `target` option with a no existing
* directory target
* @expectedException Cake\Network\Exception\InternalErrorException
* @expectedExceptionMessage Can't write the file `/tmp/noExistingDir/thumb.jpg`
* @expectedExceptionMessage The directory `/tmp/noExistingDir` is not writeable
* @test
*/
public function testSaveInvalidTargetDir()
Expand Down

0 comments on commit c14870a

Please sign in to comment.