7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/anyfetch/original_filename/content_type.rb', line 7
def original_filename
if meta.include? 'content-disposition'
match = meta['content-disposition'].match(/filename=(\"?)(.+)\1/)
return match[2] unless match.nil?
end
filename = ::File.basename(base_uri.path)
ext = ::File.extname(filename)
cmd = Terrapin::CommandLine.new('/usr/bin/file', '--mime-type -b :file')
begin
mime_type = cmd.run(:file => path)
rescue
mime_type = meta['content-type']
end
if mime = MIME::Types[mime_type.to_s.strip].first
mime_ext = mime.extensions.first
ext != mime_ext ? [::File.basename(filename, ext), '.', mime_ext].join : filename
else
filename
end
end
|