I've tried everything now and I can't seem to figure out how to add content type validations in CarrierWave before performing actual processing on files I am uploading. The reason for this is that I want to allow only images but a user can upload a spoofed content file and rename file.pdf
file to file.jpg
. Steps I have tried so far:
photo_uploader.rb
def content_type_whitelist
/image\//
end
I have tried validate_integrity
in my uploader but no luck as well.
I have also tried overwriting CarrierWave error messages (which seems to me strange to me actually):
en:
errors:
messages:
content_type_whitelist: "You are not allowed to upload %{content_type} file"
but I am getting an error from MiniMagic
"Failed to manipulate with MiniMagick, maybe it is not an image? Original Error: %{e}"
The thing is that I want to display rails
validations instead so that when the content type is not one of those I have defined in my model it displays the message like "File should be one of image/jpeg, image/png, image/gif"
. So I need a way to force rails validations to trigger before image processing, although I think it is not quite possible.
Here they say that CarrierWave::Uploader::MagicMimeWhitelist
should be included in order to perform mime types validations so I've tried that but got uninitialzied constant CarrierWave::Uploader::MagicMimeWhitelist
(server restarted before)
include CarrierWave::Uploader::MagicMimeWhitelist
def whitelist_mime_type_pattern
/image\//
end
I've tried using carrier-mimetype-fu but after including CarrierWave::MimetypeFu
I got unitilzied constant CarrierWave::MimetypeFu
Do you have any experience with it?