Commit c594c249 by Dan Collis-Puro

Fix Paperclip::DataUriAdapter to handle multiline base64

Base64 encoded data-uris can contain newlines, make sure we handle
them correctly.
parent a12dec10
module Paperclip module Paperclip
class DataUriAdapter < StringioAdapter class DataUriAdapter < StringioAdapter
REGEXP = /^data:([-\w]+\/[-\w\+]+);base64,(.*)/ REGEXP = /^data:([-\w]+\/[-\w\+]+);base64,(.*)/m
def initialize(target_uri) def initialize(target_uri)
@target_uri = target_uri @target_uri = target_uri
......
...@@ -3,7 +3,7 @@ require './test/helper' ...@@ -3,7 +3,7 @@ require './test/helper'
class DataUriAdapterTest < Test::Unit::TestCase class DataUriAdapterTest < Test::Unit::TestCase
context "a new instance" do context "a new instance" do
setup do setup do
@contents = "data:image/png;base64,dGVzdA==" @contents = "data:image/png;base64,#{original_base64_content}"
@subject = Paperclip.io_adapters.for(@contents) @subject = Paperclip.io_adapters.for(@contents)
end end
...@@ -16,11 +16,14 @@ class DataUriAdapterTest < Test::Unit::TestCase ...@@ -16,11 +16,14 @@ class DataUriAdapterTest < Test::Unit::TestCase
end end
should "return the size of the data" do should "return the size of the data" do
assert_equal 4, @subject.size assert_equal 4456, @subject.size
end end
should "generate an MD5 hash of the contents" do should "generate a correct MD5 hash of the contents" do
assert_equal Digest::MD5.hexdigest(Base64.decode64('dGVzdA==')), @subject.fingerprint assert_equal(
Digest::MD5.hexdigest(Base64.decode64(original_base64_content)),
@subject.fingerprint
)
end end
should "generate correct fingerprint after read" do should "generate correct fingerprint after read" do
...@@ -32,10 +35,6 @@ class DataUriAdapterTest < Test::Unit::TestCase ...@@ -32,10 +35,6 @@ class DataUriAdapterTest < Test::Unit::TestCase
assert_equal @subject.fingerprint, @subject.fingerprint assert_equal @subject.fingerprint, @subject.fingerprint
end end
should "return the data contained in the StringIO" do
assert_equal "test", @subject.read
end
should 'accept a content_type' do should 'accept a content_type' do
@subject.content_type = 'image/png' @subject.content_type = 'image/png'
assert_equal 'image/png', @subject.content_type assert_equal 'image/png', @subject.content_type
...@@ -57,4 +56,12 @@ class DataUriAdapterTest < Test::Unit::TestCase ...@@ -57,4 +56,12 @@ class DataUriAdapterTest < Test::Unit::TestCase
end end
end end
def original_base64_content
Base64.encode64(original_file_contents)
end
def original_file_contents
@original_file_contents ||= File.read(fixture_file('5k.png'))
end
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment