Commit 38a6b4dc by Marco Otte-Witte Committed by Jon Yurek

added handler for (base64) data URIs

parent d4f43139
......@@ -186,6 +186,7 @@ require 'paperclip/io_adapters/empty_string_adapter'
require 'paperclip/io_adapters/identity_adapter'
require 'paperclip/io_adapters/file_adapter'
require 'paperclip/io_adapters/stringio_adapter'
require 'paperclip/io_adapters/data_uri_adapter'
require 'paperclip/io_adapters/nil_adapter'
require 'paperclip/io_adapters/attachment_adapter'
require 'paperclip/io_adapters/uploaded_file_adapter'
......
module Paperclip
class DataUriAdapter < StringioAdapter
REGEXP = /^data:([-\w]+\/[-\w\+]+);base64,(.*)/
def initialize(target)
@data_uri_parts = target.match(REGEXP) || []
deserialize
cache_current_values
@tempfile = copy_to_tempfile
end
private
def cache_current_values
self.original_filename = 'base64.txt'
@content_type = @data_uri_parts[1]
@content_type ||= 'text/plain'
@size = @target.size
end
def deserialize
@target = StringIO.new(Base64.decode64(@data_uri_parts[2]))
end
end
end
Paperclip.io_adapters.register Paperclip::DataUriAdapter do |target|
String === target && target =~ Paperclip::DataUriAdapter::REGEXP
end
......@@ -3,7 +3,7 @@ module Paperclip
def initialize(target)
@target = target
cache_current_values
@tempfile = copy_to_tempfile(@target)
@tempfile = copy_to_tempfile
end
attr_writer :content_type
......@@ -21,8 +21,8 @@ module Paperclip
@size = @target.size
end
def copy_to_tempfile(src)
while data = src.read(16*1024)
def copy_to_tempfile
while data = @target.read(16*1024)
destination.write(data)
end
destination.rewind
......
require './test/helper'
class DataUriAdapterTest < Test::Unit::TestCase
context "a new instance" do
setup do
@contents = "data:image/png;base64,dGVzdA=="
@subject = Paperclip.io_adapters.for(@contents)
end
should "return a file name" do
assert_equal "base64.txt", @subject.original_filename
end
should "return a content type" do
assert_equal "image/png", @subject.content_type
end
should "return the size of the data" do
assert_equal 4, @subject.size
end
should "generate an MD5 hash of the contents" do
assert_equal Digest::MD5.hexdigest(Base64.decode64('dGVzdA==')), @subject.fingerprint
end
should "generate correct fingerprint after read" do
fingerprint = Digest::MD5.hexdigest(@subject.read)
assert_equal fingerprint, @subject.fingerprint
end
should "generate same fingerprint" do
assert_equal @subject.fingerprint, @subject.fingerprint
end
should "return the data contained in the StringIO" do
assert_equal "test", @subject.read
end
should 'accept a content_type' do
@subject.content_type = 'image/png'
assert_equal 'image/png', @subject.content_type
end
should 'accept an original_filename' do
@subject.original_filename = 'image.png'
assert_equal 'image.png', @subject.original_filename
end
should "not generate filenames that include restricted characters" do
@subject.original_filename = 'image:restricted.png'
assert_equal 'image_restricted.png', @subject.original_filename
end
should "not generate paths that include restricted characters" do
@subject.original_filename = 'image:restricted.png'
assert_no_match /:/, @subject.path
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