Skip to content

Commit

Permalink
Support file extension names both as symbols and strings for :content…
Browse files Browse the repository at this point in the history
…_type_mappings (#119)
  • Loading branch information
fatkodima committed Jul 20, 2023
1 parent 93a1400 commit 8e3cd82
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
3 changes: 2 additions & 1 deletion NEWS
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[UNRELEASED]
Issue file delete only once per unique style when nullifying attachment or destroying an object. Avoids triggering a rate limit error on Google Cloud Storage.
* Improvement: Support file extension names both as symbols and strings for :content_type_mappings
* Issue file delete only once per unique style when nullifying attachment or destroying an object. Avoids triggering a rate limit error on Google Cloud Storage.

7.0.0 (2021-05-28)
* Replace `mimemagic` gem with `marcel` due to licensing issues. See https://github.com/kreeti/kt-paperclip/pull/54 for details and limitations
Expand Down
3 changes: 2 additions & 1 deletion lib/paperclip/media_type_spoof_detector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ def type_from_file_command
end

def mapped_content_type
Paperclip.options[:content_type_mappings][filename_extension]
content_type_mappings = Paperclip.options[:content_type_mappings]
content_type_mappings[filename_extension] || content_type_mappings[filename_extension.to_s]
end

def filename_extension
Expand Down
14 changes: 10 additions & 4 deletions spec/paperclip/media_type_spoof_detector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,18 @@
end

it "does not reject when the extension => content_type is in :content_type_mappings" do
file = Tempfile.open(["test", ".PEM"])
file.puts "Certificate!"
file.close

adapter = Paperclip.io_adapters.for(File.new(file.path))

begin
Paperclip.options[:content_type_mappings] = { pem: "text/plain" }
file = Tempfile.open(["test", ".PEM"])
file.puts "Certificate!"
file.close
adapter = Paperclip.io_adapters.for(File.new(file.path))
assert !Paperclip::MediaTypeSpoofDetector.using(adapter, adapter.original_filename, adapter.content_type).spoofed?

# As a string.
Paperclip.options[:content_type_mappings] = { "pem" => "text/plain" }
assert !Paperclip::MediaTypeSpoofDetector.using(adapter, adapter.original_filename, adapter.content_type).spoofed?
ensure
Paperclip.options[:content_type_mappings] = {}
Expand Down

0 comments on commit 8e3cd82

Please sign in to comment.