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 @@ ...@@ -2,3 +2,4 @@
*.swp *.swp
tmp tmp
s3.yml s3.yml
public
\ No newline at end of file
...@@ -37,6 +37,9 @@ require 'paperclip/attachment' ...@@ -37,6 +37,9 @@ require 'paperclip/attachment'
module Paperclip module Paperclip
VERSION = "2.1.0" 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 class << self
# Provides configurability to Paperclip. There are a number of options available, such as: # Provides configurability to Paperclip. There are a number of options available, such as:
...@@ -166,6 +169,22 @@ module Paperclip ...@@ -166,6 +169,22 @@ module Paperclip
end end
end 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. # Returns the attachment definitions defined by each call to has_attached_file.
def attachment_definitions def attachment_definitions
......
...@@ -9,7 +9,8 @@ module Paperclip ...@@ -9,7 +9,8 @@ module Paperclip
type = self.path.match(/\.(\w+)$/)[1] rescue "octet-stream" type = self.path.match(/\.(\w+)$/)[1] rescue "octet-stream"
case type case type
when "jpg", "png", "gif" then "image/#{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}" else "x-application/#{type}"
end end
end end
......
...@@ -64,12 +64,15 @@ class PaperclipTest < Test::Unit::TestCase ...@@ -64,12 +64,15 @@ class PaperclipTest < Test::Unit::TestCase
assert Dummy.new.respond_to?(:avatar=) assert Dummy.new.respond_to?(:avatar=)
end end
[[:presence, nil, "5k.png", nil], [[:presence, nil, "5k.png", nil],
[:size, {:in => 1..10240}, "5k.png", "12k.png"]].each do |args| [: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 context "with #{args[0]} validations" do
setup do setup do
Dummy.class_eval 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 end
@dummy = Dummy.new @dummy = Dummy.new
end 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