Class: Marcel::MimeType

Inherits:
Object
  • Object
show all
Defined in:
lib/marcel/mime_type.rb

Constant Summary collapse

BINARY =
"application/octet-stream"

Class Method Summary collapse

Class Method Details

.extend(type, extensions: [], parents: [], magic: nil) ⇒ Object



8
9
10
11
12
# File 'lib/marcel/mime_type.rb', line 8

def extend(type, extensions: [], parents: [], magic: nil)
  extensions = (Array(extensions) + Array(Marcel::TYPE_EXTS[type])).uniq
  parents = (Array(parents) + Array(Marcel::TYPE_PARENTS[type])).uniq
  Magic.add(type, extensions: extensions, magic: magic, parents: parents)
end

.for(pathname_or_io = nil, name: nil, extension: nil, declared_type: nil) ⇒ Object

Returns the most appropriate content type for the given file.

The first argument should be a Pathname or an IO. If it is a Pathname, the specified file will be opened first.

Optional parameters:

  • name: file name, if known

  • extension: file extension, if known

  • declared_type: MIME type, if known

The most appropriate type is determined by the following:

  • type declared by binary magic number data

  • type declared by the first of file name, file extension, or declared MIME type

If no type can be determined, then application/octet-stream is returned.



29
30
31
32
# File 'lib/marcel/mime_type.rb', line 29

def for(pathname_or_io = nil, name: nil, extension: nil, declared_type: nil)
  filename_type = for_name(name) || for_extension(extension)
  most_specific_type for_data(pathname_or_io), for_declared_type(declared_type), filename_type, BINARY
end