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
604304e3
Commit
604304e3
authored
Jan 27, 2012
by
Prem Sichanugrist
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Replacing all special characters to underscore
Fixes #703
parent
ed5cd9f1
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
47 additions
and
18 deletions
+47
-18
ChangeLog
+7
-0
lib/paperclip/attachment.rb
+4
-1
test/attachment_test.rb
+20
-1
test/storage/filesystem_test.rb
+4
-4
test/storage/s3_live_test.rb
+4
-4
test/storage/s3_test.rb
+8
-8
No files found.
ChangeLog
View file @
604304e3
2012-01-27 Prem Sichanugrist <psichanugrist@thoughtbot.com>
* Paperclip will now replace all the special characters in the filename to an
underscore. This is a more desired behavior against having to deal with URL
escaping and unescaping later. You can see the list of blacklist characters
in `lib/paperclip/attachment.rb`
2012-01-20 Jon Yurek <jyurek@thoughtbot.com>
2012-01-20 Jon Yurek <jyurek@thoughtbot.com>
* lib/paperclip/railtie.rb (insert): Hide ActiveRecord-specific stuff in
* lib/paperclip/railtie.rb (insert): Hide ActiveRecord-specific stuff in
...
...
lib/paperclip/attachment.rb
View file @
604304e3
...
@@ -108,7 +108,7 @@ module Paperclip
...
@@ -108,7 +108,7 @@ module Paperclip
uploaded_filename
||=
uploaded_file
.
original_filename
uploaded_filename
||=
uploaded_file
.
original_filename
stores_fingerprint
=
@instance
.
respond_to?
(
"
#{
name
}
_fingerprint"
.
to_sym
)
stores_fingerprint
=
@instance
.
respond_to?
(
"
#{
name
}
_fingerprint"
.
to_sym
)
@queued_for_write
[
:original
]
=
to_tempfile
(
uploaded_file
)
@queued_for_write
[
:original
]
=
to_tempfile
(
uploaded_file
)
instance_write
(
:file_name
,
uploaded_filename
.
strip
)
instance_write
(
:file_name
,
cleanup_filename
(
uploaded_filename
.
strip
)
)
instance_write
(
:content_type
,
uploaded_file
.
content_type
.
to_s
.
strip
)
instance_write
(
:content_type
,
uploaded_file
.
content_type
.
to_s
.
strip
)
instance_write
(
:file_size
,
uploaded_file
.
size
.
to_i
)
instance_write
(
:file_size
,
uploaded_file
.
size
.
to_i
)
instance_write
(
:fingerprint
,
generate_fingerprint
(
uploaded_file
))
if
stores_fingerprint
instance_write
(
:fingerprint
,
generate_fingerprint
(
uploaded_file
))
if
stores_fingerprint
...
@@ -478,5 +478,8 @@ module Paperclip
...
@@ -478,5 +478,8 @@ module Paperclip
end
end
end
end
def
cleanup_filename
(
filename
)
filename
.
gsub
(
/[&$+,\/:;=?@<>\[\]\{\}\|\\\^~%# ]/
,
'_'
)
end
end
end
end
end
test/attachment_test.rb
View file @
604304e3
...
@@ -666,7 +666,7 @@ class AttachmentTest < Test::Unit::TestCase
...
@@ -666,7 +666,7 @@ class AttachmentTest < Test::Unit::TestCase
context
"Assigning an attachment"
do
context
"Assigning an attachment"
do
setup
do
setup
do
rebuild_model
:styles
=>
{
:something
=>
"100x100#"
}
rebuild_model
:styles
=>
{
:something
=>
"100x100#"
}
@file
=
StringIO
.
new
(
"."
)
@file
=
StringIO
.
new
(
"."
)
@file
.
stubs
(
:original_filename
).
returns
(
"5k.png
\n\n
"
)
@file
.
stubs
(
:original_filename
).
returns
(
"5k.png
\n\n
"
)
@file
.
stubs
(
:content_type
).
returns
(
"image/png
\n\n
"
)
@file
.
stubs
(
:content_type
).
returns
(
"image/png
\n\n
"
)
@file
.
stubs
(
:to_tempfile
).
returns
(
@file
)
@file
.
stubs
(
:to_tempfile
).
returns
(
@file
)
...
@@ -713,6 +713,25 @@ class AttachmentTest < Test::Unit::TestCase
...
@@ -713,6 +713,25 @@ class AttachmentTest < Test::Unit::TestCase
end
end
end
end
context
"Attachment with reserved filename"
do
"&$+,/:;=?@<>[]{}|
\^
~%# "
.
split
(
//
).
each
do
|
character
|
context
"with character
#{
character
}
"
do
setup
do
rebuild_model
file
=
StringIO
.
new
(
"."
)
file
.
stubs
(
:original_filename
).
returns
(
"file
#{
character
}
name.png"
)
@dummy
=
Dummy
.
new
@dummy
.
avatar
=
file
end
should
"convert special character into underscore"
do
assert_equal
"file_name.png"
,
@dummy
.
avatar
.
original_filename
end
end
end
end
context
"Attachment with uppercase extension and a default style"
do
context
"Attachment with uppercase extension and a default style"
do
setup
do
setup
do
@old_defaults
=
Paperclip
::
Attachment
.
default_options
.
dup
@old_defaults
=
Paperclip
::
Attachment
.
default_options
.
dup
...
...
test/storage/filesystem_test.rb
View file @
604304e3
...
@@ -44,12 +44,12 @@ class FileSystemTest < Test::Unit::TestCase
...
@@ -44,12 +44,12 @@ class FileSystemTest < Test::Unit::TestCase
assert
File
.
exists?
(
@dummy
.
avatar
.
path
)
assert
File
.
exists?
(
@dummy
.
avatar
.
path
)
end
end
should
"
store the path unescaped
"
do
should
"
return a replaced version for path
"
do
assert_match
/
\/spaced
file\.png/
,
@dummy
.
avatar
.
path
assert_match
/
.+\/spaced_
file\.png/
,
@dummy
.
avatar
.
path
end
end
should
"return a
n escaped version of URL
"
do
should
"return a
replaced version for url
"
do
assert_match
/
\/spaced%20
file\.png/
,
@dummy
.
avatar
.
url
assert_match
/
.+\/spaced_
file\.png/
,
@dummy
.
avatar
.
url
end
end
end
end
end
end
...
...
test/storage/s3_live_test.rb
View file @
604304e3
...
@@ -80,12 +80,12 @@ unless ENV["S3_BUCKET"].blank?
...
@@ -80,12 +80,12 @@ unless ENV["S3_BUCKET"].blank?
@dummy
.
save
@dummy
.
save
end
end
should
"return a
n unescap
ed version for path"
do
should
"return a
replac
ed version for path"
do
assert_match
/.+\/spaced
file\.png/
,
@dummy
.
avatar
.
path
assert_match
/.+\/spaced
_
file\.png/
,
@dummy
.
avatar
.
path
end
end
should
"return a
n escap
ed version for url"
do
should
"return a
replac
ed version for url"
do
assert_match
/.+\/spaced
%20
file\.png/
,
@dummy
.
avatar
.
url
assert_match
/.+\/spaced
_
file\.png/
,
@dummy
.
avatar
.
url
end
end
should
"be accessible"
do
should
"be accessible"
do
...
...
test/storage/s3_test.rb
View file @
604304e3
...
@@ -222,12 +222,12 @@ class S3Test < Test::Unit::TestCase
...
@@ -222,12 +222,12 @@ class S3Test < Test::Unit::TestCase
@dummy
.
avatar
=
File
.
new
(
fixture_file
(
'spaced file.png'
),
'rb'
)
@dummy
.
avatar
=
File
.
new
(
fixture_file
(
'spaced file.png'
),
'rb'
)
end
end
should
"return a
n unescap
ed version for path"
do
should
"return a
replac
ed version for path"
do
assert_match
/.+\/spaced
file\.png/
,
@dummy
.
avatar
.
path
assert_match
/.+\/spaced
_
file\.png/
,
@dummy
.
avatar
.
path
end
end
should
"return a
n escap
ed version for url"
do
should
"return a
replac
ed version for url"
do
assert_match
/.+\/spaced
%20
file\.png/
,
@dummy
.
avatar
.
url
assert_match
/.+\/spaced
_
file\.png/
,
@dummy
.
avatar
.
url
end
end
end
end
...
@@ -248,12 +248,12 @@ class S3Test < Test::Unit::TestCase
...
@@ -248,12 +248,12 @@ class S3Test < Test::Unit::TestCase
@dummy
.
save
@dummy
.
save
end
end
should
"return a
n unescap
ed version for path"
do
should
"return a
replac
ed version for path"
do
assert_match
/.+\/question
\?
mark\.png/
,
@dummy
.
avatar
.
path
assert_match
/.+\/question
_
mark\.png/
,
@dummy
.
avatar
.
path
end
end
should
"return a
n escap
ed version for url"
do
should
"return a
replac
ed version for url"
do
assert_match
/.+\/question
%3F
mark\.png/
,
@dummy
.
avatar
.
url
assert_match
/.+\/question
_
mark\.png/
,
@dummy
.
avatar
.
url
end
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