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
dbd63a29
Commit
dbd63a29
authored
Apr 29, 2008
by
Jon Yurek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Removed the need for S3 to connect just to get a URL
parent
f0cd9ea4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
37 additions
and
30 deletions
+37
-30
lib/paperclip/attachment.rb
+1
-1
lib/paperclip/storage.rb
+14
-9
test/test_attachment.rb
+17
-8
test/test_storage.rb
+5
-12
No files found.
lib/paperclip/attachment.rb
View file @
dbd63a29
...
@@ -74,7 +74,7 @@ module Paperclip
...
@@ -74,7 +74,7 @@ module Paperclip
# This is not recommended if you don't need the security, however, for
# This is not recommended if you don't need the security, however, for
# performance reasons.
# performance reasons.
def
url
style
=
default_style
def
url
style
=
default_style
exists?
(
style
)
?
interpolate
(
@url
,
style
)
:
interpolate
(
@default_
url
,
style
)
original_filename
.
nil?
?
interpolate
(
@default_url
,
style
)
:
interpolate
(
@
url
,
style
)
end
end
# Returns the path of the attachment as defined by the :path optionn. If the
# Returns the path of the attachment as defined by the :path optionn. If the
...
...
lib/paperclip/storage.rb
View file @
dbd63a29
...
@@ -50,11 +50,6 @@ module Paperclip
...
@@ -50,11 +50,6 @@ module Paperclip
@s3_credentials
=
parse_credentials
(
@options
[
:s3_credentials
])
@s3_credentials
=
parse_credentials
(
@options
[
:s3_credentials
])
@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
=
RightAws
::
S3
.
new
(
@s3_credentials
[
:access_key_id
],
@s3_credentials
[
:secret_access_key
],
@s3_options
)
@s3_bucket
=
@s3
.
bucket
(
@bucket
,
true
,
@s3_permissions
)
@url
=
":s3_url"
@url
=
":s3_url"
end
end
base
.
class
.
interpolations
[
:s3_url
]
=
lambda
do
|
attachment
,
style
|
base
.
class
.
interpolations
[
:s3_url
]
=
lambda
do
|
attachment
,
style
|
...
@@ -62,26 +57,36 @@ module Paperclip
...
@@ -62,26 +57,36 @@ module Paperclip
end
end
end
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
)
end
def
parse_credentials
creds
def
parse_credentials
creds
creds
=
find_credentials
(
creds
).
stringify_keys
creds
=
find_credentials
(
creds
).
stringify_keys
(
creds
[
ENV
[
'RAILS_ENV'
]]
||
creds
).
symbolize_keys
(
creds
[
ENV
[
'RAILS_ENV'
]]
||
creds
).
symbolize_keys
end
end
def
exists?
(
style
=
default_style
)
def
exists?
(
style
=
default_style
)
@
s3_bucket
.
key
(
path
(
style
))
?
true
:
false
s3_bucket
.
key
(
path
(
style
))
?
true
:
false
end
end
# Returns representation of the data of the file assigned to the given
# Returns representation of the data of the file assigned to the given
# style, in the format most representative of the current storage.
# style, in the format most representative of the current storage.
def
to_file
style
=
default_style
def
to_file
style
=
default_style
@queued_for_write
[
style
]
||
@
s3_bucket
.
key
(
path
(
style
))
@queued_for_write
[
style
]
||
s3_bucket
.
key
(
path
(
style
))
end
end
alias_method
:to_io
,
:to_file
alias_method
:to_io
,
:to_file
def
flush_writes
#:nodoc:
def
flush_writes
#:nodoc:
@queued_for_write
.
each
do
|
style
,
file
|
@queued_for_write
.
each
do
|
style
,
file
|
begin
begin
key
=
@
s3_bucket
.
key
(
path
(
style
))
key
=
s3_bucket
.
key
(
path
(
style
))
key
.
data
=
file
key
.
data
=
file
key
.
put
(
nil
,
@s3_permissions
)
key
.
put
(
nil
,
@s3_permissions
)
rescue
RightAws
::
AwsError
=>
e
rescue
RightAws
::
AwsError
=>
e
...
@@ -94,7 +99,7 @@ module Paperclip
...
@@ -94,7 +99,7 @@ module Paperclip
def
flush_deletes
#:nodoc:
def
flush_deletes
#:nodoc:
@queued_for_delete
.
each
do
|
path
|
@queued_for_delete
.
each
do
|
path
|
begin
begin
if
file
=
@
s3_bucket
.
key
(
path
)
if
file
=
s3_bucket
.
key
(
path
)
file
.
delete
file
.
delete
end
end
rescue
RightAws
::
AwsError
rescue
RightAws
::
AwsError
...
...
test/test_attachment.rb
View file @
dbd63a29
...
@@ -95,9 +95,9 @@ class AttachmentTest < Test::Unit::TestCase
...
@@ -95,9 +95,9 @@ class AttachmentTest < Test::Unit::TestCase
@instance
=
stub
@instance
=
stub
@instance
.
stubs
(
:id
).
returns
(
41
)
@instance
.
stubs
(
:id
).
returns
(
41
)
@instance
.
stubs
(
:class
).
returns
(
Dummy
)
@instance
.
stubs
(
:class
).
returns
(
Dummy
)
@instance
.
stubs
(
:[]
).
with
(
:test_file_name
).
returns
(
"5k.png"
)
@instance
.
stubs
(
:[]
).
with
(
:test_file_name
).
returns
(
nil
)
@instance
.
stubs
(
:[]
).
with
(
:test_content_type
).
returns
(
"image/png"
)
@instance
.
stubs
(
:[]
).
with
(
:test_content_type
).
returns
(
nil
)
@instance
.
stubs
(
:[]
).
with
(
:test_file_size
).
returns
(
12345
)
@instance
.
stubs
(
:[]
).
with
(
:test_file_size
).
returns
(
nil
)
@attachment
=
Paperclip
::
Attachment
.
new
(
:test
,
@attachment
=
Paperclip
::
Attachment
.
new
(
:test
,
@instance
)
@instance
)
@file
=
File
.
new
(
File
.
join
(
File
.
dirname
(
__FILE__
),
@file
=
File
.
new
(
File
.
join
(
File
.
dirname
(
__FILE__
),
...
@@ -111,6 +111,18 @@ class AttachmentTest < Test::Unit::TestCase
...
@@ -111,6 +111,18 @@ class AttachmentTest < Test::Unit::TestCase
assert_equal
"/tests/blah/missing.png"
,
@attachment
.
url
(
:blah
)
assert_equal
"/tests/blah/missing.png"
,
@attachment
.
url
(
:blah
)
end
end
context
"with a file assigned in the database"
do
setup
do
@instance
.
stubs
(
:[]
).
with
(
:test_file_name
).
returns
(
"5k.png"
)
@instance
.
stubs
(
:[]
).
with
(
:test_content_type
).
returns
(
"image/png"
)
@instance
.
stubs
(
:[]
).
with
(
:test_file_size
).
returns
(
12345
)
end
should
"return a correct url even if the file does not exist"
do
assert_nil
@attachment
.
to_file
assert_equal
"/tests/41/blah/5k.png"
,
@attachment
.
url
(
:blah
)
end
context
"when expecting three styles"
do
context
"when expecting three styles"
do
setup
do
setup
do
styles
=
{
:styles
=>
{
:large
=>
[
"400x400"
,
:png
],
styles
=
{
:styles
=>
{
:large
=>
[
"400x400"
,
:png
],
...
@@ -148,11 +160,6 @@ class AttachmentTest < Test::Unit::TestCase
...
@@ -148,11 +160,6 @@ class AttachmentTest < Test::Unit::TestCase
assert_equal
"/tests/41/small/5k.jpg"
,
@attachment
.
url
(
:small
)
assert_equal
"/tests/41/small/5k.jpg"
,
@attachment
.
url
(
:small
)
end
end
should
"return its default_url when no file assigned"
do
assert
@attachment
.
to_file
assert_equal
"/tests/blah/missing.png"
,
@attachment
.
url
(
:blah
)
end
should
"commit the files to disk"
do
should
"commit the files to disk"
do
[
:large
,
:medium
,
:small
].
each
do
|
style
|
[
:large
,
:medium
,
:small
].
each
do
|
style
|
io
=
@attachment
.
to_io
(
style
)
io
=
@attachment
.
to_io
(
style
)
...
@@ -199,6 +206,8 @@ class AttachmentTest < Test::Unit::TestCase
...
@@ -199,6 +206,8 @@ class AttachmentTest < Test::Unit::TestCase
end
end
end
end
end
context
"when trying a nonexistant storage type"
do
context
"when trying a nonexistant storage type"
do
setup
do
setup
do
rebuild_model
:storage
=>
:not_here
rebuild_model
:storage
=>
:not_here
...
...
test/test_storage.rb
View file @
dbd63a29
...
@@ -5,17 +5,13 @@ require 'right_aws'
...
@@ -5,17 +5,13 @@ require 'right_aws'
require
File
.
join
(
File
.
dirname
(
__FILE__
),
'..'
,
'lib'
,
'paperclip'
,
'geometry.rb'
)
require
File
.
join
(
File
.
dirname
(
__FILE__
),
'..'
,
'lib'
,
'paperclip'
,
'geometry.rb'
)
class
S
3
Test
<
Test
::
Unit
::
TestCase
class
S
torage
Test
<
Test
::
Unit
::
TestCase
context
"Parsing S3 credentials"
do
context
"Parsing S3 credentials"
do
setup
do
setup
do
rebuild_model
:storage
=>
:s3
,
rebuild_model
:storage
=>
:s3
,
:bucket
=>
"testing"
,
:bucket
=>
"testing"
,
:s3_credentials
=>
{
:not
=>
:important
}
:s3_credentials
=>
{
:not
=>
:important
}
@s3_stub
=
stub
@bucket_stub
=
stub
RightAws
::
S3
.
expects
(
:new
).
returns
(
@s3_stub
)
@s3_stub
.
expects
(
:bucket
).
returns
(
@bucket_stub
)
@dummy
=
Dummy
.
new
@dummy
=
Dummy
.
new
@avatar
=
@dummy
.
avatar
@avatar
=
@dummy
.
avatar
...
@@ -54,13 +50,6 @@ class S3Test < Test::Unit::TestCase
...
@@ -54,13 +50,6 @@ class S3Test < Test::Unit::TestCase
'access_key_id'
=>
"12345"
,
'access_key_id'
=>
"12345"
,
'secret_access_key'
=>
"54321"
'secret_access_key'
=>
"54321"
}
}
@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
)
end
end
should
"be extended by the S3 module"
do
should
"be extended by the S3 module"
do
...
@@ -80,6 +69,10 @@ class S3Test < Test::Unit::TestCase
...
@@ -80,6 +69,10 @@ class S3Test < Test::Unit::TestCase
context
"and saved"
do
context
"and saved"
do
setup
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
@key_mock
=
stub
@bucket_mock
.
expects
(
:key
).
returns
(
@key_mock
)
@bucket_mock
.
expects
(
:key
).
returns
(
@key_mock
)
@key_mock
.
expects
(
:data
=
)
@key_mock
.
expects
(
:data
=
)
...
...
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