Rails 5, paperclip not validating the attachment? - ruby-on-rails

I am getting this response:
Command :: file -b --mime "C:/Users/Ben/AppData/Local/Temp/5523c88dd347d1b7cc617f632b7efdb720171021-12076-b6btes.jpg"
[paperclip] Content Type Spoof: Filename bg.jpg (image/jpeg from Headers, ["image/jpeg"] from Extension), content type discovered from file command: . See documentation to allow this combination.
(0.0ms) BEGIN
Command :: file -b --mime "C:/Users/Ben/AppData/Local/Temp/5523c88dd347d1b7cc617f632b7efdb720171021-12076-1fvpv4a.jpg"
[paperclip] Content Type Spoof: Filename bg.jpg (image/jpeg from Headers, ["image/jpeg"] from Extension), content type discovered from file command: . See documentation to allow this combination.
(0.6ms) ROLLBACK
On Windows
And I have this in my model:
has_attached_file :image
# Validate content type
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
# Explicitly do not validate
do_not_validate_attachment_file_type :image
And the request just fails, I dont understand why is it happening?

Try:
config/initializers/paperclip.rb:
Paperclip.options[:command_path] = '/usr/bin/' # Path to ImageMagick

Related

Can't save image from API with paperclip

I want to save an image from my react app in my rails app.
I post this to my api:
and when I try to save it i get this error:
{
"file":[
"has contents that are not what they are reported to be",
"file type is not acceptable"
],
"file_content_type":[
"file type is not acceptable"
]
}
my model looks like this:
class Photo < ApplicationRecord
validates :title, presence: true
belongs_to :user
has_attached_file :file, styles: { square: "350x233#", medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
validates_attachment_content_type :file, content_type: /\Aimage\/.*\z/, message: 'file type is not acceptable'
validates_attachment_size :file, less_than: 10.megabytes, message: 'file size is more than 50MB'
end
and my controller:
def create
uploaded_file = false
if params[:photo][:image]
tempfile = Tempfile.new(params[:photo][:filename])
tempfile.binmode
tempfile.write(Base64.decode64(params[:photo][:image]))
uploaded_file = ActionDispatch::Http::UploadedFile.new(
:tempfile => tempfile,
:filename => params[:photo][:filename],
:original_filename => params[:photo][:filename],
:content_type => Mime::Type.lookup_by_extension(File.extname(params[:photo][:filename])[1..-1]).to_s
)
params[:photo][:file] = uploaded_file
end
params[:photo][:title] = params[:photo][:filename] if params[:photo][:title] == ""
# #picture = Photo.new(photo_params)
#photo = current_user.photos.build photo_params
#photo.save
render json: #photo.errors
end
and in my logs i found this:
Unpermitted parameters: :image, :filename
Command :: file -b --mime '/tmp/homepage.jpg20180415-7-1wa7yr3'
[paperclip] Trying to link /tmp/homepage.jpg20180415-7-1wa7yr3 to /tmp/ba3988db0a3167093b1f74e8ae4a8e8320180415-7-jpgu5m.jpg
[paperclip] Trying to link /tmp/ba3988db0a3167093b1f74e8ae4a8e8320180415-7-jpgu5m.jpg to /tmp/ba3988db0a3167093b1f74e8ae4a8e8320180415-7-147ntu1.jpg
Command :: file -b --mime '/tmp/ba3988db0a3167093b1f74e8ae4a8e8320180415-7-147ntu1.jpg'
[paperclip] Content Type Spoof: Filename homepage.jpg (application/octet-stream from Headers, ["image/jpeg"] from Extension), content type discovered from file command: application/octet-stream. See documentation to allow this combination.
[1m[35m (1.5ms)[0m [1m[35mBEGIN[0m
[paperclip] Trying to link /tmp/ba3988db0a3167093b1f74e8ae4a8e8320180415-7-jpgu5m.jpg to /tmp/ba3988db0a3167093b1f74e8ae4a8e8320180415-7-ndp9hd.jpg
Command :: file -b --mime '/tmp/ba3988db0a3167093b1f74e8ae4a8e8320180415-7-ndp9hd.jpg'
[paperclip] Content Type Spoof: Filename homepage.jpg (application/octet-stream from Headers, ["image/jpeg"] from Extension), content type discovered from file command: application/octet-stream. See documentation to allow this combination.
I didn't get my mistake. Can anyone help me with that upload?
I think the content_type is missing, but I don't know where.
Have you tried using Paperclip's io_adapter to decode the base64 into the attachment object? Something along the lines of:
def create
file = Paperclip.io_adapters.for(params[:photo][:image])
file.original_filename = params[:photo][:filename]
#photo = Photo.new(file: file, user: current_user)
#photo.save
end

Paperclip failed to upload msg file type

I need to attach the msg file to rails, i have add the mime type for the msg filetype
validates_attachment :file, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif", "application/pdf","application/vnd.ms-excel",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"application/msword",
"application/vnd.ms-outlook",
"application/octet-stream",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"text/plain"] }
but when upload with the sample msg file it went error
Command :: file -b --mime '/tmp/5e8ff9bf55ba3508199d22e984129be620170527-6239-gqgnhz.msg'
Command :: file -b --mime '/tmp/5e8ff9bf55ba3508199d22e984129be620170527-6239-g903st.msg'
(0.2ms) ROLLBACK
Completed 500 Internal Server Error in 26ms (ActiveRecord: 1.7ms)
any suggestion? i am using paperclip 5.1.0
what is the rails validation error?
if object.save
...
else
logger.debug object.errors.inspect
end
Read the save error that cause rollback, from there you'll know the reason it were rejected.

Paperclip: Cannot attach XLS (But DOC works)

I cannot attach a XLS file, but DOC works:
Attachments file content type is invalid
Attachments file is invalid
Here is the log:
Parameters: ... "files"=>[#<ActionDispatch::Http::UploadedFile:0x0000000daf7730 #tempfile=#<Tempfile:C:/Users/Chloe/AppData/Local/Temp/RackMultipart20170511-47156-ym774u.xls>, #original_filename="Chocolate_Store1.xls", #content_type="application/vnd.ms-excel", #headers="Content-Disposition: form-data; name=\"deal[files][]\"; filename=\"Chocolate_Store1.xls\"\r\nContent-Type: application/vnd.ms-excel\r\n">],
...
Command :: file -b --mime "C:/Users/Chloe/AppData/Local/Temp/1f1fb4d71efc0bd245abd7b5b9aa661220170511-47156-1rqzd0q.xls"
(1.0ms) ROLLBACK
Rendering deals/edit.html.erb within layouts/application
When I test the type:
>file -b --mime "C:/Users/Chloe/AppData/Local/Temp/1f1fb4d71efc0bd245abd7b5b9aa661220170511-47156-1rqzd0q.xls"
application/vnd.ms-office; charset=binary
I have this type in my accept list:
class Attachment < ApplicationRecord
has_attached_file :file
validates_attachment_content_type :file, content_type: [
"application/pdf", "application/msword",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"application/vnd.oasis.opendocument.text",
"application/vnd.ms-excel",
"application/vnd.ms-office",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"application/vnd.oasis.opendocument.spreadsheet"
]
end
Rails 5.0.2, Paperclip 5.1.0, File 5.04 (DevKit)
I noticed DevKit file differs from Cygwin file. Not sure if relevant:
>file -b --mime "C:/Users/Chloe/AppData/Local/Temp/1f1fb4d71efc0bd245abd7b5b9aa661220170515-54920-1e2bk5s.xls"
application/vnd.ms-office; charset=binary
$ file -b --mime "C:/Users/Chloe/AppData/Local/Temp/1f1fb4d71efc0bd245abd7b5b9aa661220170515-54920-1e2bk5s.xls"
application/vnd.ms-excel; charset=binary
I switched to use
validates_attachment_file_name :file, matches: [/\.pdf$/, /\.docx?$/, /\.xlsx?$/, /\.odt$/, /\.ods$/]
...content_type: [...'application/x-ole-storage'...]
worked for me

Paperclip: The following errors were found: Image has contents that are not what they are reported to be

I am using paperclip with a rails app and always I get the following error:
I, [2015-06-06T20:10:25.310071 #37358] INFO -- : Command :: file -b --mime '/tmp/58e53d1324eef6265fdb97b08ed9aadf20150606-37358-ouvtzl.png'
I, [2015-06-06T20:10:25.317478 #37358] INFO -- : [paperclip] Content Type Spoof: Filename ruby.png (application/octet-stream from Headers, [#<MIME::Type:0x000000053624c8 #friendly={"en"=>"Portable Network Graphics (PNG)"}, #system=nil, #obsolete=false, #registered=true, #use_instead=nil, #signature=false, #content_type="image/png", #raw_media_type="image", #raw_sub_type="png", #simplified="image/png", #i18n_key="image.png", #media_type="image", #sub_type="png", #docs=[], #encoding="base64", #extensions=["png"], #references=["IANA", "[Glenn_Randers-Pehrson]", "{image/png=http://www.iana.org/assignments/media-types/image/png}"], #xrefs={"person"=>["Glenn_Randers-Pehrson"], "template"=>["image/png"]}>] from Extension), content type discovered from file command: image/png. See documentation to allow this combination.
I, [2015-06-06T20:10:25.349416 #37358] INFO -- : Command :: file -b --mime '/tmp/58e53d1324eef6265fdb97b08ed9aadf20150606-37358-1n574dp.png'
I, [2015-06-06T20:10:25.356667 #37358] INFO -- : [paperclip] Content Type Spoof: Filename ruby.png (application/octet-stream from Headers, [#<MIME::Type:0x000000053624c8 #friendly={"en"=>"Portable Network Graphics (PNG)"}, #system=nil, #obsolete=false, #registered=true, #use_instead=nil, #signature=false, #content_type="image/png", #raw_media_type="image", #raw_sub_type="png", #simplified="image/png", #i18n_key="image.png", #media_type="image", #sub_type="png", #docs=[], #encoding="base64", #extensions=["png"], #references=["IANA", "[Glenn_Randers-Pehrson]", "{image/png=http://www.iana.org/assignments/media-types/image/png}"], #xrefs={"person"=>["Glenn_Randers-Pehrson"], "template"=>["image/png"]}>] from Extension), content type discovered from file command: image/png. See documentation to allow this combination.
Mongoid::Errors::Validations (
Problem:
Validation of Mock failed.
Summary:
The following errors were found: Image has contents that are not what they are reported to be
Resolution:
Try persisting the document with valid data or remove the validations.):
app/api/mockaccino/api.rb:23:in `block (2 levels) in <class:API>'
Curl command for test:
curl -X POST -i -F image=#/home/user/workspace/ruby.png -F name=mock http://localhost:3000/api/mock
I am traying send an image and a parameter called "name" to the server and creating a model with this attribute and image.
Model:
class Mock
include Mongoid::Document
include Mongoid::Paperclip
field :name, type: String
has_mongoid_attached_file :image
validates_attachment_presence :image
validates_attachment_size :image, :less_than => 1.megabytes
validates_attachment_file_name :image, :matches => [/png\Z/, /jpe?g\Z/]
end
Controller (Grape):
desc "Create an mock."
params do
requires :name, type: String, desc: "Mock name"
requires :image, :type => Rack::Multipart::UploadedFile, :desc => "Image file."
end
post do
mock = Mock.create!(
name: params[:name],
image: ActionDispatch::Http::UploadedFile.new(params[:image])
)
end
curl -i -X POST -H "Content-Type:multipart/form-data" -F image=#\"./ruby.png\";type=image/png;filename=\"ruby.png\"" http://localhost:3000/api/mock
I have one user who is seeing it with various browsers.. I can't duplicate it on my local development server or on production. It works for me, doesn't work for them. Haven't been able to isolate anything that is different (tried PC vs Mac, various browsers, etc.)
Pushing paperclip back from 4.3 to 4.2.1 makes the issue go away. This switches activemodel and activesupport from 3.2 back to 3.0 and it eliminates the dependency on mimemagic. My guess is that it's something subtle and complex in mimemagic.

paperclip not displaying medium or thumb in rails 3.1

I'm using paperclip with Rails 3.1. When I add the image, it shows me the original size but doesn't show me thumb or medium sizes:
Here is what I have in my view:
<%= image_tag #image.avatar.url(:thumb) %>
<%= image_tag #image.avatar.url(:medium) %>
image.rb
has_attached_file :avatar, :whiny => false, :styles => { :medium => "300x300>", :thumb => "100x100>" }
UPDATE:
Here is the error I'm getting with :whiny => true
Command :: identify -format %wx%h '/var/folders/54/txjcl9l130j6dq73r37hf2c00000gn/T/stream20111213-9180-1plu1me.png[0]'
[paperclip] An error was received while processing: #
Command :: identify -format %wx%h '/var/folders/54/txjcl9l130j6dq73r37hf2c00000gn/T/stream20111213-9180-1plu1me.png[0]'
[paperclip] An error was received while processing: #
Rendered images/new.html.erb within layouts/application (4.0ms)
First, make sure that Image Magick is installed.
To see if its installed properly, go to a terminal session and type which convert. You should see a path to the executable.
Once that is done, you may need to add the path to your environment.rb file. For example, my convert is located at /usr/local/bin/convert. Now I've seen two different ways of setting your path for paperclip, try one or the other and see what works.
# specifically set the paperclip path
Paperclip.options[:command_path] = '/usr/local/bin'
# set the path in general, might not be necessary
ENV['PATH'] = '/usr/local/bin:' + ENV['PATH']

Resources