Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Illustrator detection as application/pdf instead of application/illustrator #94

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 6 additions & 26 deletions lib/marcel/mime_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,8 @@ def extend(type, extensions: [], parents: [], magic: nil)
#
# If no type can be determined, then +application/octet-stream+ is returned.
def for(pathname_or_io = nil, name: nil, extension: nil, declared_type: nil)
type_from_data = for_data(pathname_or_io)
fallback_type = for_declared_type(declared_type) || for_name(name) || for_extension(extension) || BINARY

if type_from_data
most_specific_type type_from_data, fallback_type
else
fallback_type
end
filename_type = for_name(name) || for_extension(extension)
most_specific_type for_data(pathname_or_io), for_declared_type(declared_type), filename_type, BINARY
end

private
Expand Down Expand Up @@ -66,11 +60,7 @@ def for_extension(extension)
end

def for_declared_type(declared_type)
type = parse_media_type(declared_type)

if type != BINARY && !type.nil?
type.downcase
end
parse_media_type(declared_type)
end

def with_io(pathname_or_io, &block)
Expand All @@ -91,19 +81,9 @@ def parse_media_type(content_type)
# For some document types (notably Microsoft Office) we recognise the main content
# type with magic, but not the specific subclass. In this situation, if we can get a more
# specific class using either the name or declared_type, we should use that in preference
def most_specific_type(from_magic_type, fallback_type)
if (root_types(from_magic_type) & root_types(fallback_type)).any?
fallback_type
else
from_magic_type
end
end

def root_types(type)
if TYPE_EXTS[type].nil? || TYPE_PARENTS[type].nil?
[ type ]
else
TYPE_PARENTS[type].map {|t| root_types t }.flatten
def most_specific_type(*candidates)
candidates.compact.uniq.reduce do |type, candidate|
Marcel::Magic.child?(candidate, type) ? candidate : type
end
end
end
Expand Down
9 changes: 9 additions & 0 deletions test/illustrator_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'test_helper'
require 'rack'

class Marcel::MimeType::IllustratorTest < Marcel::TestCase
test ".ai uploaded as application/postscript" do
file = files("name/application/illustrator/illustrator.ai")
assert_equal "application/illustrator", Marcel::MimeType.for(file, name: "illustrator.ai", declared_type: "application/postscript")
end
end