carrierwave 0.4.5 → 0.4.6

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.

Files changed (92) hide show
  1. data/README.rdoc +33 -15
  2. data/lib/carrierwave.rb +0 -1
  3. data/lib/carrierwave/compatibility/paperclip.rb +2 -2
  4. data/lib/carrierwave/orm/activerecord.rb +9 -5
  5. data/lib/carrierwave/orm/datamapper.rb +10 -0
  6. data/lib/carrierwave/processing/mini_magick.rb +1 -1
  7. data/lib/carrierwave/processing/rmagick.rb +4 -4
  8. data/lib/carrierwave/sanitized_file.rb +2 -2
  9. data/lib/carrierwave/storage/cloud_files.rb +11 -12
  10. data/lib/carrierwave/storage/grid_fs.rb +37 -26
  11. data/lib/carrierwave/storage/right_s3.rb +1 -168
  12. data/lib/carrierwave/storage/s3.rb +73 -66
  13. data/lib/carrierwave/test/matchers.rb +57 -21
  14. data/lib/carrierwave/uploader/configuration.rb +8 -5
  15. data/lib/carrierwave/uploader/processing.rb +1 -0
  16. data/lib/carrierwave/uploader/url.rb +2 -2
  17. data/lib/carrierwave/uploader/versions.rb +10 -0
  18. metadata +31 -279
  19. data/Generators +0 -4
  20. data/History.txt +0 -125
  21. data/Manifest.txt +0 -111
  22. data/Rakefile +0 -39
  23. data/carrierwave.gemspec +0 -85
  24. data/cucumber.yml +0 -2
  25. data/features/caching.feature +0 -28
  26. data/features/download.feature +0 -20
  27. data/features/file_storage.feature +0 -37
  28. data/features/file_storage_overridden_filename.feature +0 -38
  29. data/features/file_storage_overridden_store_dir.feature +0 -38
  30. data/features/file_storage_reversing_processor.feature +0 -43
  31. data/features/fixtures/bork.txt +0 -1
  32. data/features/fixtures/monkey.txt +0 -1
  33. data/features/grid_fs_storage.feature +0 -32
  34. data/features/mount_activerecord.feature +0 -46
  35. data/features/mount_datamapper.feature +0 -46
  36. data/features/step_definitions/activerecord_steps.rb +0 -22
  37. data/features/step_definitions/caching_steps.rb +0 -14
  38. data/features/step_definitions/datamapper_steps.rb +0 -29
  39. data/features/step_definitions/download_steps.rb +0 -4
  40. data/features/step_definitions/file_steps.rb +0 -53
  41. data/features/step_definitions/general_steps.rb +0 -85
  42. data/features/step_definitions/mount_steps.rb +0 -19
  43. data/features/step_definitions/store_steps.rb +0 -18
  44. data/features/support/activerecord.rb +0 -30
  45. data/features/support/datamapper.rb +0 -7
  46. data/features/support/env.rb +0 -22
  47. data/features/versions_basics.feature +0 -50
  48. data/features/versions_nested_versions.feature +0 -70
  49. data/features/versions_overridden_filename.feature +0 -51
  50. data/features/versions_overriden_store_dir.feature +0 -41
  51. data/lib/carrierwave/orm/mongomapper.rb +0 -27
  52. data/merb_generators/uploader_generator.rb +0 -22
  53. data/rails_generators/uploader/USAGE +0 -2
  54. data/rails_generators/uploader/templates/uploader.rb +0 -47
  55. data/rails_generators/uploader/uploader_generator.rb +0 -21
  56. data/script/console +0 -10
  57. data/script/destroy +0 -14
  58. data/script/generate +0 -14
  59. data/spec/compatibility/paperclip_spec.rb +0 -52
  60. data/spec/fixtures/bork.txt +0 -1
  61. data/spec/fixtures/landscape.jpg +0 -0
  62. data/spec/fixtures/portrait.jpg +0 -0
  63. data/spec/fixtures/test.jpeg +0 -1
  64. data/spec/fixtures/test.jpg +0 -1
  65. data/spec/mount_spec.rb +0 -538
  66. data/spec/orm/activerecord_spec.rb +0 -271
  67. data/spec/orm/datamapper_spec.rb +0 -168
  68. data/spec/orm/mongoid_spec.rb +0 -202
  69. data/spec/orm/mongomapper_spec.rb +0 -202
  70. data/spec/orm/sequel_spec.rb +0 -183
  71. data/spec/processing/image_science_spec.rb +0 -56
  72. data/spec/processing/mini_magick_spec.rb +0 -76
  73. data/spec/processing/rmagick_spec.rb +0 -75
  74. data/spec/sanitized_file_spec.rb +0 -623
  75. data/spec/spec_helper.rb +0 -92
  76. data/spec/storage/cloudfiles_spec.rb +0 -78
  77. data/spec/storage/grid_fs_spec.rb +0 -83
  78. data/spec/storage/right_s3_spec.rb +0 -83
  79. data/spec/storage/s3_spec.rb +0 -95
  80. data/spec/uploader/cache_spec.rb +0 -209
  81. data/spec/uploader/configuration_spec.rb +0 -105
  82. data/spec/uploader/default_url_spec.rb +0 -85
  83. data/spec/uploader/download_spec.rb +0 -75
  84. data/spec/uploader/extension_whitelist_spec.rb +0 -44
  85. data/spec/uploader/mountable_spec.rb +0 -33
  86. data/spec/uploader/paths_spec.rb +0 -22
  87. data/spec/uploader/processing_spec.rb +0 -73
  88. data/spec/uploader/proxy_spec.rb +0 -54
  89. data/spec/uploader/remove_spec.rb +0 -70
  90. data/spec/uploader/store_spec.rb +0 -264
  91. data/spec/uploader/url_spec.rb +0 -102
  92. data/spec/uploader/versions_spec.rb +0 -298
