FriendsOfCake / cakephp-upload

CakePHP: Handle file uploading sans ridiculous automagic
https://cakephp-upload.readthedocs.io/
MIT License
551 stars 255 forks source link

add base64 upload #495

Open mandricmihai opened 6 years ago

mandricmihai commented 6 years ago

Settings to use it

 'pathProcessor' => Base64Processor::class,
 'base64_extension' => '.jpg',
 'transformer' => Base64Transformer::class,
 'uploadValidator' => Base64UploadValidator::class,

@josegonzalez suggestions on how I could get rid of is_string() and isset() On base64 we just get a long string of data with no filename, no type, no size. Type and size column will also not be set.

josegonzalez commented 6 years ago

Needs tests before I'll review, sorry.

mandricmihai commented 6 years ago

@josegonzalez I added unit tests

mandricmihai commented 6 years ago

Looks like some code style fails. I will fix it later today.

mandricmihai commented 6 years ago

@josegonzalez could you review, please?

davidyell commented 6 years ago

I'm not quite sure why this functionality needs to be added to the core? Who will want to use this?

To me this is an edge-case and could be implemented with custom classes in userland code base.

mandricmihai commented 6 years ago

I think you are right.In this case, we can close the pull.

josegonzalez commented 6 years ago

Could be useful. Can we get documentation for this?

mandricmihai commented 6 years ago

@josegonzalez before getting the doc for this, we should decide what to do with the type and the size, right now they are not supported and if we are ok with https://github.com/FriendsOfCake/cakephp-upload/pull/495/files#diff-84c66ea1e7013949dabde1157612da57R103

jorisvaesen commented 6 years ago

After some googling

Filesize:

strlen(base64_decode($data));

Filetype:

$f = finfo_open();
$mime_type = finfo_buffer($f, $data, FILEINFO_MIME_TYPE);
josegonzalez commented 6 years ago

Those seem reasonable to me @jorisvaesen.

mandricmihai commented 6 years ago

The problem is not how to determine the file size and type, is where do we put that code that does this?

josegonzalez commented 6 years ago

We could have a FileProcessor or something that represents how to get that data for each type of file upload?

mandricmihai commented 6 years ago

That will solve one problem. How about this if https://github.com/FriendsOfCake/cakephp-upload/pull/495/files#diff-84c66ea1e7013949dabde1157612da57R103 Are you ok with having this if or is better to make another interface?

josegonzalez commented 6 years ago

@mma can you add actual usage docs for this so I can see how we plan on presenting it's use for users? That'll give me a better idea of what the internals should look like.

jorisvaesen commented 6 years ago

Adding a FileProcessor interface and a FileProcessor option to the config seems to me the right thing to do. Just make sure that this config option allows a callback function so an user can return the right processor based on the upload (base64, php file upload, ...)

mandricmihai commented 6 years ago

@josegonzalez Sure, I will add the docs this week.

ADmad commented 6 years ago

The problem is not how to determine the file size and type, is where do we put that code that does this?

It should be in validation methods shouldn't it?

mandricmihai commented 6 years ago

I don't think it should be in validation.We want to use the methods to get the actual size and file type https://github.com/FriendsOfCake/cakephp-upload/pull/495/files#diff-84c66ea1e7013949dabde1157612da57R123 so that we can set on the entity.

ADmad commented 6 years ago

Just FYI, how I provide cropping feature is use js code to just generate the cropping dimensions and submit it in a separate field without touching the actual upload file. Then use the cropping data to crop on server side.

Only drawback of this way is the full image is uploaded but in return it saves all the extra code for handing base64 encoded data and more importantly upload validation.

ADmad commented 6 years ago

I don't think it should be in validation.

If not in validation then how can I prevent someone from submitting a non image base64 encoded data?

mandricmihai commented 6 years ago

We can provided a method in ImageValidationTrait.php that uses https://secure.php.net/manual/en/function.imagecreatefromstring.php

ADmad commented 6 years ago

Unless cake's validator can be used to validate the mime type of base64 encoded data before any other processing begins, I am a big "NO" on adding this feature to the plugin.

mandricmihai commented 6 years ago

When you say cake's validators you talk about the validators already provided by cake or the custom validators?

ADmad commented 6 years ago

Custom validation methods which will be used by Cake's Validator :slightly_smiling_face:

mandricmihai commented 6 years ago

@ADmad could you take a look over the validator when you have some time?

boyteenphonui commented 6 years ago

It is very useful, I also really need it. I am very supportive of this.