carrierwave 0.5.6 → 0.5.7

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 CHANGED
@@ -59,15 +59,14 @@ uploader.retrieve_from_store!('my_file.png')
59
59
  ```
60
60
 
61
61
  CarrierWave gives you a `store` for permanent storage, and a `cache` for
62
- temporary storage. You can use different stores, at the moment a filesystem
63
- store, an Amazon S3 store, a Rackspace Cloud Files store, and a store for
64
- MongoDB's GridFS are bundled.
62
+ temporary storage. You can use different stores, including filesystem
63
+ and cloud storage.
65
64
 
66
65
  Most of the time you are going to want to use CarrierWave together with an ORM.
67
66
  It is quite simple to mount uploaders on columns in your model, so you can
68
67
  simply assign files and get going:
69
68
 
70
- ### ActiveRecord, Mongoid
69
+ ### ActiveRecord
71
70
 
72
71
  Make sure you are loading CarrierWave after loading your ORM, otherwise you'll
73
72
  need to require the relevant extension manually, e.g.:
@@ -90,8 +89,6 @@ class User
90
89
  end
91
90
  ```
92
91
 
93
- This works the same with all supported ORMs.
94
-
95
92
  Now you can cache files by assigning them to the attribute, they will
96
93
  automatically be stored when the record is saved.
97
94
 
@@ -102,17 +99,15 @@ u.avatar = File.open('somewhere')
102
99
  u.save!
103
100
  u.avatar.url # => '/url/to/file.png'
104
101
  u.avatar.current_path # => 'path/to/file.png'
102
+ u.avatar_identifier # => 'file.png'
105
103
  ```
106
104
 
107
- If using Mongoid, note that embedded documents files aren't saved when parent documents are saved.
108
- You must explicitly call save on embedded documents in order to save their attached files.
109
- You can read more about this [here](https://github.com/jnicklas/carrierwave/issues#issue/81)
110
-
111
- ### DataMapper, Sequel
105
+ ### DataMapper, Mongoid, Sequel
112
106
 
113
- Other ORM support has been extracted into separate gems. Learn more:
107
+ Other ORM support has been extracted into separate gems:
114
108
 
115
109
  * [carrierwave-datamapper](https://github.com/jnicklas/carrierwave-datamapper)
110
+ * [carrierwave-mongoid](https://github.com/jnicklas/carrierwave-mongoid)
116
111
  * [carrierwave-sequel](https://github.com/jnicklas/carrierwave-sequel)
117
112
 
118
113
  There are more extensions listed in [the wiki](https://github.com/jnicklas/carrierwave/wiki)
@@ -478,6 +473,13 @@ of this information.
478
473
  config.fog_host = "c000000.cdn.rackspacecloud.com"
479
474
  ```
480
475
 
476
+ The UK Rackspace Cloud doesn’t have the same auth server as the US Cloud.
477
+ In case you are using Rackspace UK, you have to adjust the Auth URL:
478
+
479
+ ``` ruby
480
+ config.rackspace_auth_url = 'lon.auth.api.rackspacecloud.com'
481
+ ```
482
+
481
483
  In your uploader, set the storage to :fog
482
484
 
483
485
  ``` ruby
@@ -520,38 +522,6 @@ end
520
522
  That's it! You can still use the `CarrierWave::Uploader#url` method to return
521
523
  the url to the file on Google.
522
524
 
523
- ## Using MongoDB's GridFS store
524
-
525
- You'll need to configure the database and host to use:
526
-
527
- ``` ruby
528
- CarrierWave.configure do |config|
529
- config.grid_fs_database = 'my_mongo_database'
530
- config.grid_fs_host = 'mongo.example.com'
531
- end
532
- ```
533
-
534
- The defaults are 'carrierwave' and 'localhost'.
535
-
536
- And then in your uploader, set the storage to `:grid_fs`:
537
-
538
- ``` ruby
539
- class AvatarUploader < CarrierWave::Uploader::Base
540
- storage :grid_fs
541
- end
542
- ```
543
-
544
- Since GridFS doesn't make the files available via HTTP, you'll need to stream
545
- them yourself. In Rails for example, you could use the `send_data` method. You
546
- can tell CarrierWave the URL you will serve your images from, allowing it to
547
- generate the correct URL, by setting eg:
548
-
549
- ``` ruby
550
- CarrierWave.configure do |config|
551
- config.grid_fs_access_url = "/image/show"
552
- end
553
- ```
554
-
555
525
  ## Using RMagick
