Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
Fix a problem with incorrect content_type detected with 'file' comman…
Browse files Browse the repository at this point in the history
…d for an empty file on Mac
  • Loading branch information
kir committed Apr 15, 2012
1 parent 05a2371 commit 3e98fc2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/paperclip/io_adapters/file_adapter.rb
Expand Up @@ -69,7 +69,7 @@ def best_content_type_option(types)
def type_from_file_command
# On BSDs, `file` doesn't give a result code of 1 if the file doesn't exist.
type = (self.original_filename.match(/\.(\w+)$/)[1] rescue "octet-stream").downcase
mime_type = (Paperclip.run("file", "-b --mime :file", :file => self.path).split(/[:;]\s+/)[0] rescue "application/x-#{type}")
mime_type = (Paperclip.run("file", "-b --mime :file", :file => self.path).split(/[:;\s]+/)[0] rescue "application/x-#{type}")
mime_type = "application/x-#{type}" if mime_type.match(/\(.*?\)/)
mime_type
end
Expand Down
7 changes: 7 additions & 0 deletions test/file_adapter_test.rb
Expand Up @@ -20,6 +20,13 @@ class FileAdapterTest < Test::Unit::TestCase
assert_equal "image/png", @subject.content_type
end

should "provide correct mime-type for empty file" do
@subject = Paperclip.io_adapters.for(Tempfile.new("file_adapter_test"))

# Content type contained '\n' at the end, for an empty file, on my Mac
assert_equal "application/x-empty", @subject.content_type
end

should "get the file's size" do
assert_equal 4456, @subject.size
end
Expand Down

0 comments on commit 3e98fc2

Please sign in to comment.