diff --git a/docs/Tutorials/Quick-Start.md b/docs/Tutorials/Quick-Start.md index 83ee9067..225af050 100644 --- a/docs/Tutorials/Quick-Start.md +++ b/docs/Tutorials/Quick-Start.md @@ -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); } } @@ -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); } } ``` diff --git a/src/Model/Table/FileStorageTable.php b/src/Model/Table/FileStorageTable.php index c49b2d6e..396298c0 100644 --- a/src/Model/Table/FileStorageTable.php +++ b/src/Model/Table/FileStorageTable.php @@ -1,6 +1,7 @@ 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']) diff --git a/src/Validation/UploadValidator.php b/src/Validation/UploadValidator.php index f611189a..11251ecd 100644 --- a/src/Validation/UploadValidator.php +++ b/src/Validation/UploadValidator.php @@ -34,7 +34,7 @@ class UploadValidator extends Validator { * * @var string */ - protected $_fileSize = 0; + protected $_filesize = 0; /** * Upload error message. @@ -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); } /** diff --git a/tests/TestCase/Model/Table/FileStorageTest.php b/tests/TestCase/Model/Table/FileStorageTest.php index 9b56f67c..a631130e 100644 --- a/tests/TestCase/Model/Table/FileStorageTest.php +++ b/tests/TestCase/Model/Table/FileStorageTest.php @@ -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']); + } }