556
526
 
557
527
  If you're uploading images, you'll probably want to manipulate them in some way,
data/lib/carrierwave.rb CHANGED
@@ -107,5 +107,3 @@ elsif defined?(Sinatra)
107
107
  CarrierWave.root = Sinatra::Application.public
108
108
 
109
109
  end
110
-
111
- require 'carrierwave/orm/mongoid' if defined?(Mongoid)
@@ -21,15 +21,13 @@ module CarrierWave
21
21
  #
22
22
  def uploaders
23
23
  @uploaders ||= {}
24
- @uploaders = superclass.uploaders.merge(@uploaders)
25
- rescue NoMethodError
24
+ @uploaders = superclass.uploaders.merge(@uploaders) if superclass.respond_to?(:uploaders)
26
25
  @uploaders
27
26
  end
28
27
 
29
28
  def uploader_options
30
29
  @uploader_options ||= {}
31
- @uploader_options = superclass.uploader_options.merge(@uploader_options)
32
- rescue NoMethodError
30
+ @uploader_options = superclass.uploader_options.merge(@uploader_options) if superclass.respond_to?(:uploader_options)
33
31
  @uploader_options
34
32
  end
35
33
 
@@ -337,10 +335,8 @@ module CarrierWave
337
335
  end
338
336
 
339
337
  def remote_url=(url)
340
- unless uploader.cached?
341
- @remote_url = url
342
- uploader.download!(url)
343
- end
338
+ @remote_url = url
339
+ uploader.download!(url)
344
340
  end
345
341
 
346
342
  def store!
@@ -130,16 +130,7 @@ module CarrierWave
130
130
  with_callbacks(:retrieve_from_cache, cache_name) do
131
131
  self.cache_id, self.original_filename = cache_name.to_s.split('/', 2)
132
132
  @filename = original_filename
133
-
134
- if File.exist?(cache_path) && defined?(MIME::Types)
135
- @file = SanitizedFile.new(
136
- :tempfile => cache_path,
137
- :filename => @filename,
138
- :content_type => MIME::Types.of(File.basename(cache_path))[0].content_type
139
- )
140
- else
141
- @file = CarrierWave::SanitizedFile.new(cache_path)
142
- end
133
+ @file = CarrierWave::SanitizedFile.new(cache_path)
143
134
  end
144
135
  end
145
136
 
@@ -37,7 +37,6 @@ module CarrierWave
37
37
  add_config :enable_processing
38
38
  add_config :ensure_multipart_form
39
39
  add_config :delete_tmp_file_after_storage
40
- add_config :delete_cache_id_after_storage
41
40
  add_config :remove_previously_stored_files_after_update
42
41
 
43
42
  # fog
@@ -145,7 +144,6 @@ module CarrierWave
145
144
  config.store_dir = 'uploads'
146
145
  config.cache_dir = 'uploads/tmp'
147
146
  config.delete_tmp_file_after_storage = true
148
- config.delete_cache_id_after_storage = true
149
147
  config.remove_previously_stored_files_after_update = true
150
148
  config.ignore_integrity_errors = true
151
149
  config.ignore_processing_errors = true
@@ -53,18 +53,18 @@ module CarrierWave
53
53
  # [new_file (File, IOString, Tempfile)] any kind of file object
54
54
  #
55
55
  def store!(new_file=nil)
56
- cache!(new_file) if new_file
56
+ cache!(new_file) if new_file && ((@cache_id != parent_cache_id) || @cache_id.nil?)
57
57
  if @file and @cache_id
58
58
  with_callbacks(:store, new_file) do
59
59
  new_file = storage.store!(@file)
60
60
  @file.delete if delete_tmp_file_after_storage
61
- delete_cache_id if delete_cache_id_after_storage
61
+ delete_cache_id
62
62
  @file = new_file
