Commit 044acfcd by austin.bain

Added a validation to validate against the content-type

Added tests to prove that the new validation is working.
Added the public directory to .gitignore
Changed the upfile content_type code to assign txt files to text/plain mime type
parent 5bbafae0
......@@ -2,3 +2,4 @@
*.swp
tmp
s3.yml
public
\ No newline at end of file
......@@ -37,6 +37,9 @@ require 'paperclip/attachment'
module Paperclip
VERSION = "2.1.0"
@@content_types = ['image/jpeg', 'image/pjpeg', 'image/gif', 'image/png', 'image/x-png', 'image/jpg']
mattr_reader :content_types
class << self
# Provides configurability to Paperclip. There are a number of options available, such as:
......@@ -166,6 +169,22 @@ module Paperclip
end
end
end
# Places ActiveRecord-style validations on the content type of the file assigned. The
# possible options are:
# * +content_type+: Allowed content types. Can be a single content type or an array. Allows all by default. Use :image to allow all standard image types.
# * +message+: The message to display when the uploaded file has an invalid content type.
def validates_attachment_content_type name, options = {}
attachment_definitions[name][:validations] << lambda do |attachment, instance|
options[:content_type] = [options[:content_type]].flatten.collect! { |t| t == :image ? Paperclip.content_types : t }.flatten unless options[:content_type].nil?
unless options[:content_type].empty?
unless options[:content_type].include?(instance[:"#{name}_content_type"])
options[:message] || ActiveRecord::Errors.default_error_messages[:inclusion]
end
end
end
end
# Returns the attachment definitions defined by each call to has_attached_file.
def attachment_definitions
......
......@@ -9,7 +9,8 @@ module Paperclip
type = self.path.match(/\.(\w+)$/)[1] rescue "octet-stream"
case type
when "jpg", "png", "gif" then "image/#{type}"
when "txt", "csv", "xml", "html", "htm", "css", "js" then "text/#{type}"
when "txt" then "text/plain"
when "csv", "xml", "html", "htm", "css", "js" then "text/#{type}"
else "x-application/#{type}"
end
end
......
......@@ -64,12 +64,15 @@ class PaperclipTest < Test::Unit::TestCase
assert Dummy.new.respond_to?(:avatar=)
end
[[:presence, nil, "5k.png", nil],
[:size, {:in => 1..10240}, "5k.png", "12k.png"]].each do |args|
[[:presence, nil, "5k.png", nil],
[:size, {:in => 1..10240}, "5k.png", "12k.png"],
[:content_type1,{:content_type => "image/png"}, "5k.png", "text.txt"],
[:content_type2, {:content_type => :image}, "5k.png", "text.txt"],
[:content_type3, {:content_type => "text/plain"}, "text.txt", "5k.png"]].each do |args|
context "with #{args[0]} validations" do
setup do
Dummy.class_eval do
send(*[:"validates_attachment_#{args[0]}", :avatar, args[1]].compact)
send(*[:"validates_attachment_#{args[0].to_s[/[a-z_]*/]}", :avatar, args[1]].compact)
end
@dummy = Dummy.new
end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment