Skip to content

Commit

Permalink
[Validator] Handle content_type with parameters properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Mth0158 committed Jan 2, 2025
1 parent c39c9d8 commit fd00852
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/active_storage_validations/content_type_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ def attachable_content_type_vs_detected_content_type_mismatch?

def attachable_content_type_intersects_detected_content_type?
# Ruby intersects? method is only available from 3.1
enlarged_content_type(@attachable_content_type).any? do |item|
enlarged_content_type(@detected_content_type).include?(item)
enlarged_content_type(content_type_without_parameters(@attachable_content_type)).any? do |item|
enlarged_content_type(content_type_without_parameters(@detected_content_type)).include?(item)
end
end

Expand Down
10 changes: 8 additions & 2 deletions lib/active_storage_validations/shared/asv_attachable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,15 @@ def full_attachable_content_type(attachable)
# Retrieve the declared content_type from attachable without potential mime
# type parameters (e.g. 'application/x-rar-compressed;version=5')
def attachable_content_type(attachable)
full_attachable_content_type(attachable) && full_attachable_content_type(attachable).downcase.split(/[;,\s]/, 2).first
full_attachable_content_type(attachable) && content_type_without_parameters(full_attachable_content_type(attachable))
end


# Remove the potential mime type parameters from the content_type (e.g.
# 'application/x-rar-compressed;version=5')
def content_type_without_parameters(content_type)
content_type && content_type.downcase.split(/[;,\s]/, 2).first
end

# Retrieve the content_type from attachable using the same logic as Rails
# ActiveStorage::Blob::Identifiable#identify_content_type
def attachable_content_type_rails_like(attachable)
Expand Down

0 comments on commit fd00852

Please sign in to comment.