plowdawg-carrierwave 0.5.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. data/README.md +674 -0
  2. data/lib/carrierwave.rb +109 -0
  3. data/lib/carrierwave/compatibility/paperclip.rb +95 -0
  4. data/lib/carrierwave/locale/en.yml +5 -0
  5. data/lib/carrierwave/mount.rb +382 -0
  6. data/lib/carrierwave/orm/activerecord.rb +46 -0
  7. data/lib/carrierwave/processing/mime_types.rb +58 -0
  8. data/lib/carrierwave/processing/mini_magick.rb +253 -0
  9. data/lib/carrierwave/processing/rmagick.rb +279 -0
  10. data/lib/carrierwave/sanitized_file.rb +302 -0
  11. data/lib/carrierwave/storage/abstract.rb +30 -0
  12. data/lib/carrierwave/storage/cloud_files.rb +188 -0
  13. data/lib/carrierwave/storage/file.rb +47 -0
  14. data/lib/carrierwave/storage/fog.rb +332 -0
  15. data/lib/carrierwave/storage/right_s3.rb +1 -0
  16. data/lib/carrierwave/storage/s3.rb +240 -0
  17. data/lib/carrierwave/test/matchers.rb +164 -0
  18. data/lib/carrierwave/uploader.rb +44 -0
  19. data/lib/carrierwave/uploader/cache.rb +160 -0
  20. data/lib/carrierwave/uploader/callbacks.rb +35 -0
  21. data/lib/carrierwave/uploader/configuration.rb +162 -0
  22. data/lib/carrierwave/uploader/default_url.rb +19 -0
  23. data/lib/carrierwave/uploader/download.rb +75 -0
  24. data/lib/carrierwave/uploader/extension_whitelist.rb +49 -0
  25. data/lib/carrierwave/uploader/mountable.rb +39 -0
  26. data/lib/carrierwave/uploader/processing.rb +90 -0
  27. data/lib/carrierwave/uploader/proxy.rb +77 -0
  28. data/lib/carrierwave/uploader/remove.rb +23 -0
  29. data/lib/carrierwave/uploader/store.rb +113 -0
  30. data/lib/carrierwave/uploader/url.rb +45 -0
  31. data/lib/carrierwave/uploader/versions.rb +237 -0
  32. data/lib/carrierwave/validations/active_model.rb +79 -0
  33. data/lib/carrierwave/version.rb +3 -0
  34. data/lib/generators/templates/uploader.rb +49 -0
  35. data/lib/generators/uploader_generator.rb +7 -0
  36. metadata +215 -0
