Skip to content

Commit

Permalink
Merge 83c9177 into e9ca228
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardobazico committed May 20, 2016
2 parents e9ca228 + 83c9177 commit 06cfb63
Show file tree
Hide file tree
Showing 7 changed files with 360 additions and 33 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
api/
vendor/
composer.lock
build/
*.DS_Store
7 changes: 3 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- 7.0

sudo: false

Expand All @@ -20,10 +19,10 @@ matrix:
fast_finish: true

include:
- php: 5.5
- php: 5.6
env: PHPCS=1 DEFAULT=0

- php: 5.5
- php: 5.6
env: COVERALLS=1 DEFAULT=0 DB=mysql db_dsn='mysql://travis@0.0.0.0/cakephp_test'

install:
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
}
],
"require": {
"php": ">=5.4",
"php": ">=5.6",
"cakephp/cakephp": "~3.0"
},
"require-dev": {
"phpunit/phpunit": "*"
"phpunit/phpunit": "*",
"nesbot/carbon": ">=1.13.0"
},
"autoload": {
"psr-4": {
Expand Down
15 changes: 14 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@
</listener>
</listeners>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./</directory>
<directory suffix=".ctp">./</directory>
<exclude>
<directory suffix=".php">./vendor/</directory>
<directory suffix=".ctp">./vendor/</directory>

<directory suffix=".php">./tests/</directory>
<directory suffix=".ctp">./tests/</directory>
</exclude>
</whitelist>
</filter>

<!-- Prevent coverage reports from looking in tests and vendors -->
<filter>
<blacklist>
Expand All @@ -39,5 +53,4 @@
<directory suffix=".ctp">./tests/</directory>
</blacklist>
</filter>

</phpunit>
4 changes: 2 additions & 2 deletions src/Controller/AppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
*/
namespace Utils\Controller;

use App\Controller\AppController as BaseController;
use Cake\Controller\Controller;

/**
* AppController Class for Utils Plugin
*
*/
class AppController extends BaseController
class AppController extends Controller
{

}
82 changes: 71 additions & 11 deletions src/Model/Behavior/UploadableBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ class UploadableBehavior extends Behavior
'removeFileOnUpdate' => true,
'removeFileOnDelete' => true,
'field' => 'id',
'path' => '{ROOT}{DS}{WEBROOT}{DS}uploads{DS}{model}{DS}{field}{DS}',
'path' => '{ROOT}{WEBROOT}{DS}uploads{DS}{model}{DS}{field}{DS}',
'fileName' => '{ORIGINAL}',
'entityReplacements' => [ ],
'accept_type' => null /* can be 'image'*/
]
];

Expand Down Expand Up @@ -109,6 +111,7 @@ public function beforeSave($event, $entity, $options)
{
$uploads = [];
$fields = $this->getFieldList();

foreach ($fields as $field => $data) {
if (!is_string($entity->get($field))) {
$uploads[$field] = $entity->get($field);
Expand All @@ -122,7 +125,7 @@ public function beforeSave($event, $entity, $options)
$fieldConfig = $this->config($field);

if ($fieldConfig['removeFileOnUpdate']) {
$this->_removeFile($entity->getOriginal($field));
$this->_removeFile($entity, $field);
}
}
}
Expand Down Expand Up @@ -152,9 +155,11 @@ public function afterSave($event, $entity, $options)
}
}
}

foreach ($storedToSave as $toSave) {
$event->subject()->save($toSave);
}

$this->_savedFields = [];
}

