Module: DerivativeRodeo::Services::MimeTypeService

Defined in:
lib/derivative_rodeo/services/mime_type_service.rb

Overview

This module provides an interface for determining a mime-type.

Class Method Summary collapse

Class Method Details

.hyrax_type(filename:) ⇒ Symbol

Hyrax has it’s own compression of mime_types into conceptual types (as defined in Hyrax::FileSetDerivativesService). This provides a somewhat conceptual overlap with that, while also being more generalized.

Parameters:

  • filename (String)

Returns:

  • (Symbol)


16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/derivative_rodeo/services/mime_type_service.rb', line 16

def self.hyrax_type(filename:)
  mime = mime_type(filename: filename)
  media_type, sub_type = mime.split("/")
  case media_type
  when "image", "audio", "text", "video"
    media_type.to_sym
  when "application" # The wild woolly weird world of all the things.
    # TODO: Do we need to worry about office documents?
    sub_type.to_sym
  else
    sub_type.to_sym
  end
end

.mime_type(filename:) ⇒ String

Given a local :filename (e.g. downloaded and available on the server this is running), return the mime_type of the file.

Parameters:

  • filename (String)

Returns:

  • (String)

    (e.g. “application/pdf”, “text/plain”)



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/derivative_rodeo/services/mime_type_service.rb', line 36

def self.mime_type(filename:)
  ##
  # TODO: Does this attempt to read the whole file?  That may create memory constraints.  By
  # using Pathname (instead of File.read), we're letting Marcel do it's best mime magic.
  pathname = Pathname.new(filename)
  extension = filename.split(".")&.last&.downcase
  if extension
    # By including a possible extension, we can help nudge Marcel into making a more
    # Without extension, we will get a lot of "application/octet-stream" results.
    ::Marcel::MimeType.for(pathname, extension: extension)
  else
    ::Marcel::MimeType.for(pathname)
  end
end