@@ -0,0 +1,47 @@
1
+ # encoding: utf-8
2
+
3
+ module CarrierWave
4
+ module Storage
5
+
6
+ ##
7
+ # File storage stores file to the Filesystem (surprising, no?). There's really not much
8
+ # to it, it uses the store_dir defined on the uploader as the storage location. That's
9
+ # pretty much it.
10
+ #
11
+ class File < Abstract
12
+
13
+ ##
14
+ # Move the file to the uploader's store path.
15
+ #
16
+ # === Parameters
17
+ #
18
+ # [file (CarrierWave::SanitizedFile)] the file to store
19
+ #
20
+ # === Returns
21
+ #
22
+ # [CarrierWave::SanitizedFile] a sanitized file
23
+ #
24
+ def store!(file)
25
+ path = ::File.expand_path(uploader.store_path, uploader.root)
26
+ file.copy_to(path, uploader.permissions)
27
+ end
28
+
29
+ ##
30
+ # Retrieve the file from its store path
31
+ #
32
+ # === Parameters
33
+ #
34
+ # [identifier (String)] the filename of the file
35
+ #
36
+ # === Returns
37
+ #
38
+ # [CarrierWave::SanitizedFile] a sanitized file
39
+ #
40
+ def retrieve!(identifier)
41
+ path = ::File.expand_path(uploader.store_path(identifier), uploader.root)
42
+ CarrierWave::SanitizedFile.new(path)
43
+ end
44
+
45
+ end # File
46
+ end # Storage
47
+ end # CarrierWave
@@ -0,0 +1,332 @@
1
+ # encoding: utf-8
2
+
3
+ begin
4
+ require 'fog'
5
+ rescue LoadError
6
+ raise "You don't have the 'fog' gem installed"
7
+ end
8
+
9
+ module CarrierWave
10
+ module Storage
11
+
12
+ ##
13
+ # Stores things using the "fog" gem.
14
+ #
15
+ # fog supports storing files with AWS, Google, Local and Rackspace
16
+ #
17
+ # You need to setup some options to configure your usage:
18
+ #
19
+ # [:fog_credentials] credentials for service
20
+ # [:fog_directory] specifies name of directory to store data in, assumed to already exist
21
+ #
22
+ # [:fog_attributes] (optional) additional attributes to set on files
23
+ # [:fog_host] (optional) non-default host to serve files from
24
+ # [:fog_public] (optional) public readability, defaults to true
25
+ # [:fog_authenticated_url_expiration] (optional) time (in seconds) that authenticated urls
26
+ # will be valid, when fog_public is false and provider is AWS or Google, defaults to 600
27
+ #
28
+ #
29
+ # AWS credentials contain the following keys:
30
+ #
31
+ # [:aws_access_key_id]
32
+ # [:aws_secret_access_key]
33
+ # [:region] (optional) defaults to 'us-east-1'
34
+ # :region should be one of ['eu-west-1', 'us-east-1', 'ap-southeast-1', 'us-west-1', 'ap-northeast-1']
35
+ #
36
+ #
37
+ # Google credentials contain the following keys:
38
+ # [:google_storage_access_key_id]
39
+ # [:google_storage_secrete_access_key]
40
+ #
41
+ #
42
+ # Local credentials contain the following keys:
43
+ #
44
+ # [:local_root] local path to files
45
+ #
46
+ #
47
+ # Rackspace credentials contain the following keys:
48
+ #
49
+ # [:rackspace_username]
50
+ # [:rackspace_api_key]
51
+ #
52
+ #
53
+ # A full example with AWS credentials:
54
+ # CarrierWave.configure do |config|
55
+ # config.fog_credentials = {
56
+ # :aws_access_key_id => 'xxxxxx',
57
+ # :aws_secret_access_key => 'yyyyyy',
58
+ # :provider => 'AWS'
59
+ # }
60
+ # config.fog_directory = 'directoryname'
61
+ # config.fog_public = true
62
+ # end
63
+ #
64
+ class Fog < Abstract
65
+
66
+ ##
67
+ # Store a file
68
+ #
69
+ # === Parameters
70
+ #
71
+ # [file (CarrierWave::SanitizedFile)] the file to store
72
+ #
73
+ # === Returns
74
+ #
75
+ # [CarrierWave::Storage::Fog::File] the stored file
76
+ #
77
+ def store!(file)
78
+ f = CarrierWave::Storage::Fog::File.new(uploader, self, uploader.store_path)
79
+ f.store(file)
80
+ f
81
+ end
82
+
83
+ ##
84
+ # Retrieve a file
85
+ #
86
+ # === Parameters
87
+ #
88
+ # [identifier (String)] unique identifier for file
89
+ #
90
+ # === Returns
91
+ #
92
+ # [CarrierWave::Storage::Fog::File] the stored file
93
+ #
94
+ def retrieve!(identifier)
95
+ CarrierWave::Storage::Fog::File.new(uploader, self, uploader.store_path(identifier))
96
+ end
97
+
98
+ def connection
99
+ @connection ||= begin
100
+ ::Fog::Storage.new(uploader.fog_credentials)
101
+ end
102
+ end
103
+
104
+ class File
105
+
106
+ ##
107
+ # Current local path to file
108
+ #
109
+ # === Returns
110
+ #
111
+ # [String] a path to file
112
+ #
113
+ attr_reader :path
114
+
115
+ ##
116
+ # Return all attributes from file
117
+ #
118
+ # === Returns
119
+ #
120
+ # [Hash] attributes from file
121
+ #
122
+ def attributes
123
+ file.attributes
124
+ end
125
+
126
+ ##
127
+ # Return a temporary authenticated url to a private file, if available
128
+ # Only supported for AWS and Google providers
129
+ #
130
+ # === Returns
131
+ #
132
+ # [String] temporary authenticated url
133
+ # or
134
+ # [NilClass] no authenticated url available
135
+ #
136
+ def authenticated_url
137
+ if ['AWS', 'Google'].include?(@uploader.fog_credentials[:provider])
138
+ # avoid a get by using local references
139
+ local_directory = connection.directories.new(:key => @uploader.fog_directory)
140
+ local_file = local_directory.files.new(:key => path)
141
+ local_file.url(::Fog::Time.now + @uploader.fog_authenticated_url_expiration)
142
+ else
143
+ nil
144
+ end
145
+ end
146
+
147
+ ##
148
+ # Lookup value for file content-type header
149
+ #
150
+ # === Returns
151
+ #
152
+ # [String] value of content-type
153
+ #
154
+ def content_type
155
+ @content_type || file.content_type
156
+ end
157
+
158
+ ##
159
+ # Set non-default content-type header (default is file.content_type)
160
+ #
161
+ # === Returns
162
+ #
163
+ # [String] returns new content type value
164
+ #
165
+ def content_type=(new_content_type)
166
+ @content_type = new_content_type
167
+ end
168
+
169
+ ##
170
+ # Remove the file from service
171
+ #
172
+ # === Returns
173
+ #
174
+ # [Boolean] true for success or raises error
175
+ #
176
+ def delete
177
+ # avoid a get by just using local reference
178
+ directory.files.new(:key => path).destroy
179
+ end
180
+
181
+ ##
182
+ # deprecated: All attributes from file (includes headers)
183
+ #
184
+ # === Returns
185
+ #
186
+ # [Hash] attributes from file
187
+ #
188
+ def headers
189
+ location = caller.first
190
+ warning = "[yellow][WARN] headers is deprecated, use attributes instead[/]"
191
+ warning << " [light_black](#{location})[/]"
192
+ Formatador.display_line(warning)
193
+ attributes
194
+ end
195
+
196
+ def initialize(uploader, base, path)
197
+ @uploader, @base, @path = uploader, base, path
198
+ end
199
+
200
+ ##
201
+ # Read content of file from service
202
+ #
203
+ # === Returns
204
+ #
205
+ # [String] contents of file
206
+ def read
207
+ file.body
208
+ end
209
+
210
+ ##
211
+ # Return size of file body
212
+ #
213
+ # === Returns
214
+ #
215
+ # [Integer] size of file body
216
+ #
217
+ def size
218
+ file.content_length
219
+ end
220
+
221
+ ##
222
+ # Write file to service
223
+ #
224
+ # === Returns
225
+ #
226
+ # [Boolean] true on success or raises error
227
+ def store(new_file)
228
+ @content_type ||= new_file.content_type
229
+ @file = directory.files.create({
230
+ :body => new_file.read,
231
+ :content_type => @content_type,
232
+ :key => path,
233
+ :public => @uploader.fog_public
234
+ }.merge(@uploader.fog_attributes))
235
+ true
236
+ end
237
+
238
+ ##
239
+ # Return a url to a public file, if available
240
+ #
241
+ # === Returns
242
+ #
243
+ # [String] public url
244
+ # or
245
+ # [NilClass] no public url available
246
+ #
247
+ def public_url
248
+ if host = @uploader.fog_host
249
+ "#{host}/#{path}"
250
+ else
251
+ # AWS/Google optimized for speed over correctness
252
+ case @uploader.fog_credentials[:provider]
253
+ when 'AWS'
254
+ # if directory is a valid subdomain, use that style for access
255
+ if @uploader.fog_directory.to_s =~ /^(?:[a-z]|\d(?!\d{0,2}(?:\.\d{1,3}){3}$))(?:[a-z0-9]|\.(?![\.\-])|\-(?![\.])){1,61}[a-z0-9]$/
256
+ "https://#{@uploader.fog_directory}.s3.amazonaws.com/#{path}"
257
+ else
258
+ # directory is not a valid subdomain, so use path style for access
259
+ "https://s3.amazonaws.com/#{@uploader.fog_directory}/#{path}"
260
+ end
261
+ when 'Google'
262
+ "https://commondatastorage.googleapis.com/#{@uploader.fog_directory}/#{path}"
263
+ else
264
+ # avoid a get by just using local reference
265
+ directory.files.new(:key => path).public_url
266
+ end
267
+ end
268
+ end
269
+
270
+ ##
271
+ # Return url to file, if avaliable
272
+ #
273
+ # === Returns
274
+ #
275
+ # [String] url
276
+ # or
277
+ # [NilClass] no url available
278
+ #
279
+ def url
280
+ if !@uploader.fog_public
281
+ authenticated_url
282
+ else
283
+ public_url
284
+ end
285
+ end
286
+
287
+ private
288
+
289
+ ##
290
+ # connection to service
291
+ #
292
+ # === Returns
293
+ #
294
+ # [Fog::#{provider}::Storage] connection to service
295
+ #
296
+ def connection
297
+ @base.connection
298
+ end
299
+
300
+ ##
301
+ # local reference to directory containing file
302
+ #
303
+ # === Returns
304
+ #
305
+ # [Fog::#{provider}::Directory] containing directory
306
+ #
307
+ def directory
308
+ @directory ||= begin
309
+ connection.directories.new(
310
+ :key => @uploader.fog_directory,
311
+ :public => @uploader.fog_public
312
+ )
313
+ end
314
+ end
315
+
316
+ ##
317
+ # lookup file
318
+ #
319
+ # === Returns
320
+ #
321
+ # [Fog::#{provider}::File] file data from remote service
322
+ #
323
+ def file
324
+ @file ||= directory.files.get(path)
325
+ end
326
+
327
+ end
328
+
329
+ end # Fog
330
+
331
+ end # Storage
332
+ end # CarrierWave
@@ -0,0 +1 @@
1
+ raise "The right_aws library is no longer supported. Please install the 'fog' gem instead."
@@ -0,0 +1,240 @@
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_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
+