@@ -66,7 +66,7 @@ Most of the time you are going to want to use CarrierWave together with an ORM.
66
66
  It is quite simple to mount uploaders on columns in your model, so you can
67
67
  simply assign files and get going:
68
68
 
69
- === ActiveRecord, DataMapper, Sequel, MongoMapper
69
+ === ActiveRecord, DataMapper, Sequel, Mongoid
70
70
 
71
71
  Make sure you are loading CarrierWave after loading your ORM, otherwise you'll
72
72
  need to require the relevant extension manually, e.g.:
@@ -169,7 +169,7 @@ in that case. Suppose your +user+ model has an uploader mounted on +avatar+
169
169
  file, just add a hidden field called +avatar_cache+. In Rails, this would look
170
170
  like this:
171
171
 
172
- <% form_for @user do |f| %>
172
+ <% form_for @user, :html => {:multipart => true} do |f| %>
173
173
  <p>
174
174
  <label>My Avatar</label>
175
175
  <%= f.file_field :avatar %>
@@ -180,7 +180,7 @@ like this:
180
180
  It might be a good idea to show the user that a file has been uploaded, in the
181
181
  case of images, a small thumbnail would be a good indicator:
182
182
 
183
- <% form_for @user do |f| %>
183
+ <% form_for @user, :html => {:multipart => true} do |f| %>
184
184
  <p>
185
185
  <label>My Avatar</label>
186
186
  <%= image_tag(@user.avatar_url) if @user.avatar? %>
@@ -194,7 +194,7 @@ case of images, a small thumbnail would be a good indicator:
194
194
  If you want to remove a previously uploaded file on a mounted uploader, you can
195
195
  easily add a checkbox to the form which will remove the file when checked.
196
196
 
197
- <% form_for @user do |f| %>
197
+ <% form_for @user, :html => {:multipart => true} do |f| %>
198
198
  <p>
199
199
  <label>My Avatar</label>
200
200
  <%= image_tag(@user.avatar_url) if @user.avatar? %>
@@ -217,7 +217,7 @@ Your users may find it convenient to upload a file from a location on the Intern
217
217
  via a URL. CarrierWave makes this simple, just add the appropriate column to your
