carrierwave 0.5.8 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of carrierwave might be problematic. Click here for more details.
- data/README.md +92 -61
- data/lib/carrierwave.rb +11 -16
- data/lib/carrierwave/mount.rb +4 -4
- data/lib/carrierwave/orm/activerecord.rb +16 -0
- data/lib/carrierwave/processing/mime_types.rb +1 -1
- data/lib/carrierwave/processing/mini_magick.rb +5 -3
- data/lib/carrierwave/processing/rmagick.rb +2 -2
- data/lib/carrierwave/sanitized_file.rb +2 -2
- data/lib/carrierwave/storage/file.rb +3 -3
- data/lib/carrierwave/storage/fog.rb +33 -8
- data/lib/carrierwave/test/matchers.rb +3 -3
- data/lib/carrierwave/uploader.rb +1 -0
- data/lib/carrierwave/uploader/cache.rb +3 -3
- data/lib/carrierwave/uploader/configuration.rb +4 -36
- data/lib/carrierwave/uploader/download.rb +1 -1
- data/lib/carrierwave/uploader/mountable.rb +2 -2
- data/lib/carrierwave/uploader/processing.rb +3 -1
- data/lib/carrierwave/uploader/serialization.rb +30 -0
- data/lib/carrierwave/uploader/store.rb +0 -2
- data/lib/carrierwave/uploader/url.rb +8 -23
- data/lib/carrierwave/uploader/versions.rb +37 -20
- data/lib/carrierwave/validations/active_model.rb +1 -2
- data/lib/carrierwave/version.rb +1 -1
- data/lib/generators/templates/uploader.rb +7 -0
- metadata +121 -180
- data/lib/carrierwave/storage/cloud_files.rb +0 -188
- data/lib/carrierwave/storage/right_s3.rb +0 -1
- data/lib/carrierwave/storage/s3.rb +0 -240
@@ -1,188 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
require 'cloudfiles'
|
3
|
-
|
4
|
-
module CarrierWave
|
5
|
-
module Storage
|
6
|
-
|
7
|
-
##
|
8
|
-
# Uploads things to Rackspace Cloud Files webservices using the Rackspace libraries (cloudfiles gem).
|
9
|
-
# In order for CarrierWave to connect to Cloud Files, you'll need to specify an username, api key
|
10
|
-
# and container. Optional arguments are config.cloud_files_snet (using the private internal
|
11
|
-
# Rackspace network for communication) and config.cloud_files_auth_url (for connecting to Rackspace's
|
12
|
-
# UK infrastructure or an OpenStack Swift installation)
|
13
|
-
#
|
14
|
-
# CarrierWave.configure do |config|
|
15
|
-
# config.cloud_files_username = "xxxxxx"
|
16
|
-
# config.cloud_files_api_key = "xxxxxx"
|
17
|
-
# config.cloud_files_container = "my_container"
|
18
|
-
# config.cloud_files_auth_url = "https://lon.auth.api.rackspacecloud.com/v1.0"
|
19
|
-
# config.cloud_files_snet = true
|
20
|
-
# end
|
21
|
-
#
|
22
|
-
# You can optionally include your CDN host name in the configuration.
|
23
|
-
# This is *highly* recommended, as without it every request requires a lookup
|
24
|
-
# of this information.
|
25
|
-
#
|
26
|
-
# config.cloud_files_cdn_host = "c000000.cdn.rackspacecloud.com"
|
27
|
-
#
|
28
|
-
#
|
29
|
-
class CloudFiles < Abstract
|
30
|
-
|
31
|
-
class File
|
32
|
-
|
33
|
-
def initialize(uploader, base, path)
|
34
|
-
@uploader = uploader
|
35
|
-
@path = path
|
36
|
-
@base = base
|
37
|
-
end
|
38
|
-
|
39
|
-
##
|
40
|
-
# Returns the current path/filename of the file on Cloud Files.
|
41
|
-
#
|
42
|
-
# === Returns
|
43
|
-
#
|
44
|
-
# [String] A path
|
45
|
-
#
|
46
|
-
def path
|
47
|
-
@path
|
48
|
-
end
|
49
|
-
|
50
|
-
##
|
51
|
-
# Reads the contents of the file from Cloud Files
|
52
|
-
#
|
53
|
-
# === Returns
|
54
|
-
#
|
55
|
-
# [String] contents of the file
|
56
|
-
#
|
57
|
-
def read
|
58
|
-
object = cf_container.object(@path)
|
59
|
-
@content_type = object.content_type
|
60
|
-
object.data
|
61
|
-
end
|
62
|
-
|
63
|
-
##
|
64
|
-
# Remove the file from Cloud Files
|
65
|
-
#
|
66
|
-
def delete
|
67
|
-
begin
|
68
|
-
cf_container.delete_object(@path)
|
69
|
-
rescue ::CloudFiles::Exception::NoSuchObject
|
70
|
-
# If the file's not there, don't panic
|
71
|
-
nil
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
##
|
76
|
-
# Returns the url on the Cloud Files CDN. Note that the parent container must be marked as
|
77
|
-
# public for this to work.
|
78
|
-
#
|
79
|
-
# === Returns
|
80
|
-
#
|
81
|
-
# [String] file's url
|
82
|
-
#
|
83
|
-
def url
|
84
|
-
if @uploader.cloud_files_cdn_host
|
85
|
-
"http://" + @uploader.cloud_files_cdn_host + "/" + @path
|
86
|
-
else
|
87
|
-
begin
|
88
|
-
cf_container.object(@path).public_url
|
89
|
-
rescue ::CloudFiles::Exception::NoSuchObject
|
90
|
-
nil
|
91
|
-
end
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
def content_type
|
96
|
-
cf_container.object(@path).content_type
|
97
|
-
end
|
98
|
-
|
99
|
-
def content_type=(new_content_type)
|
100
|
-
headers["content-type"] = new_content_type
|
101
|
-
end
|
102
|
-
|
103
|
-
##
|
104
|
-
# Writes the supplied data into the object on Cloud Files.
|
105
|
-
#
|
106
|
-
# === Returns
|
107
|
-
#
|
108
|
-
# boolean
|
109
|
-
#
|
110
|
-
def store(data,headers={})
|
111
|
-
object = cf_container.create_object(@path)
|
112
|
-
object.write(data,headers)
|
113
|
-
end
|
114
|
-
|
115
|
-
private
|
116
|
-
|
117
|
-
def headers
|
118
|
-
@headers ||= { }
|
119
|
-
end
|
120
|
-
|
121
|
-
def container
|
122
|
-
@uploader.cloud_files_container
|
123
|
-
end
|
124
|
-
|
125
|
-
def connection
|
126
|
-
@base.connection
|
127
|
-
end
|
128
|
-
|
129
|
-
def cf_connection
|
130
|
-
config = {:username => @uploader.cloud_files_username, :api_key => @uploader.cloud_files_api_key}
|
131
|
-
config[:auth_url] = @uploader.cloud_files_auth_url if @uploader.respond_to?(:cloud_files_auth_url)
|
132
|
-
config[:snet] = @uploader.cloud_files_snet if @uploader.respond_to?(:cloud_files_snet)
|
133
|
-
@cf_connection ||= ::CloudFiles::Connection.new(config)
|
134
|
-
end
|
135
|
-
|
136
|
-
def cf_container
|
137
|
-
if @cf_container
|
138
|
-
@cf_container
|
139
|
-
else
|
140
|
-
begin
|
141
|
-
@cf_container = cf_connection.container(container)
|
142
|
-
rescue NoSuchContainerException
|
143
|
-
@cf_container = cf_connection.create_container(container)
|
144
|
-
@cf_container.make_public
|
145
|
-
end
|
146
|
-
@cf_container
|
147
|
-
end
|
148
|
-
end
|
149
|
-
|
150
|
-
|
151
|
-
end
|
152
|
-
|
153
|
-
##
|
154
|
-
# Store the file on Cloud Files
|
155
|
-
#
|
156
|
-
# === Parameters
|
157
|
-
#
|
158
|
-
# [file (CarrierWave::SanitizedFile)] the file to store
|
159
|
-
#
|
160
|
-
# === Returns
|
161
|
-
#
|
162
|
-
# [CarrierWave::Storage::CloudFiles::File] the stored file
|
163
|
-
#
|
164
|
-
def store!(file)
|
165
|
-
cloud_files_options = {'Content-Type' => file.content_type}
|
166
|
-
f = CarrierWave::Storage::CloudFiles::File.new(uploader, self, uploader.store_path)
|
167
|
-
f.store(file.read,cloud_files_options)
|
168
|
-
f
|
169
|
-
end
|
170
|
-
|
171
|
-
# Do something to retrieve the file
|
172
|
-
#
|
173
|
-
# @param [String] identifier uniquely identifies the file
|
174
|
-
#
|
175
|
-
# [identifier (String)] uniquely identifies the file
|
176
|
-
#
|
177
|
-
# === Returns
|
178
|
-
#
|
179
|
-
# [CarrierWave::Storage::CloudFiles::File] the stored file
|
180
|
-
#
|
181
|
-
def retrieve!(identifier)
|
182
|
-
CarrierWave::Storage::CloudFiles::File.new(uploader, self, uploader.store_path(identifier))
|
183
|
-
end
|
184
|
-
|
185
|
-
|
186
|
-
end # CloudFiles
|
187
|
-
end # Storage
|
188
|
-
end # CarrierWave
|
@@ -1 +0,0 @@
|
|
1
|
-
raise "The right_aws library is no longer supported. Please install the 'fog' gem instead."
|
@@ -1,240 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
begin
|
3
|
-
require 'fog'
|
4
|
-
rescue LoadError
|
5
|
-
raise "You don't have the 'fog' gem installed. The 'aws', 'aws-s3' and 'right_aws' gems are no longer supported."
|
6
|
-
end
|
7
|
-
|
8
|
-
module CarrierWave
|
9
|
-
module Storage
|
10
|
-
|
11
|
-
##
|
12
|
-
# Uploads things to Amazon S3 using the "fog" gem.
|
13
|
-
# You'll need to specify the access_key_id, secret_access_key and bucket.
|
14
|
-
#
|
15
|
-
# CarrierWave.configure do |config|
|
16
|
-
# config.s3_access_key_id = "xxxxxx"
|
17
|
-
# config.s3_secret_access_key = "xxxxxx"
|
18
|
-
# config.s3_bucket = "my_bucket_name"
|
19
|
-
# end
|
20
|
-
#
|
21
|
-
# You can set the access policy for the uploaded files:
|
22
|
-
#
|
23
|
-
# CarrierWave.configure do |config|
|
24
|
-
# config.s3_access_policy = :public_read
|
25
|
-
# end
|
26
|
-
#
|
27
|
-
# The default is :public_read. For more options see:
|
28
|
-
#
|
29
|
-
# http://docs.amazonwebservices.com/AmazonS3/latest/RESTAccessPolicy.html#RESTCannedAccessPolicies
|
30
|
-
#
|
31
|
-
# The following access policies are available:
|
32
|
-
#
|
33
|
-
# [:private] No one else has any access rights.
|
34
|
-
# [:public_read] The anonymous principal is granted READ access.
|
35
|
-
# If this policy is used on an object, it can be read from a
|
36
|
-
# browser with no authentication.
|
37
|
-
# [:public_read_write] The anonymous principal is granted READ and WRITE access.
|
38
|
-
# [:authenticated_read] Any principal authenticated as a registered Amazon S3 user
|
39
|
-
# is granted READ access.
|
40
|
-
#
|
41
|
-
# You can change the generated url to a cnamed domain by setting the cnamed config:
|
42
|
-
#
|
43
|
-
# CarrierWave.configure do |config|
|
44
|
-
# config.s3_cnamed = true
|
45
|
-
# config.s3_bucket = 'bucketname.domain.tld'
|
46
|
-
# end
|
47
|
-
#
|
48
|
-
# Now the resulting url will be
|
49
|
-
#
|
50
|
-
# http://bucketname.domain.tld/path/to/file
|
51
|
-
#
|
52
|
-
# instead of
|
53
|
-
#
|
54
|
-
# http://bucketname.domain.tld.s3.amazonaws.com/path/to/file
|
55
|
-
#
|
56
|
-
# You can specify a region. US Standard "us-east-1" is the default.
|
57
|
-
#
|
58
|
-
# CarrierWave.configure do |config|
|
59
|
-
# config.s3_region = 'eu-west-1'
|
60
|
-
# end
|
61
|
-
#
|
62
|
-
# Available options are defined in Fog Storage[http://github.com/geemus/fog/blob/master/lib/fog/aws/storage.rb]
|
63
|
-
#
|
64
|
-
# 'eu-west-1' => 's3-eu-west-1.amazonaws.com'
|
65
|
-
# 'us-east-1' => 's3.amazonaws.com'
|
66
|
-
# 'ap-southeast-1' => 's3-ap-southeast-1.amazonaws.com'
|
67
|
-
# 'us-west-1' => 's3-us-west-1.amazonaws.com'
|
68
|
-
#
|
69
|
-
class S3 < Abstract
|
70
|
-
|
71
|
-
class File
|
72
|
-
|
73
|
-
def initialize(uploader, base, path)
|
74
|
-
@uploader = uploader
|
75
|
-
@path = path
|
76
|
-
@base = base
|
77
|
-
end
|
78
|
-
|
79
|
-
##
|
80
|
-
# Returns the current path of the file on S3
|
81
|
-
#
|
82
|
-
# === Returns
|
83
|
-
#
|
84
|
-
# [String] A path
|
85
|
-
#
|
86
|
-
def path
|
87
|
-
@path
|
88
|
-
end
|
89
|
-
|
90
|
-
##
|
91
|
-
# Reads the contents of the file from S3
|
92
|
-
#
|
93
|
-
# === Returns
|
94
|
-
#
|
95
|
-
# [String] contents of the file
|
96
|
-
#
|
97
|
-
def read
|
98
|
-
result = connection.get_object(bucket, @path)
|
99
|
-
@headers = result.headers
|
100
|
-
result.body
|
101
|
-
end
|
102
|
-
|
103
|
-
##
|
104
|
-
# Remove the file from Amazon S3
|
105
|
-
#
|
106
|
-
def delete
|
107
|
-
connection.delete_object(bucket, @path)
|
108
|
-
end
|
109
|
-
|
110
|
-
##
|
111
|
-
# Returns the url on Amazon's S3 service
|
112
|
-
#
|
113
|
-
# === Returns
|
114
|
-
#
|
115
|
-
# [String] file's url
|
116
|
-
#
|
117
|
-
def url
|
118
|
-
if access_policy == :authenticated_read
|
119
|
-
authenticated_url
|
120
|
-
else
|
121
|
-
public_url
|
122
|
-
end
|
123
|
-
end
|
124
|
-
|
125
|
-
def public_url
|
126
|
-
scheme = use_ssl? ? 'https' : 'http'
|
127
|
-
if cnamed?
|
128
|
-
["#{scheme}://#{bucket}", path].compact.join('/')
|
129
|
-
else
|
130
|
-
["#{scheme}://#{bucket}.s3.amazonaws.com", path].compact.join('/')
|
131
|
-
end
|
132
|
-
end
|
133
|
-
|
134
|
-
def authenticated_url
|
135
|
-
connection.get_object_https_url(bucket, path, Time.now + authentication_timeout)
|
136
|
-
end
|
137
|
-
|
138
|
-
def store(file)
|
139
|
-
content_type ||= file.content_type # this might cause problems if content type changes between read and upload (unlikely)
|
140
|
-
connection.put_object(bucket, path, file.read,
|
141
|
-
{
|
142
|
-
'x-amz-acl' => access_policy.to_s.gsub('_', '-'),
|
143
|
-
'Content-Type' => content_type
|
144
|
-
}.merge(@uploader.s3_headers)
|
145
|
-
)
|
146
|
-
end
|
147
|
-
|
148
|
-
def content_type
|
149
|
-
headers["Content-Type"]
|
150
|
-
end
|
151
|
-
|
152
|
-
def content_type=(type)
|
153
|
-
headers["Content-Type"] = type
|
154
|
-
end
|
155
|
-
|
156
|
-
def size
|
157
|
-
headers['Content-Length'].to_i
|
158
|
-
end
|
159
|
-
|
160
|
-
# Headers returned from file retrieval
|
161
|
-
def headers
|
162
|
-
@headers ||= begin
|
163
|
-
connection.head_object(bucket, @path).headers
|
164
|
-
rescue Excon::Errors::NotFound # Don't die, just return no headers
|
165
|
-
{}
|
166
|
-
end
|
167
|
-
end
|
168
|
-
|
169
|
-
private
|
170
|
-
|
171
|
-
def use_ssl?
|
172
|
-
@uploader.s3_use_ssl
|
173
|
-
end
|
174
|
-
|
175
|
-
def cnamed?
|
176
|
-
@uploader.s3_cnamed
|
177
|
-
end
|
178
|
-
|
179
|
-
def access_policy
|
180
|
-
@uploader.s3_access_policy
|
181
|
-
end
|
182
|
-
|
183
|
-
def bucket
|
184
|
-
@uploader.s3_bucket
|
185
|
-
end
|
186
|
-
|
187
|
-
def authentication_timeout
|
188
|
-
@uploader.s3_authentication_timeout
|
189
|
-
end
|
190
|
-
|
191
|
-
def connection
|
192
|
-
@base.connection
|
193
|
-
end
|
194
|
-
|
195
|
-
end
|
196
|
-
|
197
|
-
##
|
198
|
-
# Store the file on S3
|
199
|
-
#
|
200
|
-
# === Parameters
|
201
|
-
#
|
202
|
-
# [file (CarrierWave::SanitizedFile)] the file to store
|
203
|
-
#
|
204
|
-
# === Returns
|
205
|
-
#
|
206
|
-
# [CarrierWave::Storage::S3::File] the stored file
|
207
|
-
#
|
208
|
-
def store!(file)
|
209
|
-
f = CarrierWave::Storage::S3::File.new(uploader, self, uploader.store_path)
|
210
|
-
f.store(file)
|
211
|
-
f
|
212
|
-
end
|
213
|
-
|
214
|
-
# Do something to retrieve the file
|
215
|
-
#
|
216
|
-
# @param [String] identifier uniquely identifies the file
|
217
|
-
#
|
218
|
-
# [identifier (String)] uniquely identifies the file
|
219
|
-
#
|
220
|
-
# === Returns
|
221
|
-
#
|
222
|
-
# [CarrierWave::Storage::S3::File] the stored file
|
223
|
-
#
|
224
|
-
def retrieve!(identifier)
|
225
|
-
CarrierWave::Storage::S3::File.new(uploader, self, uploader.store_path(identifier))
|
226
|
-
end
|
227
|
-
|
228
|
-
def connection
|
229
|
-
@connection ||= ::Fog::Storage.new(
|
230
|
-
:aws_access_key_id => uploader.s3_access_key_id,
|
231
|
-
:aws_secret_access_key => uploader.s3_secret_access_key,
|
232
|
-
:provider => 'AWS',
|
233
|
-
:region => uploader.s3_region
|
234
|
-
)
|
235
|
-
end
|
236
|
-
|
237
|
-
end # S3
|
238
|
-
end # Storage
|
239
|
-
end # CarrierWave
|
240
|
-
|