Class: Ddr::MediaType

Inherits:
Object
  • Object
show all
Defined in:
app/models/ddr/media_type.rb

Class Method Summary collapse

Class Method Details

.call(file_or_path) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/models/ddr/media_type.rb', line 4

def self.call(file_or_path)
  path = file_or_path.respond_to?(:path) ? file_or_path.path : file_or_path
  # Use preferred media type, if available
  media_type = Ddr.preferred_media_types[(::File.extname(path)).downcase]
  if !media_type
    if file_or_path.respond_to?(:content_type)
      # Rails ActionDispatch::Http::UploadedFile
      media_type = file_or_path.content_type
    else
      # Fall back to first MIME type or default
      mime_types = MIME::Types.of(path)
      media_type = mime_types.empty? ? Ddr.default_mime_type : mime_types.first.content_type
    end
  end
  media_type
end