Commit 9ecd52ea by Stephen Pike

Fix some style guide issues

parent 2a0274d1
...@@ -54,24 +54,26 @@ module Paperclip ...@@ -54,24 +54,26 @@ module Paperclip
end end
def calculated_type_matches def calculated_type_matches
possible_types.select { |content_type| content_type == type_from_file_contents } possible_types.select do |content_type|
content_type == type_from_file_contents
end
end end
def type_from_file_contents def type_from_file_contents
type = begin type_from_mime_magic || type_from_file_command
type_from_mime_magic || type_from_file_command rescue Errno::ENOENT => e
rescue Errno::ENOENT => e Paperclip.log("Error while determining content type: #{e}")
Paperclip.log("Error while determining content type: #{e}") SENSIBLE_DEFAULT
SENSIBLE_DEFAULT
end
end end
def type_from_file_command def type_from_file_command
@type_from_file_command ||= FileCommandContentTypeDetector.new(@filename).detect @type_from_file_command ||=
FileCommandContentTypeDetector.new(@filename).detect
end end
def type_from_mime_magic def type_from_mime_magic
@type_from_mime_magic ||= MimeMagic.by_magic(File.open(@filename)).try(:type) @type_from_mime_magic ||=
MimeMagic.by_magic(File.open(@filename)).try(:type)
end end
end end
end end
...@@ -13,8 +13,8 @@ module Paperclip ...@@ -13,8 +13,8 @@ module Paperclip
private private
def type_from_file_command def type_from_file_command
# On BSDs, `file` doesn't give a result code of 1 if the file doesn't exist.
type = begin type = begin
# On BSDs, `file` doesn't give a result code of 1 if the file doesn't exist.
Paperclip.run("file", "-b --mime :file", file: @filename) Paperclip.run("file", "-b --mime :file", file: @filename)
rescue Cocaine::CommandLineError => e rescue Cocaine::CommandLineError => e
Paperclip.log("Error while determining content type: #{e}") Paperclip.log("Error while determining content type: #{e}")
......
...@@ -4,7 +4,7 @@ describe Paperclip::ContentTypeDetector do ...@@ -4,7 +4,7 @@ describe Paperclip::ContentTypeDetector do
it 'returns a meaningful content type for open xml spreadsheets' do it 'returns a meaningful content type for open xml spreadsheets' do
file = File.new(fixture_file("empty.xlsx")) file = File.new(fixture_file("empty.xlsx"))
assert_equal "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", assert_equal "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
Paperclip::ContentTypeDetector.new(file.path).detect Paperclip::ContentTypeDetector.new(file.path).detect
end end
it 'gives a sensible default when the name is empty' do it 'gives a sensible default when the name is empty' do
...@@ -19,7 +19,8 @@ describe Paperclip::ContentTypeDetector do ...@@ -19,7 +19,8 @@ describe Paperclip::ContentTypeDetector do
it 'returns content type of file if it is an acceptable type' do it 'returns content type of file if it is an acceptable type' do
MIME::Types.stubs(:type_for).returns([MIME::Type.new('application/mp4'), MIME::Type.new('video/mp4'), MIME::Type.new('audio/mp4')]) MIME::Types.stubs(:type_for).returns([MIME::Type.new('application/mp4'), MIME::Type.new('video/mp4'), MIME::Type.new('audio/mp4')])
Paperclip::ContentTypeDetector.any_instance.stubs(:type_from_file_contents).returns("video/mp4") Paperclip::ContentTypeDetector.any_instance
.stubs(:type_from_file_contents).returns("video/mp4")
@filename = "my_file.mp4" @filename = "my_file.mp4"
assert_equal "video/mp4", Paperclip::ContentTypeDetector.new(@filename).detect assert_equal "video/mp4", Paperclip::ContentTypeDetector.new(@filename).detect
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