Skip to content

Commit a99ffec

Browse files
Clemens Fuchslochersidraval
authored andcommitted
MIME type detection of Paperclip::MediaTypeSpoofDetector doesn't work with old versions of file.
Please see thoughtbot#2527 for details.
1 parent 406ab45 commit a99ffec

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

lib/paperclip/media_type_spoof_detector.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ def calculated_media_type
7272

7373
def type_from_file_command
7474
begin
75-
Paperclip.run("file", "-b --mime :file", :file => @file.path).split(/[:;]\s+/).first
75+
Paperclip.run("file", "-b --mime :file", file: @file.path).
76+
split(/[:;\s]+/).first
7677
rescue Cocaine::CommandLineError
7778
""
7879
end

spec/paperclip/file_command_content_type_detector_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,18 @@
2323
assert_equal "application/octet-stream",
2424
Paperclip::FileCommandContentTypeDetector.new("windows").detect
2525
end
26+
27+
context "#type_from_file_command" do
28+
let(:detector) { Paperclip::FileCommandContentTypeDetector.new("html") }
29+
30+
it "does work with the output of old versions of file" do
31+
Paperclip.stubs(:run).returns("text/html charset=us-ascii")
32+
expect(detector.detect).to eq("text/html")
33+
end
34+
35+
it "does work with the output of new versions of file" do
36+
Paperclip.stubs(:run).returns("text/html; charset=us-ascii")
37+
expect(detector.detect).to eq("text/html")
38+
end
39+
end
2640
end

spec/paperclip/media_type_spoof_detector_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,19 @@
7676
Paperclip.options[:content_type_mappings] = {}
7777
end
7878
end
79+
80+
context "#type_from_file_command" do
81+
let(:file) { File.new(fixture_file("empty.html")) }
82+
let(:detector) { Paperclip::MediaTypeSpoofDetector.new(file, "html", "") }
83+
84+
it "does work with the output of old versions of file" do
85+
Paperclip.stubs(:run).returns("text/html charset=us-ascii")
86+
expect(detector.send(:type_from_file_command)).to eq("text/html")
87+
end
88+
89+
it "does work with the output of new versions of file" do
90+
Paperclip.stubs(:run).returns("text/html; charset=us-ascii")
91+
expect(detector.send(:type_from_file_command)).to eq("text/html")
92+
end
93+
end
7994
end

0 commit comments

Comments
 (0)