-
Notifications
You must be signed in to change notification settings - Fork 255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add base64 upload #495
base: master
Are you sure you want to change the base?
add base64 upload #495
Changes from 3 commits
c910542
bf8499d
6c83c5c
a0573c0
af00b22
7777cb5
0919441
4fafa7a
8a6e439
889215f
5468717
8ceb373
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: Alex | ||
* Date: 8/5/2018 | ||
* Time: 10:19 PM | ||
*/ | ||
|
||
namespace Josegonzalez\Upload\File\Path; | ||
|
||
|
||
use Cake\Utility\Hash; | ||
use Cake\Utility\Text; | ||
|
||
class Base64Processor extends DefaultProcessor | ||
{ | ||
/** | ||
* Returns the filename for the current field/data combination. | ||
* If a `nameCallback` is specified in settings, then that callable | ||
* will be invoked with the current upload data. | ||
* | ||
* @return string | ||
*/ | ||
public function filename() | ||
{ | ||
$processor = Hash::get($this->settings, 'nameCallback', null); | ||
$extension = Hash::get($this->settings, 'base64_extension', '.png'); | ||
if (is_callable($processor)) { | ||
$numberOfParameters = (new \ReflectionFunction($processor))->getNumberOfParameters(); | ||
if ($numberOfParameters == 2) { | ||
return $processor($this->data, $this->settings); | ||
} | ||
|
||
return $processor($this->table, $this->entity, $this->data, $this->field, $this->settings); | ||
} | ||
|
||
return Text::uuid() . "$extension"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: Alex | ||
* Date: 8/6/2018 | ||
* Time: 7:30 PM | ||
*/ | ||
|
||
namespace Josegonzalez\Upload\File\Transformer; | ||
|
||
|
||
class Base64Transformer extends DefaultTransformer | ||
{ | ||
|
||
/** | ||
* Creates a set of files from the initial data and returns them as key/value | ||
* pairs, where the path on disk maps to name which each file should have. | ||
* Example: | ||
* | ||
* [ | ||
* '/tmp/path/to/file/on/disk' => 'file.pdf', | ||
* '/tmp/path/to/file/on/disk-2' => 'file-preview.png', | ||
* ] | ||
* | ||
* @return array key/value pairs of temp files mapping to their names | ||
*/ | ||
public function transform() | ||
{ | ||
$decoded = base64_decode($this->data['data']); | ||
$tmp = tempnam(sys_get_temp_dir(), 'upload'); | ||
file_put_contents($tmp,$decoded); | ||
return [ | ||
$tmp => $this->data['name'], | ||
]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,7 +87,8 @@ public function beforeSave(Event $event, Entity $entity, ArrayObject $options) | |
continue; | ||
} | ||
|
||
if (Hash::get((array)$entity->get($field), 'error') !== UPLOAD_ERR_OK) { | ||
$uploadValidator = $this->getUploadValidator($entity, $settings, $field); | ||
if ($uploadValidator->hasUploadFailed()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice |
||
if (Hash::get($settings, 'restoreValueOnFailure', true)) { | ||
$entity->set($field, $entity->getOriginal($field)); | ||
$entity->setDirty($field, false); | ||
|
@@ -99,7 +100,14 @@ public function beforeSave(Event $event, Entity $entity, ArrayObject $options) | |
$path = $this->getPathProcessor($entity, $data, $field, $settings); | ||
$basepath = $path->basepath(); | ||
$filename = $path->filename(); | ||
$data['name'] = $filename; | ||
if (is_string($data)) { | ||
$temp = []; | ||
$temp['name'] = $filename; | ||
$temp['data'] = $data; | ||
$data = $temp; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the purpose of this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When upload base64 $data is just a string f.ex There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also because of the https://github.com/FriendsOfCake/cakephp-upload/pull/495/files#diff-84c66ea1e7013949dabde1157612da57R122 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you show how this would be used in practice? It's not clear whats going on here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You mean with the 2 extra interfaces? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Something like this.Maybe with some better names 😄 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe you could come with something better?I tried to think of a design pattern to apply, but I could only think of template or strategy but is pretty much like what we already have with the interfaces in place There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I meant in terms of an end user. How are you planning on using this/why should this go into the core? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm using https://foliotek.github.io/Croppie/ so that the user can select his profile picture and the result is in base64. |
||
} else { | ||
$data['name'] = $filename; | ||
} | ||
$files = $this->constructFiles($entity, $data, $field, $settings, $basepath); | ||
|
||
$writer = $this->getWriter($entity, $data, $field, $settings); | ||
|
@@ -111,8 +119,10 @@ public function beforeSave(Event $event, Entity $entity, ArrayObject $options) | |
|
||
$entity->set($field, $filename); | ||
$entity->set(Hash::get($settings, 'fields.dir', 'dir'), $basepath); | ||
$entity->set(Hash::get($settings, 'fields.size', 'size'), $data['size']); | ||
$entity->set(Hash::get($settings, 'fields.type', 'type'), $data['type']); | ||
if (!isset($temp)) { | ||
$entity->set(Hash::get($settings, 'fields.size', 'size'), $data['size']); | ||
$entity->set(Hash::get($settings, 'fields.type', 'type'), $data['type']); | ||
} | ||
} | ||
} | ||
|
||
|
@@ -205,6 +215,27 @@ public function getWriter(Entity $entity, $data, $field, $settings) | |
)); | ||
} | ||
|
||
/** | ||
* Retrieves an instance of a validator that validates that the current upload has succeded | ||
* | ||
* @param \Cake\ORM\Entity $entity an entity | ||
* @param array $data the data being submitted for a save | ||
* @return \Josegonzalez\Upload\File\Path\AbstractProcessor | ||
*/ | ||
public function getUploadValidator(Entity $entity, $settings , $field) | ||
{ | ||
$default = 'Josegonzalez\Upload\UploadValidator\DefaultUploadValidator'; | ||
$uploadValidatorClass = Hash::get($settings, 'uploadValidator', $default); | ||
if (is_a($uploadValidatorClass, 'Josegonzalez\Upload\UploadValidator\UploadValidatorInterface', true)) { | ||
return new $uploadValidatorClass($entity, $field); | ||
} | ||
|
||
throw new UnexpectedValueException(sprintf( | ||
"'uploadValidator' not set to instance of UploadValidatorInterface: %s", | ||
$uploadValidatorClass | ||
)); | ||
} | ||
|
||
/** | ||
* Creates a set of files from the initial data and returns them as key/value | ||
* pairs, where the path on disk maps to name which each file should have. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
namespace Josegonzalez\Upload\UploadValidator; | ||
|
||
use Josegonzalez\Upload\UploadValidator\DefaultUploadValidator; | ||
|
||
class Base64UploadValidator extends DefaultUploadValidator | ||
{ | ||
|
||
/** | ||
* Check's data for any upload errors. | ||
* pairs, where the path on disk maps to name which each file should have. | ||
* | ||
* @return boolean `true` if upload failed | ||
*/ | ||
public function hasUploadFailed() | ||
{ | ||
return !base64_decode($this->entity->get($this->field), true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How can one do other validations like for e.g checking for particular mimetype / file type? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah I now see the comments regarding adding methods/interfaces for checking size and mime type. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think is possible to do check on mimetype / file type without having the file already written on the disk There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't it be possible having validation methods using the example code provided in this comment #495 (comment) ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just tried and it works. I wasn't aware of such method to determine the mimetype. |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
namespace Josegonzalez\Upload\UploadValidator; | ||
|
||
use Cake\ORM\Entity; | ||
use Cake\Utility\Hash; | ||
use Josegonzalez\Upload\UploadValidator\UploadValidatorInterface; | ||
|
||
class DefaultUploadValidator implements UploadValidatorInterface | ||
{ | ||
/** | ||
* Entity instance. | ||
* | ||
* @var \Cake\ORM\Entity | ||
*/ | ||
protected $entity; | ||
|
||
/** | ||
* Name of field | ||
* | ||
* @var string | ||
*/ | ||
protected $field; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param \Cake\ORM\Entity $entity the entity to construct a path for. | ||
* @param string $field the field for which data will be saved | ||
*/ | ||
public function __construct(Entity $entity, $field) | ||
{ | ||
$this->entity = $entity; | ||
$this->field = $field; | ||
} | ||
|
||
/** | ||
* Check's data for any upload errors. | ||
* pairs, where the path on disk maps to name which each file should have. | ||
* | ||
* @return boolean `true` if upload failed | ||
*/ | ||
public function hasUploadFailed() | ||
{ | ||
return Hash::get((array)$this->entity->get($this->field), 'error') !== UPLOAD_ERR_OK; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
namespace Josegonzalez\Upload\UploadValidator; | ||
|
||
use Cake\ORM\Entity; | ||
|
||
interface UploadValidatorInterface | ||
{ | ||
/** | ||
* Constructor. | ||
* | ||
* @param \Cake\ORM\Entity $entity the entity to construct a path for. | ||
* @param string $field the field for which data will be saved | ||
*/ | ||
public function __construct(Entity $entity, $field); | ||
|
||
/** | ||
* Check's data for any upload errors. | ||
* pairs, where the path on disk maps to name which each file should have. | ||
* | ||
* @return boolean `true` if upload failed | ||
*/ | ||
public function hasUploadFailed(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These docblocks shouldn't be here.