diff options
author | George Claghorn <george@basecamp.com> | 2017-12-31 10:36:58 -0500 |
---|---|---|
committer | George Claghorn <george@basecamp.com> | 2017-12-31 10:36:58 -0500 |
commit | 316d87e412e1c6bc9c7a590b866befbb6bab167f (patch) | |
tree | 81bfdb8d0a2437d0ea94894a31ec743f64189522 /activestorage | |
parent | 5428c8e3a2d0f80240ac2e3e4b214b775ee2f346 (diff) | |
download | rails-316d87e412e1c6bc9c7a590b866befbb6bab167f.tar.gz rails-316d87e412e1c6bc9c7a590b866befbb6bab167f.tar.bz2 rails-316d87e412e1c6bc9c7a590b866befbb6bab167f.zip |
Append extension to tempfile name
Fixes analyzing an SVG image without an XML declaration. ImageMagick occasionally looks to the extension when it can't discern the type of an image file from its contents.
References #31356.
Diffstat (limited to 'activestorage')
-rw-r--r-- | activestorage/lib/active_storage/downloading.rb | 2 | ||||
-rw-r--r-- | activestorage/test/analyzer/image_analyzer_test.rb | 10 | ||||
-rw-r--r-- | activestorage/test/fixtures/files/icon.svg | 13 |
3 files changed, 23 insertions, 2 deletions
diff --git a/activestorage/lib/active_storage/downloading.rb b/activestorage/lib/active_storage/downloading.rb index 3dac6b116a..a57fda49b4 100644 --- a/activestorage/lib/active_storage/downloading.rb +++ b/activestorage/lib/active_storage/downloading.rb @@ -5,7 +5,7 @@ module ActiveStorage private # Opens a new tempfile in #tempdir and copies blob data into it. Yields the tempfile. def download_blob_to_tempfile # :doc: - Tempfile.open("ActiveStorage", tempdir) do |file| + Tempfile.open([ "ActiveStorage", blob.filename.extension_with_delimiter ], tempdir) do |file| download_blob_to file yield file end diff --git a/activestorage/test/analyzer/image_analyzer_test.rb b/activestorage/test/analyzer/image_analyzer_test.rb index 9087072215..0d9f24c5c1 100644 --- a/activestorage/test/analyzer/image_analyzer_test.rb +++ b/activestorage/test/analyzer/image_analyzer_test.rb @@ -6,11 +6,19 @@ require "database/setup" require "active_storage/analyzer/image_analyzer" class ActiveStorage::Analyzer::ImageAnalyzerTest < ActiveSupport::TestCase - test "analyzing an image" do + test "analyzing a JPEG image" do blob = create_file_blob(filename: "racecar.jpg", content_type: "image/jpeg") metadata = blob.tap(&:analyze).metadata assert_equal 4104, metadata[:width] assert_equal 2736, metadata[:height] end + + test "analyzing an SVG image without an XML declaration" do + blob = create_file_blob(filename: "icon.svg", content_type: "image/svg+xml") + metadata = blob.tap(&:analyze).metadata + + assert_equal 792, metadata[:width] + assert_equal 584, metadata[:height] + end end diff --git a/activestorage/test/fixtures/files/icon.svg b/activestorage/test/fixtures/files/icon.svg new file mode 100644 index 0000000000..6cfb0e241e --- /dev/null +++ b/activestorage/test/fixtures/files/icon.svg @@ -0,0 +1,13 @@ +<!-- The XML declaration is intentionally omitted. --> +<svg width="792px" height="584px" viewBox="0 0 792 584" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> + <path d="M9.51802657,28.724593 C9.51802657,18.2155955 18.1343454,9.60822622 28.6542694,9.60822622 L763.245541,9.60822622 C773.765465,9.60822622 782.381784,18.2155955 782.381784,28.724593 L782.381784,584 L792,584 L792,28.724593 C792,12.911054 779.075522,0 763.245541,0 L28.7544592,0 C12.9244782,0 0,12.911054 0,28.724593 L0,584 L9.61821632,584 C9.51802657,584 9.51802657,28.724593 9.51802657,28.724593 L9.51802657,28.724593 Z" id="Shape" opacity="0.3" fill="#CCCCCC"></path> + <circle id="Oval" fill="#FFCC33" cx="119.1" cy="147.2" r="33"></circle> + <circle id="Oval" fill="#3399FF" cx="119.1" cy="281.1" r="33"></circle> + <circle id="Oval" fill="#FF3333" cx="676.1" cy="376.8" r="33"></circle> + <circle id="Oval" fill="#FFCC33" cx="119.1" cy="477.2" r="33"></circle> + <rect id="Rectangle-path" opacity="0.75" fill="#CCCCCC" x="176.5" y="130.5" width="442.1" height="33.5"></rect> + <rect id="Rectangle-path" opacity="0.75" fill="#CCCCCC" x="176.5" y="183.1" width="442.1" height="33.5"></rect> + <rect id="Rectangle-path" opacity="0.75" fill="#CCCCCC" x="176.5" y="265.8" width="442.1" height="33.5"></rect> + <rect id="Rectangle-path" opacity="0.75" fill="#CCCCCC" x="176.5" y="363.4" width="442.1" height="33.5"></rect> + <rect id="Rectangle-path" opacity="0.75" fill="#CCCCCC" x="176.5" y="465.3" width="442.1" height="33.5"></rect> +</svg> |