Commit a99ffec8 by Clemens Fuchslocher Committed by Sid Raval

MIME type detection of Paperclip::MediaTypeSpoofDetector doesn't work with old versions of file.

Please see #2527 for details.
parent 406ab458
...@@ -72,7 +72,8 @@ module Paperclip ...@@ -72,7 +72,8 @@ module Paperclip
def type_from_file_command def type_from_file_command
begin begin
Paperclip.run("file", "-b --mime :file", :file => @file.path).split(/[:;]\s+/).first Paperclip.run("file", "-b --mime :file", file: @file.path).
split(/[:;\s]+/).first
rescue Cocaine::CommandLineError rescue Cocaine::CommandLineError
"" ""
end end
......
...@@ -23,4 +23,18 @@ describe Paperclip::FileCommandContentTypeDetector do ...@@ -23,4 +23,18 @@ describe Paperclip::FileCommandContentTypeDetector do
assert_equal "application/octet-stream", assert_equal "application/octet-stream",
Paperclip::FileCommandContentTypeDetector.new("windows").detect Paperclip::FileCommandContentTypeDetector.new("windows").detect
end end
context "#type_from_file_command" do
let(:detector) { Paperclip::FileCommandContentTypeDetector.new("html") }
it "does work with the output of old versions of file" do
Paperclip.stubs(:run).returns("text/html charset=us-ascii")
expect(detector.detect).to eq("text/html")
end
it "does work with the output of new versions of file" do
Paperclip.stubs(:run).returns("text/html; charset=us-ascii")
expect(detector.detect).to eq("text/html")
end
end
end end
...@@ -76,4 +76,19 @@ describe Paperclip::MediaTypeSpoofDetector do ...@@ -76,4 +76,19 @@ describe Paperclip::MediaTypeSpoofDetector do
Paperclip.options[:content_type_mappings] = {} Paperclip.options[:content_type_mappings] = {}
end end
end end
context "#type_from_file_command" do
let(:file) { File.new(fixture_file("empty.html")) }
let(:detector) { Paperclip::MediaTypeSpoofDetector.new(file, "html", "") }
it "does work with the output of old versions of file" do
Paperclip.stubs(:run).returns("text/html charset=us-ascii")
expect(detector.send(:type_from_file_command)).to eq("text/html")
end
it "does work with the output of new versions of file" do
Paperclip.stubs(:run).returns("text/html; charset=us-ascii")
expect(detector.send(:type_from_file_command)).to eq("text/html")
end
end
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