0
votes

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.

1

1 Answers

1
votes

I installed mimetype-fu and stuck in

before_validation :find_mimetype

def find_mimetype
    attachment = self.attachment.to_file.path
    file = File.open(attachment)
    mime = File.mime_type?(file)
    self.attachment.instance_write(:content_type, mime)
end