Skip to content

Commit

Permalink
MIME type detection of Paperclip::MediaTypeSpoofDetector doesn't work…
Browse files Browse the repository at this point in the history
… with old versions of file.

Please see thoughtbot#2527 for details.
  • Loading branch information
Clemens Fuchslocher committed Jan 13, 2018
1 parent c794f6d commit 8645eff
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/paperclip/media_type_spoof_detector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ def calculated_media_type

def type_from_file_command
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
""
end
Expand Down
14 changes: 14 additions & 0 deletions spec/paperclip/file_command_content_type_detector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,18 @@
assert_equal "application/octet-stream",
Paperclip::FileCommandContentTypeDetector.new("windows").detect
end

context "#type_from_file_command" do
let(:detector) { Paperclip::FileCommandContentTypeDetector.new("empty.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
14 changes: 14 additions & 0 deletions spec/paperclip/media_type_spoof_detector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,18 @@
Paperclip.options[:content_type_mappings] = {}
end
end

context "#type_from_file_command" do
let(:detector) { Paperclip::MediaTypeSpoofDetector.new(File.new(fixture_file("empty.html")), "empty.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

0 comments on commit 8645eff

Please sign in to comment.