]> source.dussan.org Git - redmine.git/commitdiff
Store mime type lookups in a hash.
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Sun, 5 Jan 2014 17:56:21 +0000 (17:56 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Sun, 5 Jan 2014 17:56:21 +0000 (17:56 +0000)
git-svn-id: http://svn.redmine.org/redmine/trunk@12484 e93f8b46-1217-0410-a6f0-8f06a7374b81

lib/redmine/mime_type.rb

index 7231e9898f1a3a8cb33fa366bcca97f6c8082c62..caca97106e23b3af57645007faba5eb581b1613a 100644 (file)
@@ -55,13 +55,16 @@ module Redmine
 
     # returns mime type for name or nil if unknown
     def self.of(name)
-      return nil unless name
-      m = name.to_s.match(/(^|\.)([^\.]+)$/)
-      ext = m[2].downcase
-      type = nil
-      type = EXTENSIONS[ext] if m
-      type ||= MIME::Types.find {|type| type.extensions.include?(ext)}.to_s.presence
-      type
+      return nil unless name.present?
+      if m = name.to_s.match(/(^|\.)([^\.]+)$/)
+        extension = m[2].downcase
+        @known_types ||= Hash.new do |h, ext|
+          type = EXTENSIONS[ext]
+          type ||= MIME::Types.find {|type| type.extensions.include?(ext)}.to_s.presence
+          h[ext] = type
+        end
+        @known_types[extension]
+      end
     end
 
     # Returns the css class associated to