Ruby on Rails: restrict file type with Paperclip using a flash uploader

Posted by aperture on Stack Overflow See other posts from Stack Overflow or by aperture
Published on 2011-02-19T13:15:34Z Indexed on 2011/02/19 15:25 UTC
Read the original article Hit count: 221

I have a pretty basic Paperclip Upload model that is attached to a User model through has_many, and am using Uploadify to do the actual uploading. Flash sends all files with the content type of "application/octet-stream" so using validates_attachment_content_type rejects all files.

In my create action, I am able to get the mime-type from the original file name, but only after it's been saved, with:

def coerce(params)
  h = Hash.new 
  h[:upload] = Hash.new 
  h[:upload][:attachment].content_type = MIME::Types.type_for(h[:upload][:attachment].original_filename).to_s
  ...
end

and

def create
  diff_params = coerce(params)
  @upload = Upload.new(diff_params[:upload])
  ...
end

What would be the best way of white listing file types?

I am thinking a before_validation method, but I'm not sure how that would work. Any ideas would be welcome.

© Stack Overflow or respective owner

Related posts about ruby-on-rails-3

Related posts about paperclip