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

Store MIME parents in a distinct Hash #72

Merged
merged 1 commit into from
May 10, 2022
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
15 changes: 8 additions & 7 deletions lib/marcel/magic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ def initialize(type)
# * <i>:comment</i>: Comment string
def self.add(type, options)
extensions = [options[:extensions]].flatten.compact
TYPES[type] = [extensions,
[options[:parents]].flatten.compact,
options[:comment]]
TYPE_EXTS[type] = extensions
parents = [options[:parents]].flatten.compact
TYPE_PARENTS[type] = parents unless parents.empty?
extensions.each {|ext| EXTENSIONS[ext] = type }
MAGIC.unshift [type, options[:magic]] if options[:magic]
end
Expand All @@ -42,7 +42,8 @@ def self.add(type, options)
def self.remove(type)
EXTENSIONS.delete_if {|ext, t| t == type }
MAGIC.delete_if {|t, m| t == type }
TYPES.delete(type)
TYPE_EXTS.delete(type)
TYPE_PARENTS.delete(type)
end

# Returns true if type is a text format
Expand All @@ -60,12 +61,12 @@ def child_of?(parent)

# Get string list of file extensions
def extensions
TYPES.key?(type) ? TYPES[type][0] : []
TYPE_EXTS[type] || []
end

# Get mime comment
def comment
(TYPES.key?(type) ? TYPES[type][2] : nil).to_s
nil # deprecated
end

# Lookup mime type by file extension
Expand Down Expand Up @@ -110,7 +111,7 @@ def hash
alias == eql?

def self.child?(child, parent)
child == parent || TYPES.key?(child) && TYPES[child][1].any? {|p| child?(p, parent) }
child == parent || TYPE_PARENTS[child]&.any? {|p| child?(p, parent) }
end

def self.magic_match(io, method)
Expand Down
15 changes: 6 additions & 9 deletions lib/marcel/mime_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@ class MimeType

class << self
def extend(type, extensions: [], parents: [], magic: nil)
existing = Marcel::TYPES[type] || [[], [], ""]

extensions = (Array(extensions) + existing[0]).uniq
parents = (Array(parents) + existing[1]).uniq
comment = existing[2]

Magic.add(type, extensions: extensions, magic: magic, parents: parents, comment: comment)
extensions = (Array(extensions) + Array(Marcel::TYPE_EXTS[type])).uniq
parents = (Array(parents) + Array(Marcel::TYPE_PARENTS[type])).uniq
Magic.add(type, extensions: extensions, magic: magic, parents: parents)
end

# Returns the most appropriate content type for the given file.
Expand Down Expand Up @@ -42,6 +38,7 @@ def for(pathname_or_io = nil, name: nil, extension: nil, declared_type: nil)
end

private

def for_data(pathname_or_io)
if pathname_or_io
with_io(pathname_or_io) do |io|
Expand Down Expand Up @@ -103,10 +100,10 @@ def most_specific_type(from_magic_type, fallback_type)
end

def root_types(type)
if TYPES[type].nil? || TYPES[type][1].empty?
if TYPE_EXTS[type].nil? || TYPE_PARENTS[type].nil?
[ type ]
else
TYPES[type][1].map {|t| root_types t }.flatten
TYPE_PARENTS[type].map {|t| root_types t }.flatten
end
end
end
Expand Down
Loading