Skip to content

Commit

Permalink
Merge pull request #25 from mirko-pagliai/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
mirko-pagliai authored Dec 29, 2017
2 parents 715de79 + fbc9075 commit 6f3d09f
Show file tree
Hide file tree
Showing 17 changed files with 647 additions and 67 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ install:
- if [[ ! -z "$CAKEPHP_VERSION" ]]; then composer require --update-with-dependencies cakephp/cakephp:${CAKEPHP_VERSION}; fi

script:
- if [[ $PHPCS != '1' && $COVERAGE != '1' ]]; then vendor/bin/phpunit --exclude-group imageEquals; fi
- if [[ $PHPCS != '1' && $COVERAGE != '1' && $CAKEPHP_VERSION != '3.4.*' ]]; then vendor/bin/phpunit --exclude-group imageEquals; fi
- if [[ $PHPCS != '1' && $COVERAGE != '1' && $CAKEPHP_VERSION = '3.4.*' ]]; then vendor/bin/phpunit --testsuite cakephp3.4 --exclude-group imageEquals; fi
- if [[ $PHPCS = 1 ]]; then vendor/bin/phpcs -p --extensions=php,ctp,js,css --standard=vendor/cakephp/cakephp-codesniffer/CakePHP --ignore=*.min.css,*.min.js ./config ./src ./tests; fi
- if [[ $COVERAGE = 1 ]]; then vendor/bin/phpunit --exclude-group imageEquals --coverage-clover=clover.xml; fi

Expand Down
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
# 1.x branch
## 1.3 branch
# 1.3.0
* the name of the thumbnails (when automatically generated) now contains a
prefix with the checksum of the path of the original image and a suffix with
the checksum of all the arguments used to create the thumbnail. Using the
prefix of a thumbnail, it will be possible to identify the original image
from which it was generated;
* the `Last-Modified` header is set up and sent to the client. It indicates the
date and time at which the thumbnail file was modified for the last time;
* added `ThumbManager` and `ThumbShell` classes;
* `resolveFilePath()` method moved from `ThumbCreator` to `ThumbTrait`, because
this method can be used in different contexts.

## 1.2 branch
# 1.2.0
* updated for intervention/image 2.4;
Expand Down
5 changes: 5 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
<testsuite name="cakephp-thumber Test Cases">
<directory>./tests/TestCase</directory>
</testsuite>

<testsuite name="cakephp3.4">
<directory>./tests/TestCase</directory>
<exclude>./tests/TestCase/Shell/ThumberShellTest.php</exclude>
</testsuite>
</testsuites>

<!-- configure code coverage -->
Expand Down
8 changes: 7 additions & 1 deletion src/Controller/ThumbsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ public function thumb($basename)
throw new ThumbNotFoundException(__d('thumber', 'File `{0}` doesn\'t exist', $file));
}

return $this->response->withFile($file)->withType(mime_content_type($file));
$response = $this->response->withModified(filemtime($file));

if ($response->checkNotModified($this->request)) {
return $response;
}

return $response->withFile($file)->withType(mime_content_type($file));
}
}
51 changes: 38 additions & 13 deletions src/Locale/it/thumber.po
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# LANGUAGE translation of CakePHP Application
# Copyright YEAR NAME <EMAIL@ADDRESS>
# LANGUAGE translation of cakephp-thumber
# Copyright 2017 Mirko Pagliai <mirko.pagliai@gmail.com>
#
msgid ""
msgstr ""
"POT-Creation-Date: 2017-03-17 21:00+0100\n"
"PO-Revision-Date: 2017-03-17 21:01+0100\n"
"POT-Creation-Date: 2017-12-29 15:18+0100\n"
"PO-Revision-Date: 2017-12-29 15:27+0100\n"
"Last-Translator: Mirko Pagliai <mirko.pagliai@gmail.com>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
Expand All @@ -13,23 +13,48 @@ msgstr ""
"Language-Team: Italian <>\n"
"Language: it\n"

#: /home/mirko/Libs/Plugins/cakephp-thumber/src/Controller/ThumbsController.php:45
#: /home/mirko/Libs/Plugins/cakephp-thumber/src/ThumbTrait.php:120
msgid "File `{0}` not readable"
msgstr "File `{0}` non leggibile"

#: /home/mirko/Libs/Plugins/cakephp-thumber/src/Controller/ThumbsController.php:37
msgid "File `{0}` doesn't exist"
msgstr "Il file `{0}` non esiste"

#: /home/mirko/Libs/Plugins/cakephp-thumber/src/Utility/ThumbCreator.php:144
msgid "File `{0}` not readable"
msgstr "File `{0}` non leggibile"
#: /home/mirko/Libs/Plugins/cakephp-thumber/src/Shell/ThumberShell.php:41
msgid "Error deleting thumbnails"
msgstr "Errore cancellando le miniature"

