Permalink
Browse files

* introduce UploadedFileAdapter::options

* UploadedFileAdapter::options[:trust_mime_type] defines whether to
trust the UploadedFile content_type or to detect it using ContentTypeDetector

* documentation, tests
  • Loading branch information...
1 parent 994d1a5 commit ed05dc6b85f52ad88a83bc18e1986f3b75176740 Richard Hirner committed with rfc2822 Oct 30, 2012
View
@@ -226,13 +226,17 @@ end
Another option is to directly modify the Paperclip::Attachment.default_options Hash, this method works for non-Rails applications or is an option if you prefer to place the Paperclip default settings in an initializer.
+Modules like the UploadedFileAdapter which is responsible for processing HTTP file uploads can have their own options, too.
+
An example Rails initializer would look something like this:
```ruby
Paperclip::Attachment.default_options[:storage] = :fog
Paperclip::Attachment.default_options[:fog_credentials] = {:provider => "Local", :local_root => "#{Rails.root}/public"}
Paperclip::Attachment.default_options[:fog_directory] = ""
Paperclip::Attachment.default_options[:fog_host] = "http://localhost:3000"
+
+Paperclip::UploadedFileAdapter.options[:trust_mime_type] = false
```
Migrations
@@ -1,6 +1,20 @@
module Paperclip
+ # The UploadedFileAdapter class is responsible for processing files that were
+ # uploaded by a HTTP client (UploadedFile).
class UploadedFileAdapter < AbstractAdapter
+ # Available options:
+ #
+ # +trust_mime_type+ (default: true) - Whether to trust the Content-Type sent by the uploading client
+ # (browser in most cases). If set to +false+, the MIME type will be detected by ContentTypeDetector
+ def self.options
+ @options ||= {
+ :trust_mime_type => true
+ }
+ end
+
def initialize(target)
+ @options = self.class.options
+
@target = target
cache_current_values
@@ -15,7 +29,9 @@ def initialize(target)
def cache_current_values
@original_filename = @target.original_filename
- @content_type = @target.content_type.to_s.strip
+ @content_type = @options[:trust_mime_type] ?
+ @target.content_type.to_s.strip :
+ ContentTypeDetector.new(@target.path).detect
@size = File.size(@target.path)
end
end
@@ -1,2 +0,0 @@
-"*��7��Y�"8�D\�>y�ո
-�r���
@@ -10,11 +10,13 @@ class UploadedFile < OpenStruct; end
@file = UploadedFile.new(
:original_filename => "5k.png",
- :content_type => "image/png\r",
+ :content_type => "image/x-png-by-browser\r", # assuming browser sends image/x-png-by-browser instead of image/png
:head => "",
:tempfile => tempfile,
:path => tempfile.path
)
+
+ Paperclip::UploadedFileAdapter.options[:trust_mime_type] = true
@subject = Paperclip.io_adapters.for(@file)
end
@@ -27,7 +29,7 @@ class UploadedFile < OpenStruct; end
end
should "get the content type" do
- assert_equal "image/png", @subject.content_type
+ assert_equal "image/x-png-by-browser", @subject.content_type
end
should "get the file's size" do
@@ -55,10 +57,12 @@ class UploadedFile < OpenStruct; end
class UploadedFile < OpenStruct; end
@file = UploadedFile.new(
:original_filename => "5k.png",
- :content_type => "image/png",
+ :content_type => "image/x-png-by-browser",
:head => "",
:path => fixture_file("5k.png")
)
+
+ Paperclip::UploadedFileAdapter.options[:trust_mime_type] = true
@subject = Paperclip.io_adapters.for(@file)
end
@@ -71,7 +75,7 @@ class UploadedFile < OpenStruct; end
end
should "get the content type" do
- assert_equal "image/png", @subject.content_type
+ assert_equal "image/x-png-by-browser", @subject.content_type
end
should "get the file's size" do
@@ -95,5 +99,24 @@ class UploadedFile < OpenStruct; end
assert_equal expected, @subject.read
end
end
+
+ context "don't trust client-given MIME type" do
+ setup do
+ Paperclip::UploadedFileAdapter.options[:trust_mime_type] = false
+
+ class UploadedFile < OpenStruct; end
+ @file = UploadedFile.new(
+ :original_filename => "5k.png",
+ :content_type => "image/x-png-by-browser",
+ :head => "",
+ :path => fixture_file("5k.png")
+ )
+ @subject = Paperclip.io_adapters.for(@file)
+ end
+
+ should "get the content type" do
+ assert_equal "image/png", @subject.content_type
+ end
+ end
end
end

0 comments on commit ed05dc6

Please sign in to comment.