Skip to content

Commit 8c7c1e8

Browse files
author
Jon Yurek
committedApr 26, 2010
Handling the file command, added after the initial rails3 branch.
1 parent 488ffd2 commit 8c7c1e8

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed
 

‎lib/paperclip/upfile.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ def content_type
1616
when "js" then "application/js"
1717
when "csv", "xml", "css" then "text/#{type}"
1818
else
19-
Paperclip.run("file", "--mime-type #{self.path}").split(':').last.strip rescue "application/x-#{type}"
19+
# On BSDs, `file` doesn't give a result code of 1 if the file doesn't exist.
20+
content_type = (Paperclip.run("file", "--mime-type #{self.path}").split(':').last.strip rescue "application/x-#{type}")
21+
content_type = "application/x-#{type}" if content_type.match(/\(.*?\)/)
22+
content_type
2023
end
2124
end
2225

‎test/upfile_test.rb

+7-7
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ class << file
2323

2424
assert_equal content_type, file.content_type
2525
end
26+
end
27+
end
2628

27-
should "return a content_type of text/plain on a real file whose content_type is determined with the file command" do
28-
file = File.new(File.join(File.dirname(__FILE__), "..", "LICENSE"))
29-
class << file
30-
include Paperclip::Upfile
31-
end
32-
assert_equal 'text/plain', file.content_type
33-
end
29+
should "return a content_type of text/plain on a real file whose content_type is determined with the file command" do
30+
file = File.new(File.join(File.dirname(__FILE__), "..", "LICENSE"))
31+
class << file
32+
include Paperclip::Upfile
3433
end
34+
assert_equal 'text/plain', file.content_type
3535
end
3636
end

0 commit comments

Comments
 (0)