Skip to content

Commit

Permalink
Improve content type detection for .dotx and .ai
Browse files Browse the repository at this point in the history
To correctly detect content type of file in some cases file
extension should be used in addition to magic detection.
Such cases include custom extensions with .zip contents,
.dotx / .docx files which have same magic signature.
  • Loading branch information
dzhikvas committed Sep 12, 2023
1 parent 8815592 commit b7a9edc
Show file tree
Hide file tree
Showing 4 changed files with 1,710 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/carrierwave/sanitized_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,16 @@ def guessed_safe_content_type

def identified_content_type
with_io do |io|
Marcel::Magic.by_magic(io).try(:type)
mimetype_by_magic = Marcel::Magic.by_magic(io)
mimetype_by_path = Marcel::Magic.by_path(path)

return nil if mimetype_by_magic.nil?

if mimetype_by_path&.child_of?(mimetype_by_magic.type)
mimetype_by_path.type
else
mimetype_by_magic.type
end
end
rescue Errno::ENOENT
nil
Expand Down
Loading

0 comments on commit b7a9edc

Please sign in to comment.