class Aspera::Preview::FileTypes
function conversion_type
returns one of the types: CONVERSION_TYPES
Constants
- CONVERSION_TYPES
values for
conversion_type
: input format- SUPPORTED_EXTENSIONS
this is a way to add support for extensions that are otherwise not known by node api (mime type)
- SUPPORTED_MIME_TYPES
define how files are processed based on mime type
Attributes
use_mimemagic[RW]
@attr use_mimemagic
[bool] true to use mimemagic to determine real mime type based on file content
Public Class Methods
new()
click to toggle source
# File lib/aspera/preview/file_types.rb, line 280 def initialize @use_mimemagic=false end
Public Instance Methods
conversion_type(filepath,mimetype)
click to toggle source
return file type, one of enum CONVERSION_TYPES
@param filepath [String] full path to file @param mimetype [String] provided by node api
# File lib/aspera/preview/file_types.rb, line 305 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 conv_type=SUPPORTED_MIME_TYPES[mimetype] if ! mimetype.nil? # 2- else, from computed mime type (if available) if conv_type.nil? and @use_mimemagic detected_mime=mime_from_file(filepath) if ! detected_mime.nil? conv_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] conv_type=SUPPORTED_EXTENSIONS[extension] if conv_type.nil? Log.log.debug("conversion_type(#{extension}): #{conv_type.class.name} [#{conv_type}]") return conv_type end
mime_from_file(filepath)
click to toggle source
use mime magic to find mime type based on file content (magic numbers)
# File lib/aspera/preview/file_types.rb, line 285 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.has_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