class MimeType @@all=[] def self.all @@all end def self.add(exts,mime_name) all<> (0..17).to_a.in_transposed_slices(5) # => [[0, 5, 10, 15], [1, 6, 11, 16], [2, 7, 12, 17], [3, 8, 13], [4, 9, 14]] # while # >> (0..17).enum_slice(5).to_a # => [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17]] # # If some folders contain big files and some others contain small ones, # every indexing thread will get some of both! def in_transposed_slices(n) # no need to compute anything if n==1 return [self] if n==1 # Array#transpose would raise if Array is not a square array of arrays. i=n-self.size%n # Adds nils so that size is a multiple of n, # cuts array in slices of size n, # transposes to get n slices, # and removes added nils. (self+[nil]*i).enum_slice(n).to_a.transpose.collect{|e| e.compact} end end class File def self.ext_as_sym(filename) File.extname(filename).sub(/^\./,'').downcase.to_sym rescue :no_extension end def self.mime(filename) ext=ext_as_sym(filename) m=MimeType.all.find{|m| m.exts.include?(ext)} m ? m.name : 'application/octet-stream' end def self.encoding(source) parse_for_charset="grep -io charset=[a-z0-9\\-]* | sed 's/charset=//i'" if File.extname(source)[0,4]==".htm" then enc=%x{head -n20 \"#{source}\" | #{parse_for_charset}}.chomp else enc=%x{file -i \"#{source}\" | #{parse_for_charset}}.chomp end #iso-8859-15 should be used instead of iso-8859-1, for € char case enc when "iso-8859-1" "iso-8859-15" when "unknown" "" else enc end end def self.read_and_remove(filename) content=read(filename) FileUtils.rm filename, :force=>true content end end