Skip to content

Commit

Permalink
Merge branch '1.0-uploadable' of https://github.com/leica69/cakephp-u…
Browse files Browse the repository at this point in the history
…tils into leica69-1.0-uploadable

Conflicts:
	tests/TestCase/Model/Behavior/UploadableBehaviorTest.php
  • Loading branch information
bobmulder committed Jun 2, 2015
2 parents 4b5e570 + 4affa3d commit 97bc3aa
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
15 changes: 12 additions & 3 deletions docs/behaviors/uploadable.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ The `fields` configuration contains an array with avaliable columns you want to
- `directory` - this is the column who will contain the directory to your file.
- `type` - this column will contain the type of the uplaoded file.
- `size` - this column contains the size of the uploaded file.
- `fileName` - this column contains the name of the file.
- `filePath` - this column contains the path to the file, including the file name.

Note that all of the fields will be default set to false, so not used. Only the `directory` field will be automatically set to the name of your given field.
So in the previous example it would be set to `avatar`.
Expand All @@ -58,14 +60,21 @@ Example:
$this->addBehavior('Utils.Uploadable', [
'avatar' => [
'fields' => [
'size' => 'avatar_size',
'directory' => 'avatar_directory',
'type' => 'avatar_type',
'directory' => 'avatar_directory'
'size' => 'avatar_size',
'fileName' => 'avatar_name',
'filePath' => 'avatar_path'
]
],
]);

In this case the size of the file will be stored in the column `avatar_size`, the type will be stored in the column `avatar_type` and the directory will be stored in the `avatar_directory` column.
In this case:
- the directory will be stored in the `avatar_directory` column,
- the type will be stored in the column `avatar_type`,
- the size of the file will be stored in the column `avatar_size`,
- the fileName will be stored in the column `avatar_name`,
- the filePath (including the file name) will be stored in the column `avatar_path`,

### Remove File On Update

Expand Down
25 changes: 17 additions & 8 deletions src/Model/Behavior/UploadableBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class UploadableBehavior extends Behavior
'directory' => false,
'type' => false,
'size' => false,
'fileName' => false,
'filePath' => false,
],
'removeFileOnUpdate' => false,
'removeFileOnDelete' => true,
Expand Down Expand Up @@ -85,7 +87,6 @@ public function beforeSave($event, $entity, $options)
public function afterSave($event, $entity, $options)
{
$fields = $this->getFieldList();

foreach ($fields as $field => $data) {
if ($this->_ifUploaded($entity, $field)) {
if ($this->_uploadFile($entity, $field)) {
Expand Down Expand Up @@ -238,11 +239,11 @@ protected function _ifUploaded($entity, $field)
protected function _uploadFile($entity, $field, $options = [])
{
$_upload = $entity->get($field);
$uploadPath = $this->_getPath($entity, $field, ['file' => false]);
$uploadPath = $this->_getPath($entity, $field, ['file' => true]);

// creating the path if not exists
if (!is_dir($this->_getDir($entity, $field, ['file' => false]))) {
$this->_mkdir($this->_getDir($entity, $field, ['file' => false]), 0777, true);
if (!file_exists($this->_getPath($entity, $field, ['file' => false]))) {
$this->_mkdir($this->_getPath($entity, $field, ['file' => false]), 0777, true);
}

// upload the file and return true
Expand Down Expand Up @@ -283,6 +284,12 @@ protected function _setUploadColumns($entity, $field, $options = [])
if ($key == "size") {
$entity->set($column, $_upload['size']);
}
if ($key == "fileName") {
$entity->set($column, $this->_getFileName($entity, $field, $options = []));
}
if ($key == "filePath") {
$entity->set($column, $this->_getDir($entity, $field, ['root' => false, 'file' => false]));
}
}
}

Expand All @@ -302,7 +309,7 @@ protected function _setUploadColumns($entity, $field, $options = [])
protected function _getDir($entity, $field, $options = [])
{
$_options = [
'root' => true,
'root' => false,
'file' => false,
];

Expand All @@ -325,6 +332,10 @@ protected function _getDir($entity, $field, $options = [])

$builtPath = str_replace(array_keys($replacements), array_values($replacements), $path);

if (!$options['root']) {
$builtPath = str_replace(ROOT . DS . 'webroot' . DS, '', $builtPath);
}

return $builtPath;
}

Expand Down Expand Up @@ -357,8 +368,6 @@ protected function _getPath($entity, $field, $options = [])
'{field}' => $entity->get($config['field']),
'{model}' => Inflector::underscore($this->_Table->alias()),
'{DS}' => DIRECTORY_SEPARATOR,
'//' => DIRECTORY_SEPARATOR,
'/' => DIRECTORY_SEPARATOR,
'\\' => DIRECTORY_SEPARATOR,
];

Expand Down
2 changes: 2 additions & 0 deletions tests/Fixture/ArticlesFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class ArticlesFixture extends TestFixture
'file_path' => 'text',
'file_size' => 'text',
'file_type' => 'text',
'file_dir' => 'text',
'file_name' => 'text',
'published' => [
'type' => 'string',
'length' => 1,
Expand Down
5 changes: 5 additions & 0 deletions tests/TestCase/Model/Behavior/UploadableBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ public function testSaveWithFile()
'file' => [
'fields' => [
'directory' => 'file_path',
'fileName' => 'file_name',
'filePath' => 'file_dir',
'type' => 'file_type',
'size' => 'file_size',
],
Expand Down Expand Up @@ -274,6 +276,9 @@ public function testSaveWithFile()

$get = $table->get(3);


$this->assertContains('uploads' . DS . 'articles' . DS . '3' . DS . 'cakemanager.png', $get->get('file_path'));
$this->assertContains('uploads' . DS . 'articles' . DS . '3' . DS, $get->get('file_dir'));
$this->assertContains('uploads' . DS . 'articles' . DS . '3' . DS . 'cakemanager.png', $get->get('file_path'));
$this->assertEquals("image/png", $get->get('file_type'));
$this->assertEquals(11501, $get->get('file_size'));
Expand Down

0 comments on commit 97bc3aa

Please sign in to comment.