Skip to content

Commit

Permalink
Get name and extension from Pathname
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier Bellone authored and olivier-thatch committed Oct 12, 2023
1 parent 12fc8da commit 37b511d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/marcel/mime_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ 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)
if defined?(Pathname) && pathname_or_io.is_a?(Pathname)
name ||= pathname_or_io.basename.to_s
extension ||= pathname_or_io.extname[1..-1]
end

type_from_data = for_data(pathname_or_io)
fallback_type = for_declared_type(declared_type) || for_name(name) || for_extension(extension) || BINARY

Expand Down
4 changes: 4 additions & 0 deletions test/magic_and_name_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@ class Marcel::MimeType::MagicAndNameTest < Marcel::TestCase
test "correctly returns #{content_type} for #{name} given both file and name" do
assert_equal content_type, Marcel::MimeType.for(file, name: name)
end

test "correctly returns #{content_type} for #{name} given Pathname without explicit name" do
assert_equal content_type, Marcel::MimeType.for(file)
end
end
end

0 comments on commit 37b511d

Please sign in to comment.