Method: Aspera::Preview::FileTypes#conversion_type

Defined in:
lib/aspera/preview/file_types.rb

#conversion_type(filepath, mimetype) ⇒ Object

Returns file type, one of enum CONVERSION_TYPES.

Parameters:

  • filepath (String)

    full path to file

  • mimetype (String)

    provided by node API

Returns:

  • file type, one of enum CONVERSION_TYPES

Raises:

  • (RuntimeError)

    if no conversion type found



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/aspera/preview/file_types.rb', line 90

def conversion_type(filepath, mimetype)
  Log.log.debug{"conversion_type(#{filepath},m=#{mimetype},t=#{@use_mimemagic})"}
  # 1- get type from provided mime type, using local mapping
  conversion_type = mime_to_type(mimetype) if !mimetype.nil?
  # 2- else, from computed mime type (if available)
  if conversion_type.nil? && @use_mimemagic
    detected_mime = file_to_mime(filepath)
    if !detected_mime.nil?
      conversion_type = mime_to_type(detected_mime)
      if !mimetype.nil?
        if mimetype.eql?(detected_mime)
          Log.log.debug('matching mime type per magic number')
        else
          # NOTE: detected can be nil
          Log.log.debug{"non matching mime types: node=[#{mimetype}], magic=[#{detected_mime}]"}
        end
      end
    end
  end
  # 3- else, from extensions, using local mapping
  mime_by_ext = MIME::Types.of(File.basename(filepath)).first
  conversion_type = mime_to_type(mime_by_ext.to_s) if conversion_type.nil? && !mime_by_ext.nil?
  raise "no conversion type found for #{File.basename(filepath)}" if conversion_type.nil?
  Log.log.trace1{"conversion_type(#{File.basename(filepath)}): #{conversion_type.class.name} [#{conversion_type}]"}
  return conversion_type
end