218
218
  form and you're good to go:
219
219
 
220
- <% form_for @user do |f| %>
220
+ <% form_for @user, :html => {:multipart => true} do |f| %>
221
221
  <p>
222
222
  <label>My Avatar URL:</label>
223
223
  <%= image_tag(@user.avatar_url) if @user.avatar? %>
@@ -237,6 +237,22 @@ this easily by overriding the +default_url+ method in your uploader:
237
237
  end
238
238
  end
239
239
 
240
+ == Recreating versions
241
+
242
+ You might come to a situation where you want to retroactively change a version
243
+ or add a new one. You can use the recreate_versions! method to recreate the
244
+ versions from the base file. This uses a naive approach which will reupload and
245
+ process all versions.
246
+
247
+ instance = MyUploader.new
248
+ instance.recreate_versions!
249
+
250
+ Or on a mounted uploader:
251
+
252
+ User.all.each do |user|
253
+ user.avatar.recreate_versions!
254
+ end
255
+
240
256
  == Configuring CarrierWave
241
257
 
242
258
  CarrierWave has a broad range of configuration options, which you can configure,
@@ -303,6 +319,11 @@ CarrierWave comes with some RSpec matchers which you may find useful:
303
319
 
304
320
  == Using Amazon S3
305
321
 
322
+ Older versions of CarrierWave used the +aws-s3+ and +right_aws+ libraries. The Aws[http://github.com/appoxy/aws]
323
+ is now used meaning european buckets are supported out the box. Ensure you have it installed:
324
+
325
+ gem install aws
326
+
306
327
  You'll need to configure a bucket, access id and secret key like this:
307
328
 
308
329
  CarrierWave.configure do |config|
@@ -322,15 +343,6 @@ And then in your uploader, set the storage to :s3
322
343
  That's it! You can still use the <code>CarrierWave::Uploader#url</code> method to return
323
344
  the url to the file on Amazon S3.
324
345
 
325
- Alternatively, and especially if your bucket is located in Europe, you can use the
326
- RightAWS library by setting the storage to :right_s3
327
-
328
- class AvatarUploader < CarrierWave::Uploader::Base
329
- storage :right_s3
330
- end
331
-
332
- CarrierWave uses the RightAWS S3 Interface directly, meaning that the performance issues
333
- mentioned by Jonathan Yurek for paperclip do not apply: http://groups.google.com/group/paperclip-plugin/browse_thread/thread/d4dc166a9a5f0df4#
334
346
 
335
347
  == Using Rackspace Cloud Files
336
348
 
@@ -338,11 +350,17 @@ Cloud Files support requires a {Rackspace Cloud}[http://rackspacecloud.com] user
338
350
  You must also create a container for Carrierwave to use, and mark it public (publish it to the CDN)
339
351
 
340
352
  CarrierWave.configure do |config|
341
- config.cloud_files_user_name = 'xxxxxx'
353
+ config.cloud_files_username = 'xxxxxx'
342
354
  config.cloud_files_api_key = 'xxxxxxxxxxxxxxxxxxxxx'
343
355
  config.cloud_files_container = 'name_of_bucket'
344
356
  end
345
357
 
358
+ You can optionally include your CDN host name in the configuration.
359
+ This is *highly* recommended, as without it every request requires a lookup
360
+ of this information.
361
+
362
+ config.cloud_files_cdn_host = "c000000.cdn.rackspacecloud.com"
363
+
346
364
  Do this in an initializer in Rails, and in a +before_app_loads+ block in Merb.
347
365
 
348
366
  And then in your uploader, set the storage to :cloud_files
@@ -94,5 +94,4 @@ end
94
94
  require File.join(File.dirname(__FILE__), "carrierwave", "orm", 'activerecord') if defined?(ActiveRecord)
95
95
  require File.join(File.dirname(__FILE__), "carrierwave", "orm", 'datamapper') if defined?(DataMapper)
96
96
  require File.join(File.dirname(__FILE__), "carrierwave", "orm", 'sequel') if defined?(Sequel)
97
- require File.join(File.dirname(__FILE__), "carrierwave", "orm", "mongomapper") if defined?(MongoMapper)
98
97
  require File.join(File.dirname(__FILE__), "carrierwave", "orm", "mongoid") if defined?(Mongoid)
@@ -72,7 +72,7 @@ module CarrierWave
72
72
 
73
73
  def interpolate_paperclip_path(path, filename)
74
74
  mappings.inject(path) do |agg, pair|
75
- agg.gsub(":#{pair[0]}", pair[1].call(self, filename).to_s)
75
+ agg.gsub(":#{pair[0]}") { pair[1].call(self, filename).to_s }
76
76
  end
77
77
  end
78
78
 
@@ -92,4 +92,4 @@ module CarrierWave
92
92
 
93
93
  end # Paperclip
94
94
  end # Compatibility
95
- end # CarrierWave
95
+ end # CarrierWave
@@ -6,7 +6,7 @@ module CarrierWave
6
6
  module ActiveRecord
7
7
 
8
8
  include CarrierWave::Mount
9
-
9
+
10
10
  ##
11
11
  # See +CarrierWave::Mount#mount_uploader+ for documentation
12
12
  #
@@ -39,9 +39,11 @@ module CarrierWave
39
39
  #
40
40
  def validates_integrity_of(*attrs)
41
41
  options = attrs.last.is_a?(Hash) ? attrs.last : {}
42
- options[:message] ||= I18n.t('carrierwave.errors.integrity', :default => 'is not an allowed type of file.')
43
42
  validates_each(*attrs) do |record, attr, value|
44
- record.errors.add attr, options[:message] if record.send("#{attr}_integrity_error")
43
+ if record.send("#{attr}_integrity_error")
44
+ message = options[:message] || I18n.t('carrierwave.errors.integrity', :default => 'is not an allowed type of file.')
45
+ record.errors.add attr, message
46
+ end
45
47
  end
46
48
  end
47
49
 
@@ -61,9 +63,11 @@ module CarrierWave
61
63
  #
62
64
  def validates_processing_of(*attrs)
63
65
  options = attrs.last.is_a?(Hash) ? attrs.last : {}
64
- options[:message] ||= I18n.t('carrierwave.errors.processing', :default => 'failed to be processed.')
65
66
  validates_each(*attrs) do |record, attr, value|
66
- record.errors.add attr, options[:message] if record.send("#{attr}_processing_error")
67
+ if record.send("#{attr}_processing_error")
68
+ message = options[:message] || I18n.t('carrierwave.errors.processing', :default => 'failed to be processed.')
69
+ record.errors.add attr, message
70
+ end
67
71
  end
68
72
  end
69
73
 
@@ -19,6 +19,16 @@ module CarrierWave
19
19
  pre_hook = ::DataMapper.const_defined?(:Validate) ? :valid? : :save
20
20
  before pre_hook, "write_#{column}_identifier".to_sym
21
21
  after :destroy, "remove_#{column}!".to_sym
22
+
23
+ # FIXME: Hack to work around Datamapper not triggering callbacks
24
+ # for objects that are not dirty. By explicitly calling
25
+ # attribute_set we are marking the record as dirty.
26
+ class_eval <<-RUBY
27
+ def remove_image=(value)
28
+ _mounter(:#{column}).remove = value
29
+ attribute_set(:#{column}, '') if _mounter(:#{column}).remove?
30
+ end
31
+ RUBY
22
32
  end
23
33
 
24
34
  end # DataMapper
@@ -257,7 +257,7 @@ module CarrierWave
257
257
  image = yield(image)
258
258
  image.write(current_path)
259
259
  ::MiniMagick::Image.from_file(current_path)
260
- rescue ::MiniMagick::MiniMagickError => e
260
+ rescue ::MiniMagick::Error => e
261
261
  raise CarrierWave::ProcessingError.new("Failed to manipulate with MiniMagick, maybe it is not an image? Original Error: #{e}")
262
262
  end
263
263
 
@@ -87,8 +87,8 @@ module CarrierWave
87
87
  process :resize_to_fit => [width, height]
88
88
  end
89
89
 
90
- def resize_to_fill(width, height)
91
- process :resize_to_fill => [width, height]
90
+ def resize_to_fill(width, height, gravity=::Magick::CenterGravity)
91
+ process :resize_to_fill => [width, height, gravity]
92
92
  end
93
93
 
94
94
  def resize_and_pad(width, height, background=:transparent, gravity=::Magick::CenterGravity)
@@ -185,9 +185,9 @@ module CarrierWave
185
185
  #
186
186
  # [Magick::Image] additional manipulations to perform
187
187
  #
188
- def resize_to_fill(width, height)
188
+ def resize_to_fill(width, height, gravity=::Magick::CenterGravity)
189
189
  manipulate! do |img|
190
- img.crop_resized!(width, height)
190
+ img.crop_resized!(width, height, gravity)
191
191
  img = yield(img) if block_given?
192
192
  img
193
193
  end
@@ -147,7 +147,7 @@ module CarrierWave
147
147
  #
148
148
  def read
149
149
  if is_path?
150
- File.read(@file)
150
+ File.open(@file, "rb").read
151
151
  else
152
152
  @file.rewind if @file.respond_to?(:rewind)
153
153
  @file.read
@@ -199,7 +199,7 @@ module CarrierWave
199
199
  File.open(new_path, "wb") { |f| f.write(read) }
200
200
  end
201
201
  chmod!(new_path, permissions)
202
- self.class.new(new_path)
202
+ self.class.new({:tempfile => new_path, :content_type => content_type})
203
203
  end
204
204
 
205
205
  ##
@@ -15,12 +15,11 @@ module CarrierWave
15
15
  # config.cloud_files_container = "my_container"
16
16
  # end
17
17
  #
18
+ # You can optionally include your CDN host name in the configuration.
19
+ # This is *highly* recommended, as without it every request requires a lookup
20
+ # of this information.
18
21
  #
19
- # You can set the access policy for the uploaded files:
20
- #
21
- # CarrierWave.configure do |config|
22
- # config.s3_access_policy = 'public-read'
23
- # end
22
+ # config.cloud_files_cdn_host = "c000000.cdn.rackspacecloud.com"
24
23
  #
25
24
  #
26
25
  class CloudFiles < Abstract
@@ -73,13 +72,13 @@ module CarrierWave
73
72
  # [String] file's url
74
73
  #
75
74
  def url
76
- cf_container.object(@path).public_url
75
+ if @uploader.cloud_files_cdn_host
76
+ "http://" + @uploader.cloud_files_cdn_host + "/" + @path
77
+ else
78
+ cf_container.object(@path).public_url
79
+ end
77
80
  end
78
81
 
79
- #def metadata
80
- # s3_object.metadata
81
- #end
82
-
83
82
  def content_type
84
83
  cf_container.object(@path).content_type
85
84
  end
@@ -140,7 +139,7 @@ module CarrierWave
140
139
  #
141
140
  # === Returns
142
141
  #
143
- # [CarrierWave::Storage::RightS3::File] the stored file
142
+ # [CarrierWave::Storage::CloudFiles::File] the stored file
144
143
  #
145
144
  def store!(file)
146
145
  cloud_files_options = {'Content-Type' => file.content_type}
@@ -157,7 +156,7 @@ module CarrierWave
157
156
  #
158
157
  # === Returns
159
158
  #
160
- # [CarrierWave::Storage::RightS3::File] the stored file
159
+ # [CarrierWave::Storage::CloudFiles::File] the stored file
161
160
  #
162
161
  def retrieve!(identifier)
163
162
  CarrierWave::Storage::CloudFiles::File.new(uploader, self, uploader.store_path(identifier))
@@ -1,6 +1,5 @@
1
1
  # encoding: utf-8
2
2
  require 'mongo'
3
- require 'mongo/gridfs'
4
3
 
5
4
  module CarrierWave
6
5
  module Storage
@@ -12,8 +11,7 @@ module CarrierWave
12
11
 
13
12
  class File
14
13
 
15
- def initialize(uploader, database, path)
16
- @database = database
14
+ def initialize(uploader, path)
17
15
  @path = path
18
16
  @uploader = uploader
19
17
  end
@@ -31,15 +29,44 @@ module CarrierWave
31
29
  end
32
30
 
33
31
  def read
34
- ::GridFS::GridStore.read(@database, @path)
32
+ grid.open(@path, 'r').data
33
+ end
34
+
35
+ def write(file)
36
+ grid.open(@uploader.store_path, 'w', :content_type => file.content_type) do |f|
37
+ f.write(file.read)
38
+ end
35
39
  end
36
40
 
37
41
  def delete
38
- ::GridFS::GridStore.unlink(@database, @path)
42
+ grid.delete(@path)
39
43
  end
40
44
 
41
45
  def content_type
42
- ::GridFS::GridStore.open(@database, @path, 'r') { |f| return f.content_type }
46
+ grid.open(@path, 'r').content_type
47
+ end
48
+
49
+ def file_length
50
+ grid.open(@path, 'r').file_length
51
+ end
52
+
53
+ protected
54
+
55
+ def database
56
+ @connection ||= begin
57
+ host = @uploader.grid_fs_host
58
+ port = @uploader.grid_fs_port
59
+ database = @uploader.grid_fs_database
60
+ username = @uploader.grid_fs_username
61
+ password = @uploader.grid_fs_password
62
+ db = Mongo::Connection.new(host, port).db(database)
63
+ db.authenticate(username, password) if username && password
64
+ db
65
+ end
66
+ end
67
+
68
+ def grid
69
+ @grid ||= Mongo::GridFileSystem.new(database)
43
70
  end
44
71
 
45
72
  end
@@ -56,10 +83,9 @@ module CarrierWave
56
83
  # [CarrierWave::SanitizedFile] a sanitized file
57
84
  #
58
85
  def store!(file)
59
- ::GridFS::GridStore.open(database, uploader.store_path, 'w', :content_type => file.content_type) do |f|
60
- f.write file.read
61
- end
62
- CarrierWave::Storage::GridFS::File.new(uploader, database, uploader.store_path)
86
+ stored = CarrierWave::Storage::GridFS::File.new(uploader, uploader.store_path)
87
+ stored.write(file)
88
+ stored
63
89
  end
64
90
 
65
91
  ##
@@ -74,22 +100,7 @@ module CarrierWave
74
100
  # [CarrierWave::Storage::GridFS::File] a sanitized file
75
101
  #
76
102
  def retrieve!(identifier)
77
- CarrierWave::Storage::GridFS::File.new(uploader, database, uploader.store_path(identifier))
78
- end
79
-
80
- private
81
-
82
- def database
83
- @connection ||= begin
84
- host = uploader.grid_fs_host
85
- port = uploader.grid_fs_port
86
- database = uploader.grid_fs_database
87
- username = uploader.grid_fs_username
88
- password = uploader.grid_fs_password
89
- db = Mongo::Connection.new(host, port).db(database)
90
- db.authenticate(username, password) if username && password
91
- db
92
- end
103
+ CarrierWave::Storage::GridFS::File.new(uploader, uploader.store_path(identifier))
93
104
  end
94
105
 
95
106
  end # File
@@ -1,170 +1,3 @@
1
- # encoding: utf-8
2
- require 'right_aws'
3
1
 
4
- module CarrierWave
5
- module Storage
2
+ raise "The right_aws library is no longer supported. Please install the updated 'aws' library with support for Ruby 1.9 and euro buckets"
6
3
 
7
- ##
8
- # Uploads things to Amazon S3 webservices using the RightAWS libraries (right_aws gem).
9
- # In order for CarrierWave to connect to Amazon S3, you'll need to specify an access key id, secret key
10
- # and bucket
11
- #
12
- # CarrierWave.configure do |config|
13
- # config.s3_access_key_id = "xxxxxx"
14
- # config.s3_secret_access_key = "xxxxxx"
15
- # config.s3_bucket = "my_bucket_name"
16
- # end
17
- #
18
- # The RightAWS::S3Interface is used directly as opposed to the normal RightAWS::S3::Bucket et.al. classes.
19
- # This gives much improved performance and avoids unnecessary requests.
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
- # You can change the generated url to a cnamed domain by setting the cnamed config:
32
- #
33
- # CarrierWave.configure do |config|
34
- # config.s3_cnamed = true
35
- # config.s3_bucket = 'bucketname.domain.tld'
36
- # end
37
- #
38
- # Now the resulting url will be
39
- #
40
- # http://bucketname.domain.tld/path/to/file
41
- #
42
- # instead of
43
- #
44
- # http://bucketname.domain.tld.s3.amazonaws.com/path/to/file
45
- #
46
- class RightS3 < Abstract
47
-
48
- class File
49
-
50
- def initialize(uploader, base, path)
51
- @uploader = uploader
52
- @path = path
53
- @base = base
54
- end
55
-
56
- ##
57
- # Returns the current path of the file on S3
58
- #
59
- # === Returns
60
- #
61
- # [String] A path
62
- #
63
- def path
64
- @path
65
- end
66
-
67
- ##
68
- # Reads the contents of the file from S3
69
- #
70
- # === Returns
71
- #
72
- # [String] contents of the file
73
- #
74
- def read
75
- result = connection.get(bucket, @path)
76
- @headers = result[:headers]
77
- result[:object]
78
- end
79
-
80
- ##
81
- # Remove the file from Amazon S3
82
- #
83
- def delete
84
- connection.delete(bucket, @path)
85
- end
86
-
87
- ##
88
- # Returns the url on Amazon's S3 service
89
- #
90
- # === Returns
91
- #
92
- # [String] file's url
93
- #
94
- def url
95
- if @uploader.s3_cnamed
96
- ["http://", @uploader.s3_bucket, "/", @path].compact.join
97
- else
98
- ["http://s3.amazonaws.com/", @uploader.s3_bucket, "/", @path].compact.join
99
- end
100
- end
101
-
102
- def content_type
103
- headers["content-type"]
104
- end
105
-
106
- #def content_disposition
107
- # s3_object.content_disposition
108
- #end
109
-
110
- def store(file)
111
- connection.put(bucket, @path, file.read,
112
- 'x-amz-acl' => @uploader.s3_access_policy,
113
- 'content-type' => file.content_type
114
- )
115
- end
116
-
117
- private
118
-
119
- def headers
120
- @headers ||= {}
121
- end
122
-
123
- def bucket
124
- @uploader.s3_bucket
125
- end
126
-
127
- def connection
128
- @base.connection
129
- end
130
-
131
- end
132
-
133
- ##
134
- # Store the file on S3
135
- #
136
- # === Parameters
137
- #
138
- # [file (CarrierWave::SanitizedFile)] the file to store
139
- #
140
- # === Returns
141
- #
142
- # [CarrierWave::Storage::RightS3::File] the stored file
143
- #
144
- def store!(file)
145
- f = CarrierWave::Storage::RightS3::File.new(uploader, self, uploader.store_path)
146
- f.store(file)
147
- f
148
- end
149
-
150
- # Do something to retrieve the file
151
- #
152
- # @param [String] identifier uniquely identifies the file
153
- #
154
- # [identifier (String)] uniquely identifies the file
155
- #
156
- # === Returns
157
- #
158
- # [CarrierWave::Storage::RightS3::File] the stored file
159
- #
160
- def retrieve!(identifier)
161
- CarrierWave::Storage::RightS3::File.new(uploader, self, uploader.store_path(identifier))
162
- end
163
-
164
- def connection
165
- @connection ||= RightAws::S3Interface.new(uploader.s3_access_key_id, uploader.s3_secret_access_key)
166
- end
167
-
168
- end # RightS3
169
- end # Storage
170
- end # CarrierWave