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
ac86bb9d
Commit
ac86bb9d
authored
Jul 30, 2009
by
Jon Yurek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Converted to AWS::S3, all tests passing.
parent
80d699ca
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
93 additions
and
80 deletions
+93
-80
lib/paperclip.rb
+1
-1
lib/paperclip/storage.rb
+18
-20
test/attachment_test.rb
+13
-1
test/paperclip_test.rb
+53
-32
test/storage_test.rb
+8
-26
No files found.
lib/paperclip.rb
View file @
ac86bb9d
...
...
@@ -93,7 +93,7 @@ module Paperclip
# Paperclip.options[:log_command] is set to true (defaults to false). This
# will only log if logging in general is set to true as well.
def
run
cmd
,
params
=
""
,
expected_outcodes
=
0
command
=
%Q
<
#{
%
Q
[
#{path_for_command(cmd)} #{params}].gsub(/\s+/, " ")}>
command
=
%Q
[
#{
path_for_command
(
cmd
)
}
#{
params
}
]
.
gsub
(
/\s+/
,
" "
)
command
=
"
#{
command
}
2>
#{
bit_bucket
}
"
if
Paperclip
.
options
[
:swallow_stderr
]
Paperclip
.
log
(
command
)
if
Paperclip
.
options
[
:log_command
]
output
=
`
#{
command
}
`
...
...
lib/paperclip/storage.rb
View file @
ac86bb9d
...
...
@@ -33,7 +33,6 @@ module Paperclip
def
to_file
style
=
default_style
@queued_for_write
[
style
]
||
(
File
.
new
(
path
(
style
),
'rb'
)
if
exists?
(
style
))
end
alias_method
:to_io
,
:to_file
def
flush_writes
#:nodoc:
@queued_for_write
.
each
do
|
style
,
file
|
...
...
@@ -128,7 +127,7 @@ module Paperclip
# separate parts of your file name.
module
S3
def
self
.
extended
base
require
'
right_aws
'
require
'
aws/s3
'
base
.
instance_eval
do
@s3_credentials
=
parse_credentials
(
@options
[
:s3_credentials
])
@bucket
=
@options
[
:bucket
]
||
@s3_credentials
[
:bucket
]
...
...
@@ -152,13 +151,10 @@ module Paperclip
end
def
s3
@s3
||=
RightAws
::
S3
.
new
(
@s3_credentials
[
:access_key_id
],
@s3_credentials
[
:secret_access_key
],
@s3_options
)
end
def
s3_bucket
@s3_bucket
||=
s3
.
bucket
(
@bucket
,
true
,
@s3_permissions
)
AWS
::
S3
::
S3Object
.
establish_connection
(
@s3_options
.
merge
(
:access_key_id
=>
@s3_credentials
[
:access_key_id
],
:secret_access_key
=>
@s3_credentials
[
:secret_access_key
]
))
end
def
bucket_name
...
...
@@ -175,7 +171,7 @@ module Paperclip
end
def
exists?
(
style
=
default_style
)
s3_bucket
.
key
(
path
(
style
))
?
true
:
false
AWS
::
S3
::
S3Object
.
exists?
(
path
(
style
),
bucket_name
)
end
def
s3_protocol
...
...
@@ -185,18 +181,22 @@ module Paperclip
# Returns representation of the data of the file assigned to the given
# style, in the format most representative of the current storage.
def
to_file
style
=
default_style
@queued_for_write
[
style
]
||
s3_bucket
.
key
(
path
(
style
))
return
@queued_for_write
[
style
]
if
@queued_for_write
[
style
]
file
=
Tempfile
.
new
(
path
(
style
))
file
.
write
(
AWS
::
S3
::
S3Object
.
value
(
path
(
style
),
bucket_name
))
file
.
rewind
return
file
end
alias_method
:to_io
,
:to_file
def
flush_writes
#:nodoc:
@queued_for_write
.
each
do
|
style
,
file
|
begin
log
(
"saving
#{
path
(
style
)
}
"
)
key
=
s3_bucket
.
key
(
path
(
style
))
key
.
data
=
file
key
.
put
(
nil
,
@s3_permissions
,
{
'Content-type'
=>
instance_read
(
:content_type
)}.
merge
(
@s3_headers
))
rescue
RightAws
::
AwsError
=>
e
AWS
::
S3
::
S3Object
.
store
(
path
(
style
),
file
,
bucket_name
,
{
:content_type
=>
instance_read
(
:content_type
)}.
merge
(
@s3_headers
))
rescue
AWS
::
S3
::
ResponseError
=>
e
raise
end
end
...
...
@@ -207,10 +207,8 @@ module Paperclip
@queued_for_delete
.
each
do
|
path
|
begin
log
(
"deleting
#{
path
}
"
)
if
file
=
s3_bucket
.
key
(
path
)
file
.
delete
end
rescue
RightAws
::
AwsError
AWS
::
S3
::
S3Object
.
delete
(
path
,
bucket_name
)
rescue
AWS
::
S3
::
ResponseError
# Ignore this.
end
end
...
...
test/attachment_test.rb
View file @
ac86bb9d
...
...
@@ -13,6 +13,18 @@ class AttachmentTest < Test::Unit::TestCase
assert_equal
"
#{
RAILS_ROOT
}
/public/fake_models/1234/fake"
,
@attachment
.
path
end
should
"call a proc sent to check_guard"
do
@dummy
=
Dummy
.
new
@dummy
.
expects
(
:one
).
returns
(
:one
)
assert_equal
:one
,
@dummy
.
avatar
.
send
(
:check_guard
,
lambda
{
|
x
|
x
.
one
})
end
should
"call a method name sent to check_guard"
do
@dummy
=
Dummy
.
new
@dummy
.
expects
(
:one
).
returns
(
:one
)
assert_equal
:one
,
@dummy
.
avatar
.
send
(
:check_guard
,
:one
)
end
context
"Attachment default_options"
do
setup
do
rebuild_model
...
...
@@ -593,7 +605,7 @@ class AttachmentTest < Test::Unit::TestCase
should
"commit the files to disk"
do
[
:large
,
:medium
,
:small
].
each
do
|
style
|
io
=
@attachment
.
to_
io
(
style
)
io
=
@attachment
.
to_
file
(
style
)
assert
File
.
exists?
(
io
)
assert
!
io
.
is_a?
(
::
Tempfile
)
io
.
close
...
...
test/paperclip_test.rb
View file @
ac86bb9d
require
'test/helper'
class
PaperclipTest
<
Test
::
Unit
::
TestCase
[
:image_magick_path
,
:co
nvert
_path
].
each
do
|
path
|
context
"Calling Paperclip.run with
an
#{
path
}
specified"
do
[
:image_magick_path
,
:co
mmand
_path
].
each
do
|
path
|
context
"Calling Paperclip.run with
#{
path
}
specified"
do
setup
do
Paperclip
.
options
[
:image_magick_path
]
=
nil
Paperclip
.
options
[
:co
nvert
_path
]
=
nil
Paperclip
.
options
[
:co
mmand
_path
]
=
nil
Paperclip
.
options
[
path
]
=
"/usr/bin"
end
should
"return the expected path for path_for_command"
do
assert_equal
"/usr/bin/convert"
,
Paperclip
.
path_for_command
(
"convert"
)
end
should
"execute the right command"
do
Paperclip
.
expects
(
:path_for_command
).
with
(
"convert"
).
returns
(
"/usr/bin/convert"
)
Paperclip
.
expects
(
:bit_bucket
).
returns
(
"/dev/null"
)
...
...
@@ -21,7 +25,11 @@ class PaperclipTest < Test::Unit::TestCase
context
"Calling Paperclip.run with no path specified"
do
setup
do
Paperclip
.
options
[
:image_magick_path
]
=
nil
Paperclip
.
options
[
:convert_path
]
=
nil
Paperclip
.
options
[
:command_path
]
=
nil
end
should
"return the expected path fro path_for_command"
do
assert_equal
"convert"
,
Paperclip
.
path_for_command
(
"convert"
)
end
should
"execute the right command"
do
...
...
@@ -30,38 +38,38 @@ class PaperclipTest < Test::Unit::TestCase
Paperclip
.
expects
(
:"`"
).
with
(
"convert one.jpg two.jpg 2>/dev/null"
)
Paperclip
.
run
(
"convert"
,
"one.jpg two.jpg"
)
end
end
should
"log the command when :log_command is set"
do
context
"Calling Paperclip.run and logging"
do
setup
do
Paperclip
.
options
[
:image_magick_path
]
=
nil
Paperclip
.
options
[
:command_path
]
=
nil
Paperclip
.
stubs
(
:bit_bucket
).
returns
(
"/dev/null"
)
Paperclip
.
stubs
(
:log
)
Paperclip
.
stubs
(
:"`"
).
with
(
"this is the command 2>/dev/null"
)
end
should
"log the command when :log_command is true"
do
Paperclip
.
options
[
:log_command
]
=
true
Paperclip
.
expects
(
:bit_bucket
).
returns
(
"/dev/null"
)
Paperclip
.
expects
(
:log
).
with
(
"this is the command 2>/dev/null"
)
Paperclip
.
expects
(
:"`"
).
with
(
"this is the command 2>/dev/null"
)
Paperclip
.
run
(
"this"
,
"is the command"
)
assert_received
(
Paperclip
,
:log
)
do
|
p
|
p
.
with
(
"this is the command 2>/dev/null"
)
end
assert_received
(
Paperclip
,
:`
)
do
|
p
|
p
.
with
(
"this is the command 2>/dev/null"
)
end
end
end
should
"raise when sent #processor and the name of a class that exists but isn't a subclass of Processor"
do
assert_raises
(
Paperclip
::
PaperclipError
){
Paperclip
.
processor
(
:attachment
)
}
end
should
"raise when sent #processor and the name of a class that doesn't exist"
do
assert_raises
(
NameError
){
Paperclip
.
processor
(
:boogey_man
)
}
end
should
"return a class when sent #processor and the name of a class under Paperclip"
do
assert_equal
::
Paperclip
::
Thumbnail
,
Paperclip
.
processor
(
:thumbnail
)
end
should
"call a proc sent to check_guard"
do
@dummy
=
Dummy
.
new
@dummy
.
expects
(
:one
).
returns
(
:one
)
assert_equal
:one
,
@dummy
.
avatar
.
send
(
:check_guard
,
lambda
{
|
x
|
x
.
one
})
end
should
"call a method name sent to check_guard"
do
@dummy
=
Dummy
.
new
@dummy
.
expects
(
:one
).
returns
(
:one
)
assert_equal
:one
,
@dummy
.
avatar
.
send
(
:check_guard
,
:one
)
should
"not log the command when :log_command is false"
do
Paperclip
.
options
[
:log_command
]
=
false
Paperclip
.
run
(
"this"
,
"is the command"
)
assert_received
(
Paperclip
,
:log
)
do
|
p
|
p
.
with
(
"this is the command 2>/dev/null"
).
never
end
assert_received
(
Paperclip
,
:`
)
do
|
p
|
p
.
with
(
"this is the command 2>/dev/null"
)
end
end
end
context
"Paperclip.bit_bucket"
do
...
...
@@ -86,6 +94,18 @@ class PaperclipTest < Test::Unit::TestCase
end
end
should
"raise when sent #processor and the name of a class that exists but isn't a subclass of Processor"
do
assert_raises
(
Paperclip
::
PaperclipError
){
Paperclip
.
processor
(
:attachment
)
}
end
should
"raise when sent #processor and the name of a class that doesn't exist"
do
assert_raises
(
NameError
){
Paperclip
.
processor
(
:boogey_man
)
}
end
should
"return a class when sent #processor and the name of a class under Paperclip"
do
assert_equal
::
Paperclip
::
Thumbnail
,
Paperclip
.
processor
(
:thumbnail
)
end
context
"An ActiveRecord model with an 'avatar' attachment"
do
setup
do
rebuild_model
:path
=>
"tmp/:class/omg/:style.:extension"
...
...
@@ -139,7 +159,8 @@ class PaperclipTest < Test::Unit::TestCase
end
should
"be able to see the attachment definition from the subclass's class"
do
assert_equal
"tmp/:class/omg/:style.:extension"
,
SubDummy
.
attachment_definitions
[
:avatar
][
:path
]
assert_equal
"tmp/:class/omg/:style.:extension"
,
SubDummy
.
attachment_definitions
[
:avatar
][
:path
]
end
teardown
do
...
...
test/storage_test.rb
View file @
ac86bb9d
...
...
@@ -146,14 +146,7 @@ class StorageTest < Test::Unit::TestCase
context
"and saved"
do
setup
do
@s3_mock
=
stub
@bucket_mock
=
stub
RightAws
::
S3
.
expects
(
:new
).
with
(
"12345"
,
"54321"
,
{}).
returns
(
@s3_mock
)
@s3_mock
.
expects
(
:bucket
).
with
(
"testing"
,
true
,
"public-read"
).
returns
(
@bucket_mock
)
@key_mock
=
stub
@bucket_mock
.
expects
(
:key
).
returns
(
@key_mock
)
@key_mock
.
expects
(
:data
=
)
@key_mock
.
expects
(
:put
).
with
(
nil
,
'public-read'
,
'Content-type'
=>
'image/png'
)
AWS
::
S3
::
S3Object
.
stubs
(
:store
).
with
(
@dummy
.
avatar
.
path
,
anything
,
'testing'
,
:content_type
=>
'image/png'
)
@dummy
.
save
end
...
...
@@ -164,13 +157,8 @@ class StorageTest < Test::Unit::TestCase
context
"and remove"
do
setup
do
@s3_mock
=
stub
@bucket_mock
=
stub
RightAws
::
S3
.
expects
(
:new
).
with
(
"12345"
,
"54321"
,
{}).
returns
(
@s3_mock
)
@s3_mock
.
expects
(
:bucket
).
with
(
"testing"
,
true
,
"public-read"
).
returns
(
@bucket_mock
)
@key_mock
=
stub
@bucket_mock
.
expects
(
:key
).
at_least
(
2
).
returns
(
@key_mock
)
@key_mock
.
expects
(
:delete
)
AWS
::
S3
::
S3Object
.
stubs
(
:exists?
).
returns
(
true
)
AWS
::
S3
::
S3Object
.
stubs
(
:delete
)
@dummy
.
destroy_attached_files
end
...
...
@@ -217,17 +205,11 @@ class StorageTest < Test::Unit::TestCase
context
"and saved"
do
setup
do
@s3_mock
=
stub
@bucket_mock
=
stub
RightAws
::
S3
.
expects
(
:new
).
with
(
"12345"
,
"54321"
,
{}).
returns
(
@s3_mock
)
@s3_mock
.
expects
(
:bucket
).
with
(
"testing"
,
true
,
"public-read"
).
returns
(
@bucket_mock
)
@key_mock
=
stub
@bucket_mock
.
expects
(
:key
).
returns
(
@key_mock
)
@key_mock
.
expects
(
:data
=
)
@key_mock
.
expects
(
:put
).
with
(
nil
,
'public-read'
,
'Content-type'
=>
'image/png'
,
'Cache-Control'
=>
'max-age=31557600'
)
AWS
::
S3
::
S3Object
.
stubs
(
:store
).
with
(
@dummy
.
avatar
.
path
,
anything
,
'testing'
,
:content_type
=>
'image/png'
,
'Cache-Control'
=>
'max-age=31557600'
)
@dummy
.
save
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