class Ckeditor::Utils::ContentTypeDetector

Constants

DEFAULT_CONTENT_TYPE
EMPTY_CONTENT_TYPE

Public Class Methods

new(file_path) click to toggle source
# File lib/ckeditor/utils/content_type_detector.rb, line 10
def initialize(file_path)
  @file_path = file_path
end

Public Instance Methods

detect() click to toggle source

content type detection strategy:

  1. empty file: returns 'inode/x-empty'

  2. nonempty file: if the file is not empty then returns the content type using file command

  3. invalid file: file command raises error and returns 'application/octet-stream'

# File lib/ckeditor/utils/content_type_detector.rb, line 20
def detect
  empty_file? ? EMPTY_CONTENT_TYPE : content_type_from_file_command
end

Private Instance Methods

content_type_from_file_command() click to toggle source
# File lib/ckeditor/utils/content_type_detector.rb, line 31
def content_type_from_file_command
  type = begin
    Cocaine::CommandLine.new('file', '-b --mime-type :file').run(file: @file_path)
  rescue Cocaine::CommandLineError => e
    # TODO: log command failure
    DEFAULT_CONTENT_TYPE
  end.strip
end
empty_file?() click to toggle source
# File lib/ckeditor/utils/content_type_detector.rb, line 26
def empty_file?
  return true if @file_path.blank?
  File.exists?(@file_path) && File.size(@file_path) == 0
end