Skip to content

Commit

Permalink
Allow overriding the path with :orignal_filename
Browse files Browse the repository at this point in the history
When passing a multipart uploaded file to wand it is generally
a temp file with no extension leaving MIME with no way to detect
the type.
  • Loading branch information
raykrueger authored and jnunemaker committed Dec 20, 2010
1 parent 426c210 commit 467ad9b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/wand.rb
Expand Up @@ -3,8 +3,8 @@
module Wand module Wand
Version = '0.2.1' Version = '0.2.1'


def self.wave(path) def self.wave(path, options={})
type = MIME::Types.type_for(path)[0].to_s type = MIME::Types.type_for(options[:original_filename] || path)[0].to_s
type = execute_file_cmd(path).split(';')[0].strip if type.nil? || type == '' type = execute_file_cmd(path).split(';')[0].strip if type.nil? || type == ''
type = nil if type =~ /cannot\sopen/ type = nil if type =~ /cannot\sopen/
type type
Expand All @@ -21,4 +21,4 @@ def self.executable=(path)
def self.execute_file_cmd(path) def self.execute_file_cmd(path)
`#{executable} --mime --brief #{path}` `#{executable} --mime --brief #{path}`
end end
end end
4 changes: 4 additions & 0 deletions test/test_wand.rb
Expand Up @@ -32,6 +32,10 @@ class TestWand < Test::Unit::TestCase
end end
end end


should "return value from mime type when original_filename provided" do
assert_equal 'audio/mpeg', Wand.wave('some_temp_file', :original_filename => 'test.mp3')
end

should "return nil when mime type and file fail" do should "return nil when mime type and file fail" do
assert_nil Wand.wave('AVGARDD.eot') assert_nil Wand.wave('AVGARDD.eot')
end end
Expand Down

0 comments on commit 467ad9b

Please sign in to comment.