Skip to content

class Athena::MIME::Types
inherits Reference #

Default implementation of AMIME::TypesInterface.

Also supports guessing a MIME type based on a given file path. Custom guessers can be registered via the #register_guesser method. Custom guessers are always called before any default ones.

mime_types = AMIME::Types.new

mime_types.mime_types "png"                     # => {"image/png", "image/apng", "image/vnd.mozilla.apng"}
mime_types.extensions "image/png"               # => {"png"}
mime_types.guess_mime_type "/path/to/image.png" # => "image/png"

Included modules

Athena::MIME::TypesInterface

Constructors#

.default : self#

Returns/sets the default singleton instance.

.new(map : Hash(String, Enumerable(String)) = Map.new)#

Class methods#

.default=(default : self)#

Returns/sets the default singleton instance.

Methods#

#extensions(for mime_type : String) : Enumerable(String)#

Returns the valid file extensions for the provided mime_type in decreasing order of preference.

types.extensions "image/png" # => {"png"}

#guess_mime_type(path : String | Path) : String | Nil#

Returns the guessed MIME type for the file at the provided path, or nil if it could not be determined.

How exactly the MIME type is determined is up to each individual implementation.

guesser.guess_mime_type "/path/to/image.png" # => "image/png"

#mime_types(for extension : String) : Enumerable(String)#

Returns the valid MIME types for the provided extension in decreasing order of preference.

types.mime_types "png" # => {"image/png", "image/apng", "image/vnd.mozilla.apng"}

#register_guesser(guesser : AMIME::TypesGuesserInterface) : Nil#

Registers the provided guesser. The last registered guesser is preferred over previously registered ones.

#supported? : Bool#

Returns true if this guesser is supported, otherwise false.

The value may be cached on the class level.