From 316d87e412e1c6bc9c7a590b866befbb6bab167f Mon Sep 17 00:00:00 2001 From: George Claghorn Date: Sun, 31 Dec 2017 10:36:58 -0500 Subject: 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. --- activestorage/lib/active_storage/downloading.rb | 2 +- activestorage/test/analyzer/image_analyzer_test.rb | 10 +++++++++- activestorage/test/fixtures/files/icon.svg | 13 +++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 activestorage/test/fixtures/files/icon.svg 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 @@ + + + + + + + + + + + + + -- cgit v1.2.3