#: /home/mirko/Libs/Plugins/cakephp-thumber/src/Utility/ThumbCreator.php:269
msgid "No valid method called before the `{0}` method"
msgstr "Nessun metodo valido chiamato prima del metodo `{0}`"
#: /home/mirko/Libs/Plugins/cakephp-thumber/src/Shell/ThumberShell.php:46
msgid "Thumbnails deleted: {0}"
msgstr "Miniature cancellate: {0}"

#: /home/mirko/Libs/Plugins/cakephp-thumber/src/Utility/ThumbCreator.php:299
#: /home/mirko/Libs/Plugins/cakephp-thumber/src/Shell/ThumberShell.php:80
msgid "Clears all thumbnails that have been generated from an image path"
msgstr ""
"Cancella tutte le miniature che sono state generate dalla path di un'immagine"

#: /home/mirko/Libs/Plugins/cakephp-thumber/src/Shell/ThumberShell.php:84
msgid "Path of the original image"
msgstr "Path dell'immagine originale"

#: /home/mirko/Libs/Plugins/cakephp-thumber/src/Shell/ThumberShell.php:90
msgid "Clears all thumbnails"
msgstr "Cancella tutte le miniature"

#: /home/mirko/Libs/Plugins/cakephp-thumber/src/Shell/ThumberShell.php:91
msgid "A shell to manage thumbnails"
msgstr "Una shell per gestire le miniature"

#: /home/mirko/Libs/Plugins/cakephp-thumber/src/Utility/ThumbCreator.php:107
msgid "Unable to read image from file `{0}`"
msgstr "Impossibile leggere l'immagine dal file `{0}`"

#: /home/mirko/Libs/Plugins/cakephp-thumber/src/Utility/ThumbCreator.php:313
#: /home/mirko/Libs/Plugins/cakephp-thumber/src/Utility/ThumbCreator.php:229
msgid "No valid method called before the `{0}` method"
msgstr "Nessun metodo valido chiamato prima del metodo `{0}`"

#: /home/mirko/Libs/Plugins/cakephp-thumber/src/Utility/ThumbCreator.php:260
msgid "The directory `{0}` is not writeable"
msgstr "La directory `{0}` non è scrivibile"

Expand Down
44 changes: 34 additions & 10 deletions src/Locale/thumber.pot
Original file line number Diff line number Diff line change
@@ -1,35 +1,59 @@
# LANGUAGE translation of CakePHP Application
# Copyright YEAR NAME <EMAIL@ADDRESS>
# LANGUAGE translation of cakephp-thumber
# Copyright 2017 Mirko Pagliai <mirko.pagliai@gmail.com>
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"POT-Creation-Date: 2017-03-17 21:00+0100\n"
"POT-Creation-Date: 2017-12-29 15:18+0100\n"
"PO-Revision-Date: YYYY-mm-DD HH:MM+ZZZZ\n"
"Last-Translator: NAME <EMAIL@ADDRESS>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n"

#: /home/mirko/Libs/Plugins/cakephp-thumber/src/Controller/ThumbsController.php:45
#: /home/mirko/Libs/Plugins/cakephp-thumber/src/ThumbTrait.php:120
msgid "File `{0}` not readable"
msgstr ""

#: /home/mirko/Libs/Plugins/cakephp-thumber/src/Controller/ThumbsController.php:37
msgid "File `{0}` doesn't exist"
msgstr ""

#: /home/mirko/Libs/Plugins/cakephp-thumber/src/Utility/ThumbCreator.php:144
msgid "File `{0}` not readable"
#: /home/mirko/Libs/Plugins/cakephp-thumber/src/Shell/ThumberShell.php:41
msgid "Error deleting thumbnails"
msgstr ""

#: /home/mirko/Libs/Plugins/cakephp-thumber/src/Utility/ThumbCreator.php:269
msgid "No valid method called before the `{0}` method"
#: /home/mirko/Libs/Plugins/cakephp-thumber/src/Shell/ThumberShell.php:46
msgid "Thumbnails deleted: {0}"
msgstr ""

#: /home/mirko/Libs/Plugins/cakephp-thumber/src/Shell/ThumberShell.php:80
msgid "Clears all thumbnails that have been generated from an image path"
msgstr ""

#: /home/mirko/Libs/Plugins/cakephp-thumber/src/Shell/ThumberShell.php:84
msgid "Path of the original image"
msgstr ""

#: /home/mirko/Libs/Plugins/cakephp-thumber/src/Shell/ThumberShell.php:90
msgid "Clears all thumbnails"
msgstr ""

#: /home/mirko/Libs/Plugins/cakephp-thumber/src/Shell/ThumberShell.php:91
msgid "A shell to manage thumbnails"
msgstr ""

#: /home/mirko/Libs/Plugins/cakephp-thumber/src/Utility/ThumbCreator.php:299
#: /home/mirko/Libs/Plugins/cakephp-thumber/src/Utility/ThumbCreator.php:107
msgid "Unable to read image from file `{0}`"
msgstr ""