Expand All @@ -172,7 +177,7 @@ public function beforeDelete($event, $entity, $options)
foreach ($fields as $field => $data) {
$fieldConfig = $this->config($field);
if ($fieldConfig['removeFileOnDelete']) {
$this->_removeFile($entity->get($field));
$this->_removeFile($entity, $field);
}
}
}
Expand Down Expand Up @@ -229,7 +234,6 @@ protected function _ifUploaded($entity, $field)
{
if (array_key_exists($field, $this->_uploads)) {
$data = $this->_uploads[$field];

if (!empty($data['tmp_name'])) {
return true;
}
Expand All @@ -250,6 +254,13 @@ protected function _ifUploaded($entity, $field)
protected function _uploadFile($entity, $field, $options = [])
{
$_upload = $this->_uploads[$field];

$fieldConfig = $this->config($field);

if ($fieldConfig['accept_type'] === 'image' && !$this->_isImage($_upload)) {
return false;
}

$uploadPath = $this->_getPath($entity, $field, ['file' => true]);

// creating the path if not exists
Expand All @@ -258,10 +269,7 @@ protected function _uploadFile($entity, $field, $options = [])
}

// upload the file and return true
if ($this->_moveUploadedFile($_upload['tmp_name'], $uploadPath)) {
return true;
}
return false;
return $this->_moveUploadedFile($_upload['tmp_name'], $uploadPath) ? true : false;
}

/**
Expand Down Expand Up @@ -394,6 +402,7 @@ protected function _getPath($entity, $field, $options = [])
'\\' => DIRECTORY_SEPARATOR,
];

$replacements = $this->_setEntityReplacements($entity, $field, $replacements);
$builtPath = str_replace(array_keys($replacements), array_values($replacements), $path);

if (!$options['root']) {
Expand All @@ -407,6 +416,21 @@ protected function _getPath($entity, $field, $options = [])
return $builtPath;
}

/**
* _setEntityReplacements method
* @param \Cake\ORM\Entity $entity Entity to check on.
* @param string $field Field to check on.
* @param array $replacements List of current replacements
* @return array
*/
protected function _setEntityReplacements($entity, $field, array $replacements)
{
foreach ($this->config($field . '.entityReplacements') as $key => $field) {
$replacements[$key] = $entity->get($field);
}
return $replacements;
}

/**
* _getUrl
*
Expand All @@ -419,7 +443,7 @@ protected function _getPath($entity, $field, $options = [])
protected function _getUrl($entity, $field)
{
$path = '/' . $this->_getPath($entity, $field, ['root' => false, 'file' => true]);
return str_replace(DS, '/', $path);
return str_replace(DS, '/', str_replace('//', '/', $path));
}

/**
Expand Down Expand Up @@ -458,6 +482,8 @@ protected function _getFileName($entity, $field, $options = [])
'\\' => DIRECTORY_SEPARATOR,
];

$replacements = $this->_setEntityReplacements($entity, $field, $replacements);

$builtFileName = str_replace(array_keys($replacements), array_values($replacements), $fileName);

return $builtFileName;
Expand Down Expand Up @@ -495,11 +521,19 @@ protected function _mkdir($pathname, $mode, $recursive)
/**
* _removeFile
*
* @param string $file Path of the file
* @param \Cake\ORM\Entity $entity Entity to check on.
* @param string $field Field to check on.
* @return bool
*/
protected function _removeFile($file)
protected function _removeFile($entity, $field)
{
$file = $entity->getOriginal($field);

if (is_null($file)) {
$fieldConfig = $this->config($field);
$file = $entity->getOriginal($fieldConfig['fields']['filePath']);
}

$_file = new File($file);

if ($_file->exists()) {
Expand All @@ -513,4 +547,30 @@ protected function _removeFile($file)
}
return false;
}

/**
* _isImage
*
* @param string $data array data of file
* @return bool
*/
protected function _isImage($data)
{
$fileMimeType = exif_imagetype($data['tmp_name']);
$filename = strtolower($data['name']);
$fileNameArray = explode('.', $filename);
$fileExt = array_pop($fileNameArray);

$allowedMimeTypes = [ IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG ];
$allowedExt = [ 'gif', 'jpg', 'png', 'jpeg' ];

/*
* if allowed mimetype and extention return true
*/
if (in_array($fileMimeType, $allowedMimeTypes) && in_array($fileExt, $allowedExt)) {
return true;
}

return false;
}
}
Loading

0 comments on commit 06cfb63

Please sign in to comment.