mime.rb 968 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. require 'rack/mime'
  2. module Sprockets
  3. module Mime
  4. # Returns a `Hash` of registered mime types registered on the
  5. # environment and those part of `Rack::Mime`.
  6. #
  7. # If an `ext` is given, it will lookup the mime type for that extension.
  8. def mime_types(ext = nil)
  9. if ext.nil?
  10. Rack::Mime::MIME_TYPES.merge(@mime_types)
  11. else
  12. ext = Sprockets::Utils.normalize_extension(ext)
  13. @mime_types[ext] || Rack::Mime::MIME_TYPES[ext]
  14. end
  15. end
  16. if {}.respond_to?(:key)
  17. def extension_for_mime_type(type)
  18. mime_types.key(type)
  19. end
  20. else
  21. def extension_for_mime_type(type)
  22. mime_types.index(type)
  23. end
  24. end
  25. # Register a new mime type.
  26. def register_mime_type(mime_type, ext)
  27. ext = Sprockets::Utils.normalize_extension(ext)
  28. @mime_types[ext] = mime_type
  29. end
  30. end
  31. # Extend Sprockets module to provide global registry
  32. extend Mime
  33. @mime_types = {}
  34. end