63
63
  @cache_id = nil
64
64
  end
65
65
  end
66
66
  end
67
-
67
+
68
68
  ##
69
69
  # Deletes a cache id (tmp dir in cache)
70
70
  #
@@ -22,13 +22,24 @@ module CarrierWave
22
22
  ##
23
23
  # === Returns
24
24
  #
25
- # [String] A JSON serialization containing this uploader's URL(s)
25
+ # [Hash] the locations where this file and versions are accessible via a url
26
26
  #
27
27
  def as_json(options = nil)
28
28
  h = { :url => url }
29
29
  h.merge Hash[versions.map { |name, version| [name, { :url => version.url }] }]
30
30
  end
31
31
 
32
+ ##
33
+ # FIXME to_xml should work like to_json, but this is the best we've been able to do so far.
34
+ # This hack fixes issue #337.
35
+ #
36
+ # === Returns
37
+ #
38
+ # [nil]
39
+ #
40
+ def to_xml(options = nil)
41
+ end
42
+
32
43
  end # Url
33
44
  end # Uploader
34
45
  end # CarrierWave
@@ -26,6 +26,9 @@ module CarrierWave
26
26
  self.versions = {}
27
27
  self.version_names = []
28
28
 
29
+ attr_accessor :parent_cache_id
30
+
31
+ after :cache, :assign_parent_cache_id
29
32
  after :cache, :cache_versions!
30
33
  after :store, :store_versions!
31
34
  after :remove, :remove_versions!
@@ -179,6 +182,11 @@ module CarrierWave
179
182
  end
180
183
 
181
184
  private
185
+ def assign_parent_cache_id(file)
186
+ active_versions.each do |name, uploader|
187
+ uploader.parent_cache_id = @cache_id
188
+ end
189
+ end
182
190
 
183
191
  def active_versions
184
192
  versions.select do |name, uploader|
@@ -1,3 +1,3 @@
1
1
  module CarrierWave
2
- VERSION = "0.5.6"
2
+ VERSION = "0.5.7"
3
3
  end
@@ -4,6 +4,7 @@ class <%= class_name %>Uploader < CarrierWave::Uploader::Base
4
4
 
5
5
  # Include RMagick or ImageScience support:
6
6
  # include CarrierWave::RMagick
7
+ # include CarrierWave::MiniMagick
7
8
  # include CarrierWave::ImageScience
8
9
 
9
10
  # Choose what kind of storage to use for this uploader:
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carrierwave
3
3
  version: !ruby/object:Gem::Version
4
- hash: 7
4
+ hash: 5
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 5
9
- - 6
10
- version: 0.5.6
9
+ - 7
10
+ version: 0.5.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jonas Nicklas
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-07-12 00:00:00 -05:00
18
+ date: 2011-08-12 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -52,17 +52,16 @@ dependencies:
52
52
  version_requirements: &id003 !ruby/object:Gem::Requirement
53
53
  none: false
54
54
  requirements:
55
- - - ~>
55
+ - - ">="
56
56
  - !ruby/object:Gem::Version
57
57
  hash: 3
58
58
  segments:
59
- - 2
60
59
  - 0
61
- version: "2.0"
60
+ version: "0"
62
61
  prerelease: false
63
62
  type: :development
64
63
  requirement: *id003
65
- name: rspec
64
+ name: sqlite3
66
65
  - !ruby/object:Gem::Dependency
67
66
  version_requirements: &id004 !ruby/object:Gem::Requirement
68
67
  none: false
@@ -76,7 +75,7 @@ dependencies:
76
75
  prerelease: false
77
76
  type: :development
78
77
  requirement: *id004
79
- name: excon
78
+ name: cucumber
80
79
  - !ruby/object:Gem::Dependency
81
80
  version_requirements: &id005 !ruby/object:Gem::Requirement
82
81
  none: false
@@ -90,21 +89,22 @@ dependencies:
90
89
  prerelease: false
91
90
  type: :development
92
91
  requirement: *id005
93
- name: fog
92
+ name: json
94
93
  - !ruby/object:Gem::Dependency
95
94
  version_requirements: &id006 !ruby/object:Gem::Requirement