#: /home/mirko/Libs/Plugins/cakephp-thumber/src/Utility/ThumbCreator.php:313
#: /home/mirko/Libs/Plugins/cakephp-thumber/src/Utility/ThumbCreator.php:229
msgid "No valid method called before the `{0}` method"
msgstr ""

#: /home/mirko/Libs/Plugins/cakephp-thumber/src/Utility/ThumbCreator.php:260
msgid "The directory `{0}` is not writeable"
msgstr ""

95 changes: 95 additions & 0 deletions src/Shell/ThumberShell.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
/**
* This file is part of cakephp-thumber.
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Mirko Pagliai
* @link https://github.com/mirko-pagliai/cakephp-thumber
* @license https://opensource.org/licenses/mit-license.php MIT License
* @since 1.3.0
*/
namespace Thumber\Shell;

use Cake\Console\Shell;
use Thumber\Utility\ThumbManager;

/**
* A shell to manage thumbnails
*/
class ThumberShell extends Shell
{
/**
* Internal method to clears thumbnails
* @param string $method Method to call, provided by the `ThumbManager` class
* @param array $args Arguments
* @return bool
* @uses Thumber\Utility\ThumbManager::clear()
* @uses Thumber\Utility\ThumbManager::clearAll()
*/
protected function _clear($method, $args = [])
{
try {
$count = call_user_func_array([new ThumbManager, $method], (array)$args);
} catch (\Exception $e) {
$count = false;
}

if ($count === false) {
$this->err(__d('thumber', 'Error deleting thumbnails'));

return false;
}

$this->verbose(__d('thumber', 'Thumbnails deleted: {0}', $count));

return true;
}

/**
* Clears all thumbnails that have been generated from an image path
* @param string $path Path of the original image
* @return bool
* @uses _clear()
*/
public function clear($path = null)
{
return $this->_clear(__FUNCTION__, $path);
}

/**
* Clears all thumbnails
* @return bool
* @uses _clear()
*/
public function clearAll()
{
return $this->_clear(__FUNCTION__);
}

/**
* Gets the option parser instance and configures it
* @return ConsoleOptionParser
*/
public function getOptionParser()
{
$parser = parent::getOptionParser();
$parser->addSubcommand('clear', [
'help' => __d('thumber', 'Clears all thumbnails that have been generated from an image path'),
'parser' => [
'arguments' => [
'path' => [
'help' => __d('thumber', 'Path of the original image'),
'required' => true,
],
],
],
]);
$parser->addSubcommand('clearAll', ['help' => __d('thumber', 'Clears all thumbnails')]);
$parser->setDescription(__d('thumber', 'A shell to manage thumbnails'));

return $parser;
}
}
4 changes: 2 additions & 2 deletions src/TestSuite/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ public function assertMime($filename, $mime, $message = '')
public function assertThumbPath($path, $message = '')
{
$regex = sprintf(
'/^%s[a-z0-9]{32}\.(%s)/',
'/^%s[a-z0-9]{32}_[a-z0-9]{32}\.(%s)/',
preg_quote($this->getPath() . DS, '/'),
implode('|', ['bmp', 'gif', 'jpg', 'ico', 'png', 'psd', 'tiff'])
implode('|', self::getSupportedFormats())
);
self::assertRegExp($regex, $path, $message);
}
Expand Down
38 changes: 38 additions & 0 deletions src/ThumbTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
namespace Thumber;

use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\Filesystem\Folder;
use Cake\Network\Exception\InternalErrorException;
use Cake\Routing\Router;

/**
Expand Down Expand Up @@ -84,4 +87,39 @@ protected function getUrl($path, $full = true)
{
return Router::url(['_name' => 'thumb', base64_encode(basename($path))], $full);
}

/**
* Internal method to resolve a partial path, returning its full path
* @param string $path Partial path
* @return string
* @throws InternalErrorException
*/
protected function resolveFilePath($path)
{
//Returns, if it's a remote file
if (isUrl($path)) {
return $path;
}

//If it a relative path, it can be a file from a plugin or a file
// relative to `APP/webroot/img/`
if (!Folder::isAbsolute($path)) {
$pluginSplit = pluginSplit($path);

//Note that using `pluginSplit()` is not sufficient, because
// `$path` may still contain a dot
if (!empty($pluginSplit[0]) && in_array($pluginSplit[0], Plugin::loaded())) {
$path = Plugin::path($pluginSplit[0]) . 'webroot' . DS . 'img' . DS . $pluginSplit[1];
} else {
$path = WWW_ROOT . 'img' . DS . $path;
}
}

//Checks if is readable
if (!is_readable($path)) {
throw new InternalErrorException(__d('thumber', 'File `{0}` not readable', rtr($path)));
}

return $path;
}
}
Loading

0 comments on commit 6f3d09f

Please sign in to comment.