Skip to content

Commit

Permalink
ContentTypeDetector, Bump Version 5.1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Kim authored and Peter Kim committed Feb 23, 2022
1 parent e5a84d4 commit 28a4d21
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@

## NEXT

## Ckeditor 5.1.4 (2-23-2022)

* Re-add ContentTypeDetector

## Ckeditor 5.1.3 (2-23-2022)

* Add extract_dimensions and extract_content_type as AssetResponse InstanceMethods
* Re-add extract_dimensions and extract_content_type as AssetResponse InstanceMethods

## Ckeditor 5.1.2 (2-23-2022)

Expand Down
1 change: 1 addition & 0 deletions lib/ckeditor/backend/carrierwave.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def self.included(base)
module ClassMethods
def self.extended(base)
base.class_eval do
process :extract_content_type
process :extract_size
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/ckeditor/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
module Ckeditor
module Utils
autoload :JavascriptCode, 'ckeditor/utils/javascript_code'
autoload :ContentTypeDetector, 'ckeditor/utils/content_type_detector'

class << self
def escape_single_quotes(str)
Expand Down
38 changes: 38 additions & 0 deletions lib/ckeditor/utils/content_type_detector.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# encoding: utf-8
require 'cocaine'

module Ckeditor
module Utils
class ContentTypeDetector
EMPTY_CONTENT_TYPE = 'inode/x-empty'.freeze
DEFAULT_CONTENT_TYPE = 'application/octet-stream'.freeze

def initialize(file_path)
@file_path = file_path
end

# 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'

def detect
empty_file? ? EMPTY_CONTENT_TYPE : content_type_from_file_command
end

private

def empty_file?
return true if @file_path.blank?
File.exist?(@file_path) && File.size(@file_path).zero?
end

def content_type_from_file_command
Cocaine::CommandLine.new('file', '-b --mime-type :file').run(file: @file_path).strip
rescue Cocaine::CommandLineError
DEFAULT_CONTENT_TYPE
end
end
end
end
2 changes: 1 addition & 1 deletion lib/ckeditor/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Ckeditor
module Version
GEM = '5.1.3'
GEM = '5.1.4'
EDITOR = '4.17.0'
end
end

0 comments on commit 28a4d21

Please sign in to comment.