cakephp-upload icon indicating copy to clipboard operation
cakephp-upload copied to clipboard

add base64 upload

Open mandricmihai opened this issue 6 years ago • 26 comments

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.

mandricmihai avatar Aug 06 '18 18:08 mandricmihai

Needs tests before I'll review, sorry.

josegonzalez avatar Aug 06 '18 18:08 josegonzalez

@josegonzalez I added unit tests

mandricmihai avatar Aug 07 '18 19:08 mandricmihai

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

mandricmihai avatar Aug 08 '18 10:08 mandricmihai

@josegonzalez could you review, please?

mandricmihai avatar Aug 08 '18 18:08 mandricmihai

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.

davidyell avatar Aug 10 '18 11:08 davidyell

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

mandricmihai avatar Aug 10 '18 14:08 mandricmihai

Could be useful. Can we get documentation for this?

josegonzalez avatar Aug 10 '18 21:08 josegonzalez

@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

mandricmihai avatar Aug 11 '18 05:08 mandricmihai

After some googling

Filesize:

strlen(base64_decode($data));

Filetype:

$f = finfo_open();
$mime_type = finfo_buffer($f, $data, FILEINFO_MIME_TYPE);

jorisvaesen avatar Aug 11 '18 09:08 jorisvaesen

Those seem reasonable to me @jorisvaesen.

josegonzalez avatar Aug 12 '18 01:08 josegonzalez

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

mandricmihai avatar Aug 12 '18 08:08 mandricmihai

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

josegonzalez avatar Aug 13 '18 00:08 josegonzalez

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?

mandricmihai avatar Aug 13 '18 05:08 mandricmihai

@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.

josegonzalez avatar Aug 13 '18 14:08 josegonzalez

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, ...)

jorisvaesen avatar Aug 13 '18 16:08 jorisvaesen

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

mandricmihai avatar Aug 13 '18 17:08 mandricmihai

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?

ADmad avatar Aug 15 '18 16:08 ADmad

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.

mandricmihai avatar Aug 15 '18 16:08 mandricmihai

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 avatar Aug 15 '18 16:08 ADmad

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?

ADmad avatar Aug 15 '18 16:08 ADmad

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

mandricmihai avatar Aug 15 '18 16:08 mandricmihai

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.

ADmad avatar Aug 15 '18 16:08 ADmad

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

mandricmihai avatar Aug 15 '18 17:08 mandricmihai

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

ADmad avatar Aug 15 '18 17:08 ADmad

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

mandricmihai avatar Aug 22 '18 18:08 mandricmihai

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

boyteenphonui avatar Aug 30 '18 15:08 boyteenphonui