Skip to content

Commit

Permalink
Merge pull request #289 from matthieuprat/lowercase-file-extensions
Browse files Browse the repository at this point in the history
Support file extensions in uppercase
  • Loading branch information
Mth0158 authored Nov 12, 2024
2 parents dbb8efc + eb1cff3 commit 9002bee
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/active_storage_validations/content_type_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def extension_matches_content_type?(record, attribute, attachable)

extension = @attachable_filename.split('.').last
possible_extensions = Marcel::TYPE_EXTS[@attachable_content_type]
return true if possible_extensions && extension.in?(possible_extensions)
return true if possible_extensions && extension.downcase.in?(possible_extensions)

errors_options = initialize_and_populate_error_options(options, attachable)
add_error(record, attribute, ERROR_TYPES.first, **errors_options)
Expand Down
4 changes: 4 additions & 0 deletions test/dummy/app/models/content_type/validator/check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ def self.example_for(type, several: false)
validates :extension_two_extensions_docx, content_type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
has_one_attached :extension_two_extensions_pdf
validates :extension_two_extensions_pdf, content_type: 'application/pdf'
has_one_attached :extension_upcase_extension
validates :extension_upcase_extension, content_type: 'application/pdf'
has_one_attached :extension_missing_extension
validates :extension_missing_extension, content_type: 'application/pdf'

%w(symbol string regex).each do |type|
has_one_attached :"with_#{type}"
Expand Down
35 changes: 35 additions & 0 deletions test/validators/content_type_validator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,41 @@
it { is_expected_to_be_valid }
end
end

describe "when the extension is in uppercase" do
subject { model.public_send(attribute).attach(pdf_file_with_extension_in_uppercase) and model }

let(:attribute) { :extension_upcase_extension }
let(:pdf_file_with_extension_in_uppercase) do
pdf_file.tap do |file|
file[:filename][".pdf"] = ".PDF"
end
end

it { is_expected_to_be_valid }
end

describe "when the extension is missing" do
subject { model.public_send(attribute).attach(pdf_file_without_extension) and model }

let(:attribute) { :extension_missing_extension }
let(:pdf_file_without_extension) do
pdf_file.tap do |file|
file[:filename][".pdf"] = ""
end
end
let(:error_options) do
{
authorized_types: "PDF",
content_type: pdf_file_without_extension[:content_type],
filename: pdf_file_without_extension[:filename]
}
end

it { is_expected_not_to_be_valid }
it { is_expected_to_have_error_message("content_type_invalid", error_options: error_options) }
it { is_expected_to_have_error_options(error_options) }
end
end

describe ":with" do
Expand Down

0 comments on commit 9002bee

Please sign in to comment.