Class: Aspera::Preview::FileTypes
- Inherits:
-
Object
- Object
- Aspera::Preview::FileTypes
- 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
-
#conversion_type(filepath, mimetype) ⇒ Object
File type, one of enum CONVERSION_TYPES.
-
#file_to_mime(filepath) ⇒ Object
use mime magic to find mime type based on file content (magic numbers).
-
#initialize ⇒ FileTypes
constructor
A new instance of FileTypes.
-
#mime_to_type(mimetype) ⇒ Object
File type, one of enum CONVERSION_TYPES.
Constructor Details
#initialize ⇒ FileTypes
Returns a new instance of FileTypes.
54 55 56 |
# File 'lib/aspera/preview/file_types.rb', line 54 def initialize @use_mimemagic = false end |
Instance Attribute Details
#use_mimemagic ⇒ Object
52 53 54 |
# File 'lib/aspera/preview/file_types.rb', line 52 def use_mimemagic @use_mimemagic end |
Instance Method Details
#conversion_type(filepath, mimetype) ⇒ Object
Returns file type, one of enum CONVERSION_TYPES.
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 |
#file_to_mime(filepath) ⇒ Object
use mime magic to find mime type based on file content (magic numbers)
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/aspera/preview/file_types.rb', line 69 def file_to_mime(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 mime_to_type(detected_mime).nil? 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 |
#mime_to_type(mimetype) ⇒ Object
Returns file type, one of enum CONVERSION_TYPES.
60 61 62 63 64 65 66 |
# File 'lib/aspera/preview/file_types.rb', line 60 def mime_to_type(mimetype) return SUPPORTED_MIME_TYPES[mimetype] if SUPPORTED_MIME_TYPES.key?(mimetype) return :office if mimetype.start_with?('application/vnd.') return :video if mimetype.start_with?('video/') return :image if mimetype.start_with?('image/') return nil end |