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

Commit 0427346

Browse files
author
Florian Krämer
committed
Merge branch '3.0' of git://github.com/burzum/cakephp-file-storage into 3.0
2 parents a07758a + 05b9772 commit 0427346

File tree

4 files changed

+81
-33
lines changed

4 files changed

+81
-33
lines changed

docs/Tutorials/Quick-Start.md

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -146,18 +146,20 @@ namespace App\Model\Table;
146146
use Burzum\FileStorage\Model\Table\ImageStorageTable;
147147

148148
class ProductImagesTable extends ImageStorageTable {
149-
public function uploadImage($productId, $data) {
150-
$data['adapter'] = 'Local';
151-
$data['model'] = 'ProductImage',
152-
$data['foreign_key'] = $productId;
153-
$entity = $this->newEntity($data);
149+
public function uploadImage($productId, $entity) {
150+
$entity = $this->patchEntity($entity, [
151+
'adapter' => 'Local',
152+
'model' => 'ProductImage',
153+
'foreign_key' => $productId
154+
]);
154155
return $this->save($entity);
155156
}
156-
public function uploadDocument($productId, $data) {
157-
$data['adapter'] = 'Local';
158-
$data['model'] = 'ProductDocument',
159-
$data['foreign_key'] = $productId;
160-
$entity = $this->newEntity($data);
157+
public function uploadDocument($productId, $entity) {
158+
$entity = $this->patchEntity($entity, [
159+
'adapter' => 'Local',
160+
'model' => 'ProductDocument',
161+
'foreign_key' => $productId
162+
]);
161163
return $this->save($entity);
162164
}
163165
}
@@ -172,11 +174,17 @@ namespace App\Controller;
172174
class ProductsController extends AppController {
173175
// Upload an image
174176
public function upload($productId = null) {
175-
if (!$this->request->is('get')) {
176-
if ($this->Products->ProductImages->upload($productId, $this->request->data)) {
177+
$entity = $this->Products->ProductImages->newEntity();
178+
if ($this->request->is(['post', 'put])) {
179+
$entity = $this->Products->ProductImages->patchEntity(
180+
$entity,
181+
$this->request->data
182+
);
183+
if ($this->Products->ProductImages->upload($productId, $entity)) {
177184
$this->Flash->set(__('Upload successful!');
178185
}
179186
}
187+
$this->set('productImage', $entity);
180188
}
181189
}
182190
```

src/Model/Table/FileStorageTable.php

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace Burzum\FileStorage\Model\Table;
33

4+
use ArrayObject;
45
use Cake\Log\LogTrait;
56
use Cake\ORM\Table;
67
use Cake\ORM\Entity;
@@ -64,27 +65,39 @@ public function configureUploadValidation($options) {
6465
}
6566

6667
/**
67-
* beforeSave callback
68+
* beforeMarshal callback
6869
*
69-
* @param array $options
70-
* @return boolean true on success
70+
* @param Event $event
71+
* @param ArrayObject $data
72+
* @return void
7173
*/
72-
public function beforeSave(Event $event, Entity $entity, $options) {
73-
if (!empty($event->data['entity']['file']['tmp_name'])) {
74-
$File = new File($event->data['entity']['file']['tmp_name']);
75-
$event->data['entity']['filesize'] = $File->size();
76-
$event->data['entity']['mime_type'] = $File->mime();
74+
public function beforeMarshal(Event $event, ArrayObject $data) {
75+
if (!empty($data['file']['tmp_name'])) {
76+
$File = new File($data['file']['tmp_name']);
77+
$data['filesize'] = $File->size();
78+
$data['mime_type'] = $File->mime();
7779
}
78-
if (!empty($event->data['entity']['file']['name'])) {
79-
$event->data['entity']['extension'] = pathinfo($event->data['entity']['file']['name'], PATHINFO_EXTENSION);
80-
$event->data['entity']['filename'] = $event->data['entity']['file']['name'];
80+
if (!empty($data['file']['name'])) {
81+
$data['extension'] = pathinfo($data['file']['name'], PATHINFO_EXTENSION);
82+
$data['filename'] = $data['file']['name'];
8183
}
82-
if (empty($event->data['entity']['model'])) {
83-
$event->data['entity']['model'] = $this->table();
84+
if (empty($data['model'])) {
85+
$data['model'] = $this->table();
8486
}
85-
if (empty($event->data['entity']['adapter'])) {
86-
$event->data['entity']['adapter'] = 'Local';
87+
if (empty($data['adapter'])) {
88+
$data['adapter'] = 'Local';
8789
}
90+
}
91+
92+
/**
93+
* beforeSave callback
94+
*
95+
* @param Event $event
96+
* @param Entity $entity
97+
* @param array $options
98+
* @return bool true on success
99+
*/
100+
public function beforeSave(Event $event, Entity $entity, $options) {
88101
$Event = new Event('FileStorage.beforeSave', $this, array(
89102
'record' => $entity,
90103
'storage' => $this->getStorageAdapter($event->data['entity']['adapter'])

src/Validation/UploadValidator.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class UploadValidator extends Validator {
3434
*
3535
* @var string
3636
*/
37-
protected $_fileSize = 0;
37+
protected $_filesize = 0;
3838

3939
/**
4040
* Upload error message.
@@ -85,12 +85,14 @@ public function isUploadArray($value) {
8585
* Validates the filesize.
8686
*
8787
* @param array $value.
88-
* @param array $extensions.
89-
* @return boolean
88+
* @param int $size.
89+
* @param array $context.
90+
* @param string $operator.
91+
* @return bool
9092
*/
91-
public function fileSize($value, $size, $operator = '>') {
92-
$this->_fileSize = $value['size'];
93-
return $this->_validateSize($value['size'], $operator, $size);
93+
public function fileSize($value, $size, $context = null, $operator = '<') {
94+
$this->_filesize = $value['size'];
95+
return Validation::fileSize($value, $operator, $size);
9496
}
9597

9698
/**

tests/TestCase/Model/Table/FileStorageTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,29 @@ public function testAfterDelete() {
9292
$result = $this->FileStorage->afterDelete($event, $entity, []);
9393
$this->assertTrue($result);
9494
}
95+
96+
/**
97+
* testBeforeMarshal
98+
*
99+
* @return void
100+
*/
101+
public function testBeforeMarshal() {
102+
$filename = \Cake\Core\Plugin::path('Burzum/FileStorage') . DS . 'tests' . DS . 'Fixture' . DS . 'File' . DS . 'titus.jpg';
103+
$event = new Event('Model.beforeMarshal', $this->FileStorage);
104+
105+
$data = new \ArrayObject([
106+
'file' => [
107+
'name' => 'titus.jpg',
108+
'tmp_name' => $filename
109+
]
110+
]);
111+
112+
$this->FileStorage->beforeMarshal($event, $data);
113+
114+
$this->assertEquals(332643, $data['filesize']);
115+
$this->assertEquals('Local', $data['adapter']);
116+
$this->assertEquals('image/jpeg', $data['mime_type']);
117+
$this->assertEquals('jpg', $data['extension']);
118+
$this->assertEquals('file_storage', $data['model']);
119+
}
95120
}

0 commit comments

Comments
 (0)