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
98eff60d
Commit
98eff60d
authored
Aug 04, 2009
by
Jon Yurek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
AWS::S3 Passes integration tests that actually hit S3.
parent
b3f26a69
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
38 additions
and
24 deletions
+38
-24
lib/paperclip/storage.rb
+13
-10
test/integration_test.rb
+13
-11
test/storage_test.rb
+12
-3
No files found.
lib/paperclip/storage.rb
View file @
98eff60d
...
@@ -133,11 +133,15 @@ module Paperclip
...
@@ -133,11 +133,15 @@ module Paperclip
@bucket
=
@options
[
:bucket
]
||
@s3_credentials
[
:bucket
]
@bucket
=
@options
[
:bucket
]
||
@s3_credentials
[
:bucket
]
@bucket
=
@bucket
.
call
(
self
)
if
@bucket
.
is_a?
(
Proc
)
@bucket
=
@bucket
.
call
(
self
)
if
@bucket
.
is_a?
(
Proc
)
@s3_options
=
@options
[
:s3_options
]
||
{}
@s3_options
=
@options
[
:s3_options
]
||
{}
@s3_permissions
=
@options
[
:s3_permissions
]
||
'public-read'
@s3_permissions
=
@options
[
:s3_permissions
]
||
:public_read
@s3_protocol
=
@options
[
:s3_protocol
]
||
(
@s3_permissions
==
'public-read'
?
'http'
:
'https'
)
@s3_protocol
=
@options
[
:s3_protocol
]
||
(
@s3_permissions
==
:public_read
?
'http'
:
'https'
)
@s3_headers
=
@options
[
:s3_headers
]
||
{}
@s3_headers
=
@options
[
:s3_headers
]
||
{}
@s3_host_alias
=
@options
[
:s3_host_alias
]
@s3_host_alias
=
@options
[
:s3_host_alias
]
@url
=
":s3_path_url"
unless
@url
.
to_s
.
match
(
/^:s3.*url$/
)
@url
=
":s3_path_url"
unless
@url
.
to_s
.
match
(
/^:s3.*url$/
)
AWS
::
S3
::
Base
.
establish_connection!
(
@s3_options
.
merge
(
:access_key_id
=>
@s3_credentials
[
:access_key_id
],
:secret_access_key
=>
@s3_credentials
[
:secret_access_key
]
))
end
end
Paperclip
.
interpolates
(
:s3_alias_url
)
do
|
attachment
,
style
|
Paperclip
.
interpolates
(
:s3_alias_url
)
do
|
attachment
,
style
|
"
#{
attachment
.
s3_protocol
}
://
#{
attachment
.
s3_host_alias
}
/
#{
attachment
.
path
(
style
).
gsub
(
%r{^/}
,
""
)
}
"
"
#{
attachment
.
s3_protocol
}
://
#{
attachment
.
s3_host_alias
}
/
#{
attachment
.
path
(
style
).
gsub
(
%r{^/}
,
""
)
}
"
...
@@ -150,13 +154,6 @@ module Paperclip
...
@@ -150,13 +154,6 @@ module Paperclip
end
end
end
end
def
s3
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
def
bucket_name
@bucket
@bucket
end
end
...
@@ -171,7 +168,11 @@ module Paperclip
...
@@ -171,7 +168,11 @@ module Paperclip
end
end
def
exists?
(
style
=
default_style
)
def
exists?
(
style
=
default_style
)
if
original_filename
AWS
::
S3
::
S3Object
.
exists?
(
path
(
style
),
bucket_name
)
AWS
::
S3
::
S3Object
.
exists?
(
path
(
style
),
bucket_name
)
else
false
end
end
end
def
s3_protocol
def
s3_protocol
...
@@ -195,7 +196,9 @@ module Paperclip
...
@@ -195,7 +196,9 @@ module Paperclip
AWS
::
S3
::
S3Object
.
store
(
path
(
style
),
AWS
::
S3
::
S3Object
.
store
(
path
(
style
),
file
,
file
,
bucket_name
,
bucket_name
,
{
:content_type
=>
instance_read
(
:content_type
)}.
merge
(
@s3_headers
))
{
:content_type
=>
instance_read
(
:content_type
),
:access
=>
@s3_permissions
,
}.
merge
(
@s3_headers
))
rescue
AWS
::
S3
::
ResponseError
=>
e
rescue
AWS
::
S3
::
ResponseError
=>
e
raise
raise
end
end
...
...
test/integration_test.rb
View file @
98eff60d
...
@@ -379,6 +379,11 @@ class IntegrationTest < Test::Unit::TestCase
...
@@ -379,6 +379,11 @@ class IntegrationTest < Test::Unit::TestCase
@files_on_s3
=
s3_files_for
@dummy
.
avatar
@files_on_s3
=
s3_files_for
@dummy
.
avatar
end
end
should
"have the same contents as the original"
do
@file
.
rewind
assert_equal
@file
.
read
,
@files_on_s3
[
:original
].
read
end
should
"write and delete its files"
do
should
"write and delete its files"
do
[[
"434x66"
,
:original
],
[[
"434x66"
,
:original
],
[
"300x46"
,
:large
],
[
"300x46"
,
:large
],
...
@@ -403,10 +408,8 @@ class IntegrationTest < Test::Unit::TestCase
...
@@ -403,10 +408,8 @@ class IntegrationTest < Test::Unit::TestCase
assert
@dummy
.
valid?
assert
@dummy
.
valid?
assert
@dummy
.
save
assert
@dummy
.
save
saved_keys
=
[
:thumb
,
:medium
,
:large
,
:original
].
collect
{
|
s
|
@dummy
.
avatar
.
to_file
(
s
)
}
[
:thumb
,
:medium
,
:large
,
:original
].
each
do
|
style
|
assert
@dummy
.
avatar
.
exists?
(
style
)
saved_keys
.
each
do
|
key
|
assert
key
.
exists?
end
end
@dummy
.
avatar
.
clear
@dummy
.
avatar
.
clear
...
@@ -414,8 +417,8 @@ class IntegrationTest < Test::Unit::TestCase
...
@@ -414,8 +417,8 @@ class IntegrationTest < Test::Unit::TestCase
assert
@dummy
.
valid?
assert
@dummy
.
valid?
assert
@dummy
.
save
assert
@dummy
.
save
saved_keys
.
each
do
|
key
|
[
:thumb
,
:medium
,
:large
,
:original
].
each
do
|
style
|
assert
!
key
.
exists?
assert
!
@dummy
.
avatar
.
exists?
(
style
)
end
end
@d2
=
Dummy
.
find
(
@dummy
.
id
)
@d2
=
Dummy
.
find
(
@dummy
.
id
)
...
@@ -427,7 +430,7 @@ class IntegrationTest < Test::Unit::TestCase
...
@@ -427,7 +430,7 @@ class IntegrationTest < Test::Unit::TestCase
assert_equal
@dummy
.
avatar_file_name
,
@d2
.
avatar_file_name
assert_equal
@dummy
.
avatar_file_name
,
@d2
.
avatar_file_name
[
:thumb
,
:medium
,
:large
,
:original
].
each
do
|
style
|
[
:thumb
,
:medium
,
:large
,
:original
].
each
do
|
style
|
assert_equal
@dummy
.
avatar
.
to_file
(
style
).
to_s
,
@d2
.
avatar
.
to_file
(
style
).
to_s
assert_equal
@dummy
.
avatar
.
to_file
(
style
).
read
,
@d2
.
avatar
.
to_file
(
style
).
read
end
end
saved_keys
=
[
:thumb
,
:medium
,
:large
,
:original
].
collect
{
|
s
|
@dummy
.
avatar
.
to_file
(
s
)
}
saved_keys
=
[
:thumb
,
:medium
,
:large
,
:original
].
collect
{
|
s
|
@dummy
.
avatar
.
to_file
(
s
)
}
...
@@ -435,8 +438,8 @@ class IntegrationTest < Test::Unit::TestCase
...
@@ -435,8 +438,8 @@ class IntegrationTest < Test::Unit::TestCase
@d2
.
avatar
.
clear
@d2
.
avatar
.
clear
assert
@d2
.
save
assert
@d2
.
save
saved_keys
.
each
do
|
key
|
[
:thumb
,
:medium
,
:large
,
:original
].
each
do
|
style
|
assert
!
key
.
exists?
assert
!
@dummy
.
avatar
.
exists?
(
style
)
end
end
end
end
...
@@ -444,7 +447,7 @@ class IntegrationTest < Test::Unit::TestCase
...
@@ -444,7 +447,7 @@ class IntegrationTest < Test::Unit::TestCase
expected
=
@dummy
.
avatar
.
to_file
expected
=
@dummy
.
avatar
.
to_file
@dummy
.
avatar
=
"not a file"
@dummy
.
avatar
=
"not a file"
assert
@dummy
.
valid?
assert
@dummy
.
valid?
assert_equal
expected
.
full_name
,
@dummy
.
avatar
.
to_file
.
full_name
assert_equal
expected
.
read
,
@dummy
.
avatar
.
to_file
.
read
@dummy
.
avatar
=
@bad_file
@dummy
.
avatar
=
@bad_file
assert
!
@dummy
.
valid?
assert
!
@dummy
.
valid?
...
@@ -472,7 +475,6 @@ class IntegrationTest < Test::Unit::TestCase
...
@@ -472,7 +475,6 @@ class IntegrationTest < Test::Unit::TestCase
should
"have the right content type"
do
should
"have the right content type"
do
headers
=
s3_headers_for
(
@dummy
.
avatar
,
:original
)
headers
=
s3_headers_for
(
@dummy
.
avatar
,
:original
)
p
headers
assert_equal
'image/png'
,
headers
[
'content-type'
]
assert_equal
'image/png'
,
headers
[
'content-type'
]
end
end
end
end
...
...
test/storage_test.rb
View file @
98eff60d
...
@@ -3,6 +3,7 @@ require 'test/helper'
...
@@ -3,6 +3,7 @@ require 'test/helper'
class
StorageTest
<
Test
::
Unit
::
TestCase
class
StorageTest
<
Test
::
Unit
::
TestCase
context
"Parsing S3 credentials"
do
context
"Parsing S3 credentials"
do
setup
do
setup
do
AWS
::
S3
::
Base
.
stubs
(
:establish_connection!
)
rebuild_model
:storage
=>
:s3
,
rebuild_model
:storage
=>
:s3
,
:bucket
=>
"testing"
,
:bucket
=>
"testing"
,
:s3_credentials
=>
{
:not
=>
:important
}
:s3_credentials
=>
{
:not
=>
:important
}
...
@@ -39,6 +40,7 @@ class StorageTest < Test::Unit::TestCase
...
@@ -39,6 +40,7 @@ class StorageTest < Test::Unit::TestCase
context
""
do
context
""
do
setup
do
setup
do
AWS
::
S3
::
Base
.
stubs
(
:establish_connection!
)
rebuild_model
:storage
=>
:s3
,
rebuild_model
:storage
=>
:s3
,
:s3_credentials
=>
{},
:s3_credentials
=>
{},
:bucket
=>
"bucket"
,
:bucket
=>
"bucket"
,
...
@@ -54,6 +56,7 @@ class StorageTest < Test::Unit::TestCase
...
@@ -54,6 +56,7 @@ class StorageTest < Test::Unit::TestCase
end
end
context
""
do
context
""
do
setup
do
setup
do
AWS
::
S3
::
Base
.
stubs
(
:establish_connection!
)
rebuild_model
:storage
=>
:s3
,
rebuild_model
:storage
=>
:s3
,
:s3_credentials
=>
{},
:s3_credentials
=>
{},
:bucket
=>
"bucket"
,
:bucket
=>
"bucket"
,
...
@@ -69,6 +72,7 @@ class StorageTest < Test::Unit::TestCase
...
@@ -69,6 +72,7 @@ class StorageTest < Test::Unit::TestCase
end
end
context
""
do
context
""
do
setup
do
setup
do
AWS
::
S3
::
Base
.
stubs
(
:establish_connection!
)
rebuild_model
:storage
=>
:s3
,
rebuild_model
:storage
=>
:s3
,
:s3_credentials
=>
{
:s3_credentials
=>
{
:production
=>
{
:bucket
=>
"prod_bucket"
},
:production
=>
{
:bucket
=>
"prod_bucket"
},
...
@@ -88,6 +92,7 @@ class StorageTest < Test::Unit::TestCase
...
@@ -88,6 +92,7 @@ class StorageTest < Test::Unit::TestCase
context
"Parsing S3 credentials with a bucket in them"
do
context
"Parsing S3 credentials with a bucket in them"
do
setup
do
setup
do
AWS
::
S3
::
Base
.
stubs
(
:establish_connection!
)
rebuild_model
:storage
=>
:s3
,
rebuild_model
:storage
=>
:s3
,
:s3_credentials
=>
{
:s3_credentials
=>
{
:production
=>
{
:bucket
=>
"prod_bucket"
},
:production
=>
{
:bucket
=>
"prod_bucket"
},
...
@@ -146,7 +151,7 @@ class StorageTest < Test::Unit::TestCase
...
@@ -146,7 +151,7 @@ class StorageTest < Test::Unit::TestCase
context
"and saved"
do
context
"and saved"
do
setup
do
setup
do
AWS
::
S3
::
S3Object
.
stubs
(
:store
).
with
(
@dummy
.
avatar
.
path
,
anything
,
'testing'
,
:content_type
=>
'image/png'
)
AWS
::
S3
::
S3Object
.
stubs
(
:store
).
with
(
@dummy
.
avatar
.
path
,
anything
,
'testing'
,
:content_type
=>
'image/png'
,
:access
=>
:public_read
)
@dummy
.
save
@dummy
.
save
end
end
...
@@ -171,6 +176,7 @@ class StorageTest < Test::Unit::TestCase
...
@@ -171,6 +176,7 @@ class StorageTest < Test::Unit::TestCase
context
"An attachment with S3 storage and bucket defined as a Proc"
do
context
"An attachment with S3 storage and bucket defined as a Proc"
do
setup
do
setup
do
AWS
::
S3
::
Base
.
stubs
(
:establish_connection!
)
rebuild_model
:storage
=>
:s3
,
rebuild_model
:storage
=>
:s3
,
:bucket
=>
lambda
{
|
attachment
|
"bucket_
#{
attachment
.
instance
.
other
}
"
},
:bucket
=>
lambda
{
|
attachment
|
"bucket_
#{
attachment
.
instance
.
other
}
"
},
:s3_credentials
=>
{
:not
=>
:important
}
:s3_credentials
=>
{
:not
=>
:important
}
...
@@ -184,6 +190,7 @@ class StorageTest < Test::Unit::TestCase
...
@@ -184,6 +190,7 @@ class StorageTest < Test::Unit::TestCase
context
"An attachment with S3 storage and specific s3 headers set"
do
context
"An attachment with S3 storage and specific s3 headers set"
do
setup
do
setup
do
AWS
::
S3
::
Base
.
stubs
(
:establish_connection!
)
rebuild_model
:storage
=>
:s3
,
rebuild_model
:storage
=>
:s3
,
:bucket
=>
"testing"
,
:bucket
=>
"testing"
,
:path
=>
":attachment/:style/:basename.:extension"
,
:path
=>
":attachment/:style/:basename.:extension"
,
...
@@ -205,10 +212,12 @@ class StorageTest < Test::Unit::TestCase
...
@@ -205,10 +212,12 @@ class StorageTest < Test::Unit::TestCase
context
"and saved"
do
context
"and saved"
do
setup
do
setup
do
AWS
::
S3
::
Base
.
stubs
(
:establish_connection!
)
AWS
::
S3
::
S3Object
.
stubs
(
:store
).
with
(
@dummy
.
avatar
.
path
,
AWS
::
S3
::
S3Object
.
stubs
(
:store
).
with
(
@dummy
.
avatar
.
path
,
anything
,
anything
,
'testing'
,
'testing'
,
:content_type
=>
'image/png'
,
:content_type
=>
'image/png'
,
:access
=>
:public_read
,
'Cache-Control'
=>
'max-age=31557600'
)
'Cache-Control'
=>
'max-age=31557600'
)
@dummy
.
save
@dummy
.
save
end
end
...
@@ -245,8 +254,8 @@ class StorageTest < Test::Unit::TestCase
...
@@ -245,8 +254,8 @@ class StorageTest < Test::Unit::TestCase
teardown
{
@file
.
close
}
teardown
{
@file
.
close
}
should
"still return a Tempfile when sent #to_
io
"
do
should
"still return a Tempfile when sent #to_
file
"
do
assert_equal
Tempfile
,
@dummy
.
avatar
.
to_
io
.
class
assert_equal
Tempfile
,
@dummy
.
avatar
.
to_
file
.
class
end
end
context
"and saved"
do
context
"and saved"
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