96
95
  none: false
97
96
  requirements:
98
- - - ">="
97
+ - - ~>
99
98
  - !ruby/object:Gem::Version
100
99
  hash: 3
101
100
  segments:
101
+ - 2
102
102
  - 0
103
- version: "0"
103
+ version: "2.0"
104
104
  prerelease: false
105
105
  type: :development
106
106
  requirement: *id006
107
- name: cucumber
107
+ name: rspec
108
108
  - !ruby/object:Gem::Dependency
109
109
  version_requirements: &id007 !ruby/object:Gem::Requirement
110
110
  none: false
@@ -118,7 +118,7 @@ dependencies:
118
118
  prerelease: false
119
119
  type: :development
120
120
  requirement: *id007
121
- name: sqlite3
121
+ name: sham_rack
122
122
  - !ruby/object:Gem::Dependency
123
123
  version_requirements: &id008 !ruby/object:Gem::Requirement
124
124
  none: false
@@ -132,7 +132,7 @@ dependencies:
132
132
  prerelease: false
133
133
  type: :development
134
134
  requirement: *id008
135
- name: rmagick
135
+ name: timecop
136
136
  - !ruby/object:Gem::Dependency
137
137
  version_requirements: &id009 !ruby/object:Gem::Requirement
138
138
  none: false
@@ -146,7 +146,7 @@ dependencies:
146
146
  prerelease: false
147
147
  type: :development
148
148
  requirement: *id009
149
- name: RubyInline
149
+ name: cloudfiles
150
150
  - !ruby/object:Gem::Dependency
151
151
  version_requirements: &id010 !ruby/object:Gem::Requirement
152
152
  none: false
@@ -160,7 +160,7 @@ dependencies:
160
160
  prerelease: false
161
161
  type: :development
162
162
  requirement: *id010
163
- name: image_science
163
+ name: fog
164
164
  - !ruby/object:Gem::Dependency
165
165
  version_requirements: &id011 !ruby/object:Gem::Requirement
166
166
  none: false
@@ -188,77 +188,7 @@ dependencies:
188
188
  prerelease: false
189
189
  type: :development
190
190
  requirement: *id012
191
- name: bson_ext
192
- - !ruby/object:Gem::Dependency
193
- version_requirements: &id013 !ruby/object:Gem::Requirement
194
- none: false
195
- requirements:
196
- - - ">="
197
- - !ruby/object:Gem::Version
198
- hash: 3
199
- segments:
200
- - 0
201
- version: "0"
202
- prerelease: false
203
- type: :development
204
- requirement: *id013
205
- name: mongoid
206
- - !ruby/object:Gem::Dependency
207
- version_requirements: &id014 !ruby/object:Gem::Requirement
208
- none: false
209
- requirements:
210
- - - ">="
211
- - !ruby/object:Gem::Version
212
- hash: 3
213
- segments:
214
- - 0
215
- version: "0"
216
- prerelease: false
217
- type: :development
218
- requirement: *id014
219
- name: timecop
220
- - !ruby/object:Gem::Dependency
221
- version_requirements: &id015 !ruby/object:Gem::Requirement
222
- none: false
223
- requirements:
224
- - - ">="
225
- - !ruby/object:Gem::Version
226
- hash: 3
227
- segments:
228
- - 0
229
- version: "0"
230
- prerelease: false
231
- type: :development
232
- requirement: *id015
233
- name: json
234
- - !ruby/object:Gem::Dependency
235
- version_requirements: &id016 !ruby/object:Gem::Requirement
236
- none: false
237
- requirements:
238
- - - ">="
239
- - !ruby/object:Gem::Version
240
- hash: 3
241
- segments:
242
- - 0
243
- version: "0"
244
- prerelease: false
245
- type: :development
246
- requirement: *id016
247
- name: cloudfiles
248
- - !ruby/object:Gem::Dependency
249
- version_requirements: &id017 !ruby/object:Gem::Requirement
250
- none: false
251
- requirements:
252
- - - ">="
253
- - !ruby/object:Gem::Version
254
- hash: 3
255
- segments:
256
- - 0
257
- version: "0"
258
- prerelease: false
259
- type: :development
260
- requirement: *id017
261
- name: sham_rack
191
+ name: rmagick
262
192
  description: Upload files in your Ruby applications, map them to a range of ORMs, store them on different backends.
