Permalink
Browse files

Added support for files with spaces in name.

  • Loading branch information...
1 parent e1dfd1c commit 06005fc0a66028ac87b1d6892c97049a8092e751 @tomharris tomharris committed Jun 23, 2009
Showing with 31 additions and 17 deletions.
  1. +2 −2 lib/mimetype_fu.rb
  2. +29 −15 spec/mime_type_spec.rb
View
@@ -3,7 +3,7 @@ class File
def self.mime_type?(file)
if file.class == File
unless RUBY_PLATFORM.include? 'mswin32'
- mime = `file --mime -br #{file.path}`.strip
+ mime = `file --mime -br "#{file.path}"`.strip
else
mime = EXTENSIONS[File.extname(file.path).gsub('.','').downcase.to_sym]
end
@@ -13,7 +13,7 @@ def self.mime_type?(file)
temp = File.open(Dir.tmpdir + '/upload_file.' + Process.pid.to_s, "wb")
temp << file.string
temp.close
- mime = `file --mime -br #{temp.path}`
+ mime = `file --mime -br "#{temp.path}"`
mime = mime.gsub(/^.*: */,"")
mime = mime.gsub(/;.*$/,"")
mime = mime.gsub(/,.*$/,"")
View
@@ -2,24 +2,24 @@
require File.dirname(__FILE__) + '/../lib/mimetype_fu'
# Ideally, the two solutions (`file` command versus simply looking at the extension)
-# would return identical results, but thats not the case. `file` differentiates
+# would return identical results, but thats not the case. `file` differentiates
# between file sizes, and has a tendency to classify files into the 'application/octet-stream'
# mimetype, whereas, looking at the ext results in 'unknown/unknown' more often.
describe "An open file" do
-
+
describe "with a zero filelength" do
-
+
before {create_file('file.empty')}
it 'should have a length of zero' do
File.size(@file).should == 0
end
it_should_have_a_mime_type_of("application/x-empty")
-
+
end
-
+
describe 'with a known extension' do
before {write_file('file.png', "\211PNG\r\n\032\n", 'wb')}
@@ -32,7 +32,21 @@
it_should_have_a_mime_type_of("image/png")
end
-
+
+ describe 'with spaces in name' do
+
+ before {write_file('file with spaces.png', "\211PNG\r\n\032\n", 'wb')}
+
+ it 'should have an extension' do
+ File.extname(@file.path).should == '.png'
+ end
+
+ it 'should have a mime type' do
+ File.mime_type?(@file).should == "image/png"
+ end
+
+ end
+
describe 'with an unknown extension' do
before {write_file('file.unknown', "\211Random\r\n\032\n", 'wb')}
@@ -45,7 +59,7 @@
it_should_have_a_mime_type_of("application/octet-stream")
end
-
+
describe 'with no extension' do
before{write_file('file', "Random\r\n\032\n", 'wb')}
@@ -57,23 +71,23 @@
it_should_have_a_mime_type_of("application/octet-stream")
end
-
+
end
describe "A filepath (closed file)" do
-
+
after(:each) do
delete_file
end
-
+
describe "with a zero filelength" do
-
+
before {create_file('file.empty'); close}
it_should_have_a_mime_type_of("unknown/unknown")
-
+
end
-
+
describe 'with a known extension' do
before {write_file('file.png', "\211PNG\r\n\032\n", 'wb'); close}
@@ -86,7 +100,7 @@
it_should_have_a_mime_type_of("image/png")
end
-
+
describe 'with an unknown extension' do
before {write_file('file.unknown', "\211Random\r\n\032\n", 'wb'); close}
@@ -99,7 +113,7 @@
it_should_have_a_mime_type_of("unknown/unknown")
end
-
+
describe 'with no extension' do
before{write_file('file', "Random\r\n\032\n", 'wb'); close}

0 comments on commit 06005fc

Please sign in to comment.