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.



58
59
60
# File 'lib/aspera/preview/file_types.rb', line 58

def initialize
  @use_mimemagic = false
end

Instance Attribute Details

#use_mimemagicObject



56
57
58
# File 'lib/aspera/preview/file_types.rb', line 56

def use_mimemagic
  @use_mimemagic
end

Instance Method Details

#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



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/aspera/preview/file_types.rb', line 78

def conversion_type(filepath, mimetype)
  Log.log.debug{"conversion_type(#{filepath},mime=#{mimetype},magic=#{@use_mimemagic})"}
  mimetype = nil if mimetype.is_a?(String) && (mimetype == 'application/octet-stream' || mimetype.empty?)
  # Use mimemagic if available
  mimetype ||= mime_using_mimemagic(filepath)
  mimetype ||= mime_using_file(filepath)
  # from extensions, using local mapping
  mimetype ||= MIME::Types.of(File.basename(filepath)).first
  raise "no MIME type found for #{File.basename(filepath)}" if mimetype.nil?
  conversion_type = mime_to_type(mimetype)
  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

#mime_to_type(mimetype) ⇒ Object

Returns file type, one of enum CONVERSION_TYPES, or nil if not found.

Parameters:

  • mimetype (String)

    mime type

Returns:

  • file type, one of enum CONVERSION_TYPES, or nil if not found



64
65
66
67
68
69
70
71
72
# File 'lib/aspera/preview/file_types.rb', line 64

def mime_to_type(mimetype)
  Aspera.assert_type(mimetype, String)
  return SUPPORTED_MIME_TYPES[mimetype] if SUPPORTED_MIME_TYPES.key?(mimetype)
  return :office if mimetype.start_with?('application/vnd.ms-')
  return :office if mimetype.start_with?('application/vnd.openxmlformats-officedocument')
  return :video if mimetype.start_with?('video/')
  return :image if mimetype.start_with?('image/')
  return nil
end