263
193
  email:
264
194
  - jonas.nicklas@gmail.com
@@ -273,8 +203,6 @@ files:
273
203
  - lib/carrierwave/locale/en.yml
274
204
  - lib/carrierwave/mount.rb
275
205
  - lib/carrierwave/orm/activerecord.rb
276
- - lib/carrierwave/orm/mongoid.rb
277
- - lib/carrierwave/processing/image_science.rb
278
206
  - lib/carrierwave/processing/mime_types.rb
279
207
  - lib/carrierwave/processing/mini_magick.rb
280
208
  - lib/carrierwave/processing/rmagick.rb
@@ -283,7 +211,6 @@ files:
283
211
  - lib/carrierwave/storage/cloud_files.rb
284
212
  - lib/carrierwave/storage/file.rb
285
213
  - lib/carrierwave/storage/fog.rb
286
- - lib/carrierwave/storage/grid_fs.rb
287
214
  - lib/carrierwave/storage/right_s3.rb
288
215
  - lib/carrierwave/storage/s3.rb
289
216
  - lib/carrierwave/test/matchers.rb
@@ -1,68 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'mongoid'
4
- require 'carrierwave/validations/active_model'
5
-
6
- module CarrierWave
7
- module Mongoid
8
- include CarrierWave::Mount
9
- ##
10
- # See +CarrierWave::Mount#mount_uploader+ for documentation
11
- #
12
- def mount_uploader(column, uploader=nil, options={}, &block)
13
- options[:mount_on] ||= "#{column}_filename"
14
- field options[:mount_on]
15
-
16
- super
17
-
18
- alias_method :read_uploader, :read_attribute
19
- alias_method :write_uploader, :write_attribute
20
-
21
- include CarrierWave::Validations::ActiveModel
22
-
23
- validates_integrity_of column if uploader_option(column.to_sym, :validate_integrity)
24
- validates_processing_of column if uploader_option(column.to_sym, :validate_processing)
25
-
26
- after_save :"store_#{column}!"
27
- before_save :"write_#{column}_identifier"
28
- after_destroy :"remove_#{column}!"
29
- before_update :"store_previous_model_for_#{column}"
30
- after_save :"remove_previously_stored_#{column}"
31
-
32
- class_eval <<-RUBY, __FILE__, __LINE__+1
33
- def #{column}=(new_file)
34
- column = _mounter(:#{column}).serialization_column
35
-
36
- # Note (Didier L.): equivalent of the <column>_will_change! ActiveModel method
37
- begin
38
- value = __send__(column)
39
- value = value.duplicable? ? value.clone : value
40
- rescue TypeError, NoMethodError
41
- end
42
- setup_modifications
43
-
44
- super.tap do
45
- @modifications[column] = [value, __send__(column)]
46
- end
47
- end
48
-
49
- def #{column}_changed?
50
- column = _mounter(:#{column}).serialization_column
51
- send(:"\#{column}_changed?")
52
- end
53
-
54
- def find_previous_model_for_#{column}
55
- if self.embedded?
56
- self._parent.reload.send(self.metadata.key).find(to_key.first)
57
- else
58
- self.class.find(to_key.first)
59
- end
60
- end
61
-
62
- RUBY
63
-
64
- end
65
- end # Mongoid
66
- end # CarrierWave
67
-
68
- Mongoid::Document::ClassMethods.send(:include, CarrierWave::Mongoid)
@@ -1,135 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'image_science'
4
-
5
- module CarrierWave
6
- module ImageScience
7
- extend ActiveSupport::Concern
8
-
9
- module ClassMethods
10
- def resize_to_limit(width, height)
11
- process :resize_to_limit => [width, height]
12
- end
13
-
14
- def resize_to_fit(width, height)
15
- process :resize_to_fit => [width, height]
16
- end
17
-
18
- def resize_to_fill(width, height)
19
- process :resize_to_fill => [width, height]
20
- end
21
- end
22
-
23
- ##
24
- # Resize the image to fit within the specified dimensions while retaining
25
- # the original aspect ratio. The image may be shorter or narrower than
26
- # specified in the smaller dimension but will not be larger than the
27
- # specified values.
28
- #
29
- # === Parameters
30
- #
31
- # [width (Integer)] the width to scale the image to
32
- # [height (Integer)] the height to scale the image to
33
- #
34
- def resize_to_fit(new_width, new_height)
35
- cache_stored_file! if !cached?
36
-
37
- ::ImageScience.with_image(self.current_path) do |img|
38
- width, height = extract_dimensions(img.width, img.height, new_width, new_height)
39
- img.resize( width, height ) do |file|
40
- file.save( self.current_path )
41
- end
42
- end
43
- end
44
-
45
- ##
46
- # Resize the image to fit within the specified dimensions while retaining
47
- # the aspect ratio of the original image. If necessary, crop the image in
48
- # the larger dimension.
49
- #
50
- # === Parameters
51
- #
52
- # [width (Integer)] the width to scale the image to
53
- # [height (Integer)] the height to scale the image to
54
- #
55
- def resize_to_fill(new_width, new_height)
56
- cache_stored_file! if !cached?
57
-
58
- ::ImageScience.with_image(self.current_path) do |img|
59
- width, height = extract_dimensions_for_crop(img.width, img.height, new_width, new_height)
60
- x_offset, y_offset = extract_placement_for_crop(width, height, new_width, new_height)
61
-
62
- # check if if new dimensions are too small for the new image
63
- if width < new_width
64
- width = new_width
65
- height = (new_width.to_f*(img.height.to_f/img.width.to_f)).round
66
- elsif height < new_height
67
- height = new_height
68
- width = (new_height.to_f*(img.width.to_f/img.height.to_f)).round
69
- end
70
-
71
- img.resize( width, height ) do |i2|
72
-
73
- # check to make sure offset is not negative
74
- if x_offset < 0
75
- x_offset = 0
76
- end
77
- if y_offset < 0
78
- y_offset = 0
79
- end
80
-
81
- i2.with_crop( x_offset, y_offset, new_width + x_offset, new_height + y_offset) do |file|
82
- file.save( self.current_path )
83
- end
84
- end
85
- end
86
- end
87
-
88
- ##
89
- # Resize the image to fit within the specified dimensions while retaining
90
- # the original aspect ratio. Will only resize the image if it is larger than the
91
- # specified dimensions. The resulting image may be shorter or narrower than specified
92
- # in the smaller dimension but will not be larger than the specified values.
93
- #
94
- # === Parameters
95
- #
96
- # [width (Integer)] the width to scale the image to
97
- # [height (Integer)] the height to scale the image to
98
- #
99
- def resize_to_limit(new_width, new_height)
100
- cache_stored_file! if !cached?
101
-
102
- ::ImageScience.with_image(self.current_path) do |img|
103
- if img.width > new_width or img.height > new_height
104
- resize_to_fit(new_width, new_height)
105
- end
106
- end
107
- end
108
-
109
- private
110
-
111
- def extract_dimensions(width, height, new_width, new_height, type = :resize)
112
- aspect_ratio = width.to_f / height.to_f
113
- new_aspect_ratio = new_width / new_height
114
-
115
- if (new_aspect_ratio > aspect_ratio) ^ ( type == :crop ) # Image is too wide, the caret is the XOR operator
116
- new_width, new_height = [ (new_height * aspect_ratio), new_height]
117
- else #Image is too narrow
118
- new_width, new_height = [ new_width, (new_width / aspect_ratio)]
119
- end
120
-
121
- [new_width, new_height].collect! { |v| v.round }
122
- end
123
-
124
- def extract_dimensions_for_crop(width, height, new_width, new_height)
125
- extract_dimensions(width, height, new_width, new_height, :crop)
126
- end
127
-
128
- def extract_placement_for_crop(width, height, new_width, new_height)
129
- x_offset = (width / 2.0) - (new_width / 2.0)
130
- y_offset = (height / 2.0) - (new_height / 2.0)
131
- [x_offset, y_offset].collect! { |v| v.round }
132
- end
133
-
134
- end # ImageScience
135
- end # CarrierWave
@@ -1,136 +0,0 @@
1
- # encoding: utf-8
2
- require 'mongo'
3
-
4
- module CarrierWave
5
- module Storage
6
-
7
- ##
8
- # The GridFS store uses MongoDB's GridStore file storage system to store files
9
- #
10
- # There are two ways of configuring the GridFS connection. Either you create a
11
- # connection or you reuse an existing connection.
12
- #
13
- # Creating a connection looks something like this:
14
- #
15
- # CarrierWave.configure do |config|
16
- # config.storage = :grid_fs
17
- # config.grid_fs_host = "your-host.com"
18
- # config.grid_fs_port = "27017"
19
- # config.grid_fs_database = "your_dbs_app_name"
20
- # config.grid_fs_username = "user"
21
- # config.grid_fs_password = "verysecret"
22
- # config.grid_fs_access_url = "/images"
23
- # end
24
- #
25
- # In the above example your documents url will look like:
26
- #
27
- # http://your-app.com/images/:document-identifier-here
28
- #
29
- # When you already have a Mongo connection object (for example through Mongoid)
30
- # you can also reuse this connection:
31
- #
32
- # CarrierWave.configure do |config|
33
- # config.storage = :grid_fs
34
- # config.grid_fs_connection = Mongoid.database
35
- # config.grid_fs_access_url = "/images"
36
- # end
37
- #
38
- class GridFS < Abstract
39
-
40
- class File
41
-
42
- def initialize(uploader, path)
43
- @path = path
44
- @uploader = uploader
45
- end
46
-
47
- def path
48
- @path
49
- end
50
-
51
- def url
52
- unless @uploader.grid_fs_access_url
53
- nil
54
- else
55
- [@uploader.grid_fs_access_url, @path].join("/")
56
- end
57
- end
58
-
59
- def read
60
- grid.open(@path, 'r').data
61
- end
62
-
63
- def write(file)
64
- grid.open(@uploader.store_path, 'w', :content_type => file.content_type) do |f|
65
- f.write(file.read)
66
- end
67
- end
68
-
69
- def delete
70
- grid.delete(@path)
71
- end
72
-
73
- def content_type
74
- grid.open(@path, 'r').content_type
75
- end
76
-
77
- def file_length
78
- grid.open(@path, 'r').file_length
79
- end
80
-
81
- protected
82
-
83
- def database
84
- @connection ||= @uploader.grid_fs_connection || begin
85
- host = @uploader.grid_fs_host
86
- port = @uploader.grid_fs_port
87
- database = @uploader.grid_fs_database
88
- username = @uploader.grid_fs_username
89
- password = @uploader.grid_fs_password
90
- db = Mongo::Connection.new(host, port).db(database)
91
- db.authenticate(username, password) if username && password
92
- db
93
- end
94
- end
95
-
96
- def grid
97
- @grid ||= Mongo::GridFileSystem.new(database)
98
- end
99
-
100
- end
101
-
102
- ##
103
- # Store the file in MongoDB's GridFS GridStore
104
- #
105
- # === Parameters
106
- #
107
- # [file (CarrierWave::SanitizedFile)] the file to store
108
- #
109
- # === Returns
110
- #
111
- # [CarrierWave::SanitizedFile] a sanitized file
112
- #
113
- def store!(file)
114
- stored = CarrierWave::Storage::GridFS::File.new(uploader, uploader.store_path)
115
- stored.write(file)
116
- stored
117
- end
118
-
119
- ##
120
- # Retrieve the file from MongoDB's GridFS GridStore
121
- #
122
- # === Parameters
123
- #
124
- # [identifier (String)] the filename of the file
125
- #
126
- # === Returns
127
- #
128
- # [CarrierWave::Storage::GridFS::File] a sanitized file
129
- #
130
- def retrieve!(identifier)
131
- CarrierWave::Storage::GridFS::File.new(uploader, uploader.store_path(identifier))
132
- end
133
-
134
- end # File
135
- end # Storage
136
- end # CarrierWave