Skip to content
This repository has been archived by the owner on Mar 5, 2022. It is now read-only.

Commit

Permalink
Merge branch '3.0' of git://github.com/burzum/cakephp-file-storage in…
Browse files Browse the repository at this point in the history
…to 3.0
  • Loading branch information
Florian Krämer committed Aug 17, 2015
2 parents a07758a + 05b9772 commit 0427346
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 33 deletions.
32 changes: 20 additions & 12 deletions docs/Tutorials/Quick-Start.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,20 @@ namespace App\Model\Table;
use Burzum\FileStorage\Model\Table\ImageStorageTable;

class ProductImagesTable extends ImageStorageTable {
public function uploadImage($productId, $data) {
$data['adapter'] = 'Local';
$data['model'] = 'ProductImage',
$data['foreign_key'] = $productId;
$entity = $this->newEntity($data);
public function uploadImage($productId, $entity) {
$entity = $this->patchEntity($entity, [
'adapter' => 'Local',
'model' => 'ProductImage',
'foreign_key' => $productId
]);
return $this->save($entity);
}
public function uploadDocument($productId, $data) {
$data['adapter'] = 'Local';
$data['model'] = 'ProductDocument',
$data['foreign_key'] = $productId;
$entity = $this->newEntity($data);
public function uploadDocument($productId, $entity) {
$entity = $this->patchEntity($entity, [
'adapter' => 'Local',
'model' => 'ProductDocument',
'foreign_key' => $productId
]);
return $this->save($entity);
}
}
Expand All @@ -172,11 +174,17 @@ namespace App\Controller;
class ProductsController extends AppController {
// Upload an image
public function upload($productId = null) {
if (!$this->request->is('get')) {
if ($this->Products->ProductImages->upload($productId, $this->request->data)) {
$entity = $this->Products->ProductImages->newEntity();
if ($this->request->is(['post', 'put])) {
$entity = $this->Products->ProductImages->patchEntity(
$entity,
$this->request->data
);
if ($this->Products->ProductImages->upload($productId, $entity)) {
$this->Flash->set(__('Upload successful!');
}
}
$this->set('productImage', $entity);
}
}
```
Expand Down
43 changes: 28 additions & 15 deletions src/Model/Table/FileStorageTable.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Burzum\FileStorage\Model\Table;

use ArrayObject;
use Cake\Log\LogTrait;
use Cake\ORM\Table;
use Cake\ORM\Entity;
Expand Down Expand Up @@ -64,27 +65,39 @@ public function configureUploadValidation($options) {
}

/**
* beforeSave callback
* beforeMarshal callback
*
* @param array $options
* @return boolean true on success
* @param Event $event
* @param ArrayObject $data
* @return void
*/
public function beforeSave(Event $event, Entity $entity, $options) {
if (!empty($event->data['entity']['file']['tmp_name'])) {
$File = new File($event->data['entity']['file']['tmp_name']);
$event->data['entity']['filesize'] = $File->size();
$event->data['entity']['mime_type'] = $File->mime();
public function beforeMarshal(Event $event, ArrayObject $data) {
if (!empty($data['file']['tmp_name'])) {
$File = new File($data['file']['tmp_name']);
$data['filesize'] = $File->size();
$data['mime_type'] = $File->mime();
}
if (!empty($event->data['entity']['file']['name'])) {
$event->data['entity']['extension'] = pathinfo($event->data['entity']['file']['name'], PATHINFO_EXTENSION);
$event->data['entity']['filename'] = $event->data['entity']['file']['name'];
if (!empty($data['file']['name'])) {
$data['extension'] = pathinfo($data['file']['name'], PATHINFO_EXTENSION);
$data['filename'] = $data['file']['name'];
}
if (empty($event->data['entity']['model'])) {
$event->data['entity']['model'] = $this->table();
if (empty($data['model'])) {
$data['model'] = $this->table();
}
if (empty($event->data['entity']['adapter'])) {
$event->data['entity']['adapter'] = 'Local';
if (empty($data['adapter'])) {
$data['adapter'] = 'Local';
}
}

/**
* beforeSave callback
*
* @param Event $event
* @param Entity $entity
* @param array $options
* @return bool true on success
*/
public function beforeSave(Event $event, Entity $entity, $options) {
$Event = new Event('FileStorage.beforeSave', $this, array(
'record' => $entity,
'storage' => $this->getStorageAdapter($event->data['entity']['adapter'])
Expand Down
14 changes: 8 additions & 6 deletions src/Validation/UploadValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class UploadValidator extends Validator {
*
* @var string
*/
protected $_fileSize = 0;
protected $_filesize = 0;

/**
* Upload error message.
Expand Down Expand Up @@ -85,12 +85,14 @@ public function isUploadArray($value) {
* Validates the filesize.
*
* @param array $value.
* @param array $extensions.
* @return boolean
* @param int $size.
* @param array $context.
* @param string $operator.
* @return bool
*/
public function fileSize($value, $size, $operator = '>') {
$this->_fileSize = $value['size'];
return $this->_validateSize($value['size'], $operator, $size);
public function fileSize($value, $size, $context = null, $operator = '<') {
$this->_filesize = $value['size'];
return Validation::fileSize($value, $operator, $size);
}

/**
Expand Down
25 changes: 25 additions & 0 deletions tests/TestCase/Model/Table/FileStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,29 @@ public function testAfterDelete() {
$result = $this->FileStorage->afterDelete($event, $entity, []);
$this->assertTrue($result);
}

/**
* testBeforeMarshal
*
* @return void
*/
public function testBeforeMarshal() {
$filename = \Cake\Core\Plugin::path('Burzum/FileStorage') . DS . 'tests' . DS . 'Fixture' . DS . 'File' . DS . 'titus.jpg';
$event = new Event('Model.beforeMarshal', $this->FileStorage);

$data = new \ArrayObject([
'file' => [
'name' => 'titus.jpg',
'tmp_name' => $filename
]
]);

$this->FileStorage->beforeMarshal($event, $data);

$this->assertEquals(332643, $data['filesize']);
$this->assertEquals('Local', $data['adapter']);
$this->assertEquals('image/jpeg', $data['mime_type']);
$this->assertEquals('jpg', $data['extension']);
$this->assertEquals('file_storage', $data['model']);
}
}

0 comments on commit 0427346

Please sign in to comment.