#-- # rbmh/suffixes.rb : rbmhshow 0.4.2 # # Copyright (C) 2004--2005 Merlin Hughes # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # #++ # MIME suffixes module RBMH class MimeSuffixes def initialize(context) @context = context @suffixes = {} profile_init end # text/plain or text/* def [](mime_type) @suffixes[mime_type] or @suffixes[mime_type.gsub(SubtypeRE, '*')] end private def profile_init cache_file = @context.profile[ProfileCache] if cache_file.nil? read_profile else cache_file = File.expand_path(cache_file) if (not FileTest.exists?(cache_file)) or (profile_mtime > File.stat(cache_file).mtime) read_profile write_suffixes(cache_file) else read_suffixes(cache_file) end end end def profile_mtime mtime = @context.profile.mtime filenames = @context.profile[ProfileFiles] filenames.scan(FilenameRE) { | filename | filename = File.expand_path(filename) if FileTest.readable?(filename) file_mtime = File.stat(filename).mtime mtime = file_mtime if (file_mtime > mtime) end } if filenames mtime end def read_profile filenames = @context.profile[ProfileFiles] filenames.scan(FilenameRE) { | filename | filename = File.expand_path(filename) if FileTest.readable?(filename) File.open(filename) { | mime_file | mime_file_mtime = mime_file.mtime mtime = mime_file.each { | line | if line =~ MimeLineRE @suffixes[$1.downcase] = $2 end } } else STDERR.puts "rbmh: MIME file #{filename} unreadable" end } if filenames @context.profile.each(ProfileSuffixRE) { |mime_type, suffix| @suffixes[mime_type.downcase] = suffix } @context.profile.each(ProfileAliasRE) { |mime_type, mime_alias| @suffixes[mime_type.downcase] = @suffixes[mime_alias.downcase] if @suffixes.has_key?(mime_alias.downcase) and not @suffixes.has_key?(mime_type.downcase) } end def read_suffixes(cache_file) File.open(cache_file) { | file | file.each { | line | mime_type, suffix = line.split(SpaceRE) @suffixes[mime_type] = suffix.chop } } end def write_suffixes(cache_file) STDERR.puts "rbmh: updating #{cache_file}" File.open(cache_file, "w") { | file | @suffixes.each { | key, value | file.puts "#{key} #{value}" } } end # TODO: fix this to match the spec MimeTypeRE = /([^\s\/]+\/\S+)/ # $1: type/subtype SubtypeRE = /([^\/]+)$/ # $1: subtype MimeLineRE = /^#{MimeTypeRE.source}\s+(\w+).*$/ # $1: mimetype, $2: suffix1 ProfileCache = 'rbmh-mime-cache' ProfileFiles = 'rbmh-mime-files' ProfileSuffixRE = /^rbmh-mime-suffix-#{MimeTypeRE.source}$/ # $1: mimetype ProfileAliasRE = /^rbmh-mime-alias-#{MimeTypeRE.source}$/ # $1: mimetype FilenameRE = /\S+/ SpaceRE = / / end # class MimeSuffixes end # module RBMH