Class: Aspera::Preview::FileTypes

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/aspera/preview/file_types.rb

Overview

function conversion_type returns one of the types: CONVERSION_TYPES

Constant Summary collapse

CONVERSION_TYPES =

values for conversion_type : input format

i[image office pdf plaintext video].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFileTypes

Returns a new instance of FileTypes.



278
279
280
# File 'lib/aspera/preview/file_types.rb', line 278

def initialize
  @use_mimemagic = false
end

Instance Attribute Details

#use_mimemagicObject



276
277
278
# File 'lib/aspera/preview/file_types.rb', line 276

def use_mimemagic
  @use_mimemagic
end

Instance Method Details

#conversion_type(filepath, mimetype) ⇒ Object

return file type, one of enum CONVERSION_TYPES

Parameters:

  • filepath (String)

    full path to file

  • mimetype (String)

    provided by node api



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/aspera/preview/file_types.rb', line 303

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 = SUPPORTED_MIME_TYPES[mimetype] if !mimetype.nil?
  # 2- else, from computed mime type (if available)
  if conversion_type.nil? && @use_mimemagic
    detected_mime = mime_from_file(filepath)
    if !detected_mime.nil?
      conversion_type = SUPPORTED_MIME_TYPES[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
  extension = File.extname(filepath.downcase)[1..-1]
  conversion_type = SUPPORTED_EXTENSIONS[extension] if conversion_type.nil?
  Log.log.debug{"conversion_type(#{extension}): #{conversion_type.class.name} [#{conversion_type}]"}
  return conversion_type
end

#mime_from_file(filepath) ⇒ Object

use mime magic to find mime type based on file content (magic numbers)



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/aspera/preview/file_types.rb', line 283

def mime_from_file(filepath)
  # moved here, as mimemagic can cause installation issues
  require 'mimemagic'
  require 'mimemagic/version'
  require 'mimemagic/overlay' if MimeMagic::VERSION.start_with?('0.3.')
  # check magic number inside file (empty string if not found)
  detected_mime = MimeMagic.by_magic(File.open(filepath)).to_s
  # check extension only
  if !SUPPORTED_MIME_TYPES.key?(detected_mime)
    Log.log.debug{"no conversion for #{detected_mime}, trying extension"}
    detected_mime = MimeMagic.by_extension(File.extname(filepath)).to_s
  end
  detected_mime = nil if detected_mime.empty?
  Log.log.debug{"mimemagic: #{detected_mime.class.name} [#{detected_mime}]"}
  return detected_mime
end