Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
paperclip
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ikcrm_common
paperclip
Commits
38a6b4dc
Commit
38a6b4dc
authored
Jul 18, 2013
by
Marco Otte-Witte
Committed by
Jon Yurek
Jul 18, 2013
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added handler for (base64) data URIs
parent
d4f43139
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
97 additions
and
3 deletions
+97
-3
lib/paperclip.rb
+1
-0
lib/paperclip/io_adapters/data_uri_adapter.rb
+33
-0
lib/paperclip/io_adapters/stringio_adapter.rb
+3
-3
test/io_adapters/data_uri_adapter_test.rb
+60
-0
No files found.
lib/paperclip.rb
View file @
38a6b4dc
...
@@ -186,6 +186,7 @@ require 'paperclip/io_adapters/empty_string_adapter'
...
@@ -186,6 +186,7 @@ require 'paperclip/io_adapters/empty_string_adapter'
require
'paperclip/io_adapters/identity_adapter'
require
'paperclip/io_adapters/identity_adapter'
require
'paperclip/io_adapters/file_adapter'
require
'paperclip/io_adapters/file_adapter'
require
'paperclip/io_adapters/stringio_adapter'
require
'paperclip/io_adapters/stringio_adapter'
require
'paperclip/io_adapters/data_uri_adapter'
require
'paperclip/io_adapters/nil_adapter'
require
'paperclip/io_adapters/nil_adapter'
require
'paperclip/io_adapters/attachment_adapter'
require
'paperclip/io_adapters/attachment_adapter'
require
'paperclip/io_adapters/uploaded_file_adapter'
require
'paperclip/io_adapters/uploaded_file_adapter'
...
...
lib/paperclip/io_adapters/data_uri_adapter.rb
0 → 100644
View file @
38a6b4dc
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
lib/paperclip/io_adapters/stringio_adapter.rb
View file @
38a6b4dc
...
@@ -3,7 +3,7 @@ module Paperclip
...
@@ -3,7 +3,7 @@ module Paperclip
def
initialize
(
target
)
def
initialize
(
target
)
@target
=
target
@target
=
target
cache_current_values
cache_current_values
@tempfile
=
copy_to_tempfile
(
@target
)
@tempfile
=
copy_to_tempfile
end
end
attr_writer
:content_type
attr_writer
:content_type
...
@@ -21,8 +21,8 @@ module Paperclip
...
@@ -21,8 +21,8 @@ module Paperclip
@size
=
@target
.
size
@size
=
@target
.
size
end
end
def
copy_to_tempfile
(
src
)
def
copy_to_tempfile
while
data
=
src
.
read
(
16
*
1024
)
while
data
=
@target
.
read
(
16
*
1024
)
destination
.
write
(
data
)
destination
.
write
(
data
)
end
end
destination
.
rewind
destination
.
rewind
...
...
test/io_adapters/data_uri_adapter_test.rb
0 → 100644
View file @
38a6b4dc
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment