Reputation: 2135
My model looks like:
class Art < ActiveRecord::Base
attr_accessible :image
has_attached_file :image, :styles => { :medium => "620x620>", :thumb => "200x200>" }
validates_attachment :image, :content_type => { :content_type => "image/png" }
end
It validates and allows me to upload a PNG file png_file.png and does not allow me to upload a JPG file jpg_file.jpg.
But if I rename the PNG file to png_file.jpg it does not allow me to upload the image. And if I rename the JPG file to jpg_file.png it wrongly succeeds to upload the file.
I would like to know how can I validates the uploaded file by its real contents rather than the file extension. Does anyone knows how to solve this problem?
Upvotes: 0
Views: 444
Reputation: 9969
You could read the file in raw binary and check if it's of type PNG:
File.open('path-to-your-file', 'rb').read(9).include?('PNG')
The 9
is the magic number of file.
Upvotes: 2