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
a5427dc3
Commit
a5427dc3
authored
Apr 30, 2013
by
Max Melentiev
Committed by
Jon Yurek
Jul 18, 2013
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ability to define custom sanitizers for filenames
parent
8b0f6d34
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
49 additions
and
9 deletions
+49
-9
lib/paperclip/attachment.rb
+15
-9
test/attachment_test.rb
+34
-0
No files found.
lib/paperclip/attachment.rb
View file @
a5427dc3
...
...
@@ -14,6 +14,7 @@ module Paperclip
:default_url
=>
"/:attachment/:style/missing.png"
,
:escape_url
=>
true
,
:restricted_characters
=>
/[&$+,\/:;=?@<>\[\]\{\}\|\\\^~%# ]/
,
:filename_sanitizer
=>
nil
,
:hash_data
=>
":class/:attachment/:id/:style/:updated_at"
,
:hash_digest
=>
"SHA1"
,
:interpolator
=>
Paperclip
::
Interpolations
,
...
...
@@ -98,7 +99,12 @@ module Paperclip
return
nil
if
file
.
nil?
@queued_for_write
[
:original
]
=
file
instance_write
(
:file_name
,
cleanup_filename
(
file
.
original_filename
))
cleaned_filename
=
if
@options
[
:filename_sanitizer
]
@options
[
:filename_sanitizer
].
call
(
file
.
original_filename
,
self
)
else
cleanup_filename
(
file
.
original_filename
)
end
instance_write
(
:file_name
,
cleaned_filename
)
instance_write
(
:content_type
,
file
.
content_type
.
to_s
.
strip
)
instance_write
(
:file_size
,
file
.
size
)
instance_write
(
:fingerprint
,
file
.
fingerprint
)
if
instance_respond_to?
(
:fingerprint
)
...
...
@@ -360,6 +366,14 @@ module Paperclip
end
end
def
cleanup_filename
(
filename
)
if
@options
[
:restricted_characters
]
filename
.
gsub
(
@options
[
:restricted_characters
],
'_'
)
else
filename
end
end
private
def
path_option
...
...
@@ -480,14 +494,6 @@ module Paperclip
end
end
def
cleanup_filename
(
filename
)
if
@options
[
:restricted_characters
]
filename
.
gsub
(
@options
[
:restricted_characters
],
'_'
)
else
filename
end
end
# Check if attachment database table has a created_at field
def
able_to_store_created_at?
@instance
.
respond_to?
(
"
#{
name
}
_created_at"
.
to_sym
)
...
...
test/attachment_test.rb
View file @
a5427dc3
...
...
@@ -916,6 +916,40 @@ class AttachmentTest < Test::Unit::TestCase
end
end
end
context
'with specified sanitizer proc'
do
setup
do
@old_defaults
=
Paperclip
::
Attachment
.
default_options
.
dup
end
teardown
do
Paperclip
::
Attachment
.
default_options
.
merge!
@old_defaults
end
should
'call proc and take it result as cleaned filename'
do
proc
=
Proc
.
new
do
|
str
,
attachment
|
"from_proc_
#{
str
}
"
end
Paperclip
::
Attachment
.
default_options
[
:filename_sanitizer
]
=
proc
@file
.
stubs
(
:original_filename
).
returns
(
"goood.png"
)
@dummy
=
Dummy
.
new
@dummy
.
avatar
=
@file
assert_equal
"from_proc_goood.png"
,
@dummy
.
avatar
.
original_filename
end
should
'be able to call cleanup_filename from proc'
do
proc
=
Proc
.
new
do
|
str
,
attachment
|
attachment
.
cleanup_filename
"from_proc_
#{
str
}
"
end
Paperclip
::
Attachment
.
default_options
[
:filename_sanitizer
]
=
proc
@file
.
stubs
(
:original_filename
).
returns
(
"go/ood.png"
)
@dummy
=
Dummy
.
new
@dummy
.
avatar
=
@file
assert_equal
"from_proc_go_ood.png"
,
@dummy
.
avatar
.
original_filename
end
end
end
context
"Attachment with uppercase extension and a default style"
do
...
...
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