Skip to content
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

Compatible with content_type_whitelist? #40

Closed
ehannes opened this issue Oct 28, 2016 · 16 comments
Closed

Compatible with content_type_whitelist? #40

ehannes opened this issue Oct 28, 2016 · 16 comments
Assignees
Milestone

Comments

@ehannes
Copy link

ehannes commented Oct 28, 2016

Using this gem, can I use carrierwave's content_type_whitelist somehow?

@ehannes ehannes changed the title Compatible with content_type_whitelist and such? Compatible with content_type_whitelist? Oct 28, 2016
@y9v
Copy link
Owner

y9v commented Nov 1, 2016

actually this should work already, if you defined content_type_whitelist in your uploader

@ehannes
Copy link
Author

ehannes commented Nov 2, 2016

I tried it, but I didn't get it to work. I declared it as following in my uploader:

def content_type_whitelist
  %w(pdf/application)
end

and then uploading a pdf file, but it was rejected due to incorrect content type. Then I thought it had something to do with the base64 conversion.

@jflewis
Copy link

jflewis commented Dec 14, 2016

I'm having the same issue. Using this in my Uploader.

def content_type_whitelist
    /image\//
end

Is there any status on this?

@y9v
Copy link
Owner

y9v commented Dec 15, 2016

Planning to work on this in this weekend.

@y9v y9v self-assigned this Feb 3, 2017
@y9v y9v added this to the v2.5.0 milestone Feb 3, 2017
@y9v y9v added the help wanted label Feb 3, 2017
@y9v y9v modified the milestones: v2.6.0, v3.0.0 May 22, 2017
@bobishh
Copy link

bobishh commented Jun 6, 2017

Issue won't reproduce with current version of gem (2.5.3 at the moment) and carrierwave v1.1.0, tested with rails 5.1 and 4.2.8

@y9v
Copy link
Owner

y9v commented Jun 6, 2017

@bobishh Dis you set the content_type_whitelist to an array of extensions? I think it did not work for @ehannes and @jflewis because they tried to use MIME types for the whitelist

@bobishh
Copy link

bobishh commented Jun 6, 2017

I've tried both ways - regex and array (except not with pdf but with image/png and image/jpeg), and it worked in all cases ¯_(ツ)_/¯

@y9v
Copy link
Owner

y9v commented Jun 7, 2017

@bobishh okey, thank you, I'm closing this issue then

@y9v y9v closed this as completed Jun 7, 2017
@ehannes
Copy link
Author

ehannes commented Jul 12, 2017

Great! If I have the time to check I will let you know if I can solve this issue by upgrading the gems.

@ehannes
Copy link
Author

ehannes commented Aug 24, 2017

@bobishh @lebedev-yury I still fail to get this to work. I guess I'm doing some stupid mistake... I had a look at the readme for the ContentTypeWhitelist module in CarrierWave and if I understand it correctly, I would be able to write something like this:

def content_type_whitelist
  %w(application/pdf image/jpg image/jpeg image/png)
end

If I add the above code and then try to attach a pdf file to the attachment, it fail with the following error: en.errors.messages.content_type_whitelist_error. If I remove the content_type_whitelist method from my uploader, I can once again attach pdf files.

@ehannes
Copy link
Author

ehannes commented Aug 24, 2017

I have solved the mystery. I tried in the console and it worked. Then I thought maybe something is wrong with my tests, and I were correct. Before I added the file with Rack::Test::UploadedFile.new(file_path). If I tried a simple File.open(file_path) it worked. Rack::Test::UploadedFile is for attaching files to a request (http://www.rubydoc.info/gems/rack-test/0.5.5/Rack/Test/UploadedFile) and I was attaching a file to a model. Sorry for the trouble! I hope this can help others with the same problem.

@ehannes
Copy link
Author

ehannes commented Aug 24, 2017

Sorry to bother you again but now when I tried a real upload, it still doesn't work, (though my tests are now working). I guess that means that the error is in the frontend. I'll have to continue my research... I'll be back if I get it to work. If someone wants to look at it, this is my parameters sent to the controller:

Parameters: {"profile"=>{..., "attachment"=>"data:application/pdf;base64,JVBERi0xLjQKJc...

This is the error I get:

en.errors.messages.content_type_whitelist_error

This is my content_type_whitelist:

def content_type_whitelist
  %w(application/pdf image/jpeg image/png)
end

@y9v
Copy link
Owner

y9v commented Aug 25, 2017

@ehannes the parameters look fine. I will take a look at the issue, if I will find time on this weekend

@goelinsights
Copy link

goelinsights commented Mar 22, 2018

@ehannes did you figure out what was going on or how to figure out the file name the whitelist is checking against?

Have a similar issue where I'm getting the error:

You are not allowed to upload "" files, allowed types: image/jpg, image/jpeg, image/png",

so it's stripping out the mime type somewhere and leaving it blank - would love some troubleshooting help if you figured out how to access the file name for a base64 uploaded file.

My JSON file being submitted:
{id: 2057, image: "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAASABIAAD…du8xO+QR825cZHToMAVmNPNM52KFBHTPHHuaPNmiTWh//2Q==", image_filename: "ABCD_1.jpg"}

Wondering if this is impacted by changing my photo.rb file to use image_filename to populate the filename

Photo.rb
mount_base64_uploader :image, ImageUploader, file_name: -> (u) { u.image_filename }

@y9v y9v reopened this Mar 22, 2018
@ehannes
Copy link
Author

ehannes commented Apr 9, 2018

@goelinsights No, actually I have not fixed this yet...

@JamesAndresCM
Copy link

alternative to validate content_type :
Add in gemfile :
gem 'cocaine', '~> 0.5.8'
gem 'file_validators'

Add in model :
validates :img, file_size: { in: 100.bytes..1.megabyte }, file_content_type: { allow: ['image/jpeg', 'image/png', 'image/jpg'], mode: :strict }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants