carrierwave 0.5.0 → 0.5.1

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.rdoc CHANGED
@@ -26,6 +26,11 @@ In Rails, add it to your Gemfile:
26
26
 
27
27
  gem 'carrierwave'
28
28
 
29
+ CarrierWave is only compatible with Rails 3 and later as of version 0.5. If you want to use
30
+ Rails 2, please use the latest gem in the 0.4.X series. You may also consider trying
31
+ {this plugin}[http://github.com/gbuesing/carrierwave_rails23_compat] which allows CarrierWave
32
+ to be used with a Rails 2.3 app.
33
+
29
34
  == Quick Start
30
35
 
31
36
  Start off by generating an uploader:
@@ -322,8 +327,7 @@ CarrierWave comes with some RSpec matchers which you may find useful:
322
327
 
323
328
  == Using Amazon S3
324
329
 
325
- Older versions of CarrierWave used the +aws+, +aws-s3+, and/or +right_aws+ libraries.
326
- Now fog[http://github.com/geemus/fog] is now used instead. Ensure you have it installed:
330
+ Fog[http://github.com/geemus/fog] is used to support Amazon S3. Ensure you have it installed:
327
331
 
328
332
  gem install fog
329
333
 
@@ -483,10 +487,10 @@ happily accepted.
483
487
  The activerecord validations use the Rails i18n framework. Add these keys to
484
488
  your translations file:
485
489
 
486
- carrierwave:
487
- errors:
488
- integrity: 'Not an image.'
489
- processing: 'Cannot resize image.'
490
+ errors:
491
+ messages:
492
+ carrierwave_processing_error: 'Cannot resize image.'
493
+ carrierwave_integrity_error: 'Not an image.'
490
494
 
491
495
  == License
492
496
 
@@ -524,6 +528,7 @@ And then install the dependencies:
524
528
 
525
529
  You should now be able to run the tests:
526
530
 
527
- bundle exec rake spec
531
+ bundle exec rake
528
532
 
529
533
  Issues are reported on GitHub, pull requests are very welcome!
534
+
data/lib/carrierwave.rb CHANGED
@@ -94,7 +94,7 @@ elsif defined?(Sinatra)
94
94
  end
95
95
 
96
96
 
97
- require File.join(File.dirname(__FILE__), "carrierwave", "orm", 'activerecord') if defined?(ActiveRecord)
98
- require File.join(File.dirname(__FILE__), "carrierwave", "orm", 'datamapper') if defined?(DataMapper)
99
- require File.join(File.dirname(__FILE__), "carrierwave", "orm", 'sequel') if defined?(Sequel)
100
- require File.join(File.dirname(__FILE__), "carrierwave", "orm", "mongoid") if defined?(Mongoid)
97
+ require 'carrierwave/orm/activerecord' if defined?(ActiveRecord)
98
+ require 'carrierwave/orm/datamapper' if defined?(DataMapper)
99
+ require 'carrierwave/orm/sequel' if defined?(Sequel)
100
+ require 'carrierwave/orm/mongoid' if defined?(Mongoid)
@@ -60,9 +60,7 @@ module CarrierWave
60
60
  # retrieve files like this:
61
61
  #
62
62
  # @user.image # => <Uploader>
63
- # @user.image = some_file_object
64
- #
65
- # @user.store_image!
63
+ # @user.image.store!(some_file_object)
66
64
  #
67
65
  # @user.image.url # => '/some_url.png'
68
66
  #
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  require 'active_record'
4
- require File.expand_path(File.dirname(__FILE__) + "../../validations/active_model")
4
+ require 'carrierwave/validations/active_model'
5
5
 
6
6
  module CarrierWave
7
7
  module ActiveRecord
@@ -1,6 +1,7 @@
1
1
  # encoding: utf-8
2
+
2
3
  require 'mongoid'
3
- require File.expand_path(File.dirname(__FILE__) + "../../validations/active_model")
4
+ require 'carrierwave/validations/active_model'
4
5
 
5
6
  module CarrierWave
6
7
  module Mongoid
@@ -11,17 +12,17 @@ module CarrierWave
11
12
  def mount_uploader(column, uploader, options={}, &block)
12
13
  options[:mount_on] ||= "#{column}_filename"
13
14
  field options[:mount_on]
14
-
15
+
15
16
  super
16
-
17
+
17
18
  alias_method :read_uploader, :read_attribute
18
19
  alias_method :write_uploader, :write_attribute
19
-
20
+
20
21
  include CarrierWave::Validations::ActiveModel
21
22
 
22
23
  validates_integrity_of column if uploader_option(column.to_sym, :validate_integrity)
23
24
  validates_processing_of column if uploader_option(column.to_sym, :validate_processing)
24
-
25
+
25
26
  after_save "store_#{column}!".to_sym
26
27
  before_save "write_#{column}_identifier".to_sym
27
28
  after_destroy "remove_#{column}!".to_sym
@@ -249,11 +249,11 @@ module CarrierWave
249
249
  # [CarrierWave::ProcessingError] if manipulation failed.
250
250
  #
251
251
  def manipulate!
252
- image = ::MiniMagick::Image.from_file(current_path)
252
+ image = ::MiniMagick::Image.open(current_path)
253
253
  image = yield(image)
254
254
  image.write(current_path)
255
- ::MiniMagick::Image.from_file(current_path)
256
- rescue ::MiniMagick::Error => e
255
+ ::MiniMagick::Image.open(current_path)
256
+ rescue ::MiniMagick::Error, ::MiniMagick::Invalid => e
257
257
  raise CarrierWave::ProcessingError.new("Failed to manipulate with MiniMagick, maybe it is not an image? Original Error: #{e}")
258
258
  end
259
259
 
@@ -23,8 +23,7 @@ module CarrierWave
23
23
  #
24
24
  def store!(file)
25
25
  path = ::File.expand_path(uploader.store_path, uploader.root)
26
- file.move_to(path, uploader.permissions)
27
- file
26
+ file.copy_to(path, uploader.permissions)
28
27
  end
29
28
 
30
29
  ##
@@ -7,6 +7,34 @@ module CarrierWave
7
7
  ##
8
8
  # The GridFS store uses MongoDB's GridStore file storage system to store files
9
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
+ #
10
38
  class GridFS < Abstract
11
39
 
12
40
  class File
@@ -53,7 +81,7 @@ module CarrierWave
53
81
  protected
54
82
 
55
83
  def database
56
- @connection ||= begin
84
+ @connection ||= @uploader.grid_fs_connection || begin
57
85
  host = @uploader.grid_fs_host
58
86
  port = @uploader.grid_fs_port
59
87
  database = @uploader.grid_fs_database
@@ -21,15 +21,14 @@ module CarrierWave
21
21
  # You can set the access policy for the uploaded files:
22
22
  #
23
23
  # CarrierWave.configure do |config|
24
- # config.s3_access_policy = 'public-read'
24
+ # config.s3_access_policy = :public_read
25
25
  # end
26
26
  #
27
- # The default is 'public-read'. For more options see:
27
+ # The default is :public_read. For more options see:
28
28
  #
29
29
  # http://docs.amazonwebservices.com/AmazonS3/latest/RESTAccessPolicy.html#RESTCannedAccessPolicies
30
30
  #
31
- # For backwards compatability with the original aws-s3 library, if the old +config.s3_access+ is set it
32
- # will be converted to the appropriate access policy:
31
+ # The following access policies are available:
33
32
  #
34
33
  # [:private] No one else has any access rights.
35
34
  # [:public_read] The anonymous principal is granted READ access.
@@ -54,6 +53,19 @@ module CarrierWave
54
53
  #
55
54
  # http://bucketname.domain.tld.s3.amazonaws.com/path/to/file
56
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
+ #
57
69
  class S3 < Abstract
58
70
 
59
71
  class File
@@ -103,37 +115,35 @@ module CarrierWave
103
115
  # [String] file's url
104
116
  #
105
117
  def url
106
- if @uploader.s3_cnamed
107
- ["http://#{@uploader.s3_bucket}", @path].compact.join('/')
118
+ if access_policy == :authenticated_read
119
+ authenticated_url
108
120
  else
109
- ["http://#{@uploader.s3_bucket}.s3.amazonaws.com", @path].compact.join('/')
121
+ public_url
110
122
  end
111
123
  end
112
124
 
125
+ def public_url
126
+ if cnamed?
127
+ ["http://#{bucket}", path].compact.join('/')
128
+ else
129
+ ["http://#{bucket}.s3.amazonaws.com", path].compact.join('/')
130
+ end
131
+ end
132
+
133
+ def authenticated_url
134
+ connection.get_object_url(bucket, path, Time.now + 60 * 10)
135
+ end
136
+
113
137
  def store(file)
114
138
  content_type ||= file.content_type # this might cause problems if content type changes between read and upload (unlikely)
115
- connection.put_object(bucket, @path, file.read,
139
+ connection.put_object(bucket, path, file.read,
116
140
  {
117
- 'x-amz-acl' => access_policy,
141
+ 'x-amz-acl' => access_policy.to_s.gsub('_', '-'),
118
142
  'Content-Type' => content_type
119
143
  }.merge(@uploader.s3_headers)
120
144
  )
121
145
  end
122
146
 
123
- # The Amazon S3 Access policy ready to send in storage request headers.
124
- def access_policy
125
- return @access_policy unless @access_policy.blank?
126
- if @uploader.s3_access_policy.blank?
127
- if !@uploader.s3_access.blank?
128
- @access_policy = @uploader.s3_access.to_s.gsub(/_/, '-')
129
- else
130
- @access_policy = 'public-read'
131
- end
132
- else
133
- @access_policy = @uploader.s3_access_policy
134
- end
135
- end
136
-
137
147
  def content_type
138
148
  headers["Content-Type"]
139
149
  end
@@ -143,16 +153,28 @@ module CarrierWave
143
153
  end
144
154
 
145
155
  def size
146
- headers['content-length'].to_i
156
+ headers['Content-Length'].to_i
147
157
  end
148
158
 
149
159
  # Headers returned from file retrieval
150
160
  def headers
151
- @headers ||= connection.head(bucket, @path)
161
+ @headers ||= begin
162
+ connection.head_object(bucket, @path).headers
163
+ rescue Excon::Errors::NotFound # Don't die, just return no headers
164
+ {}
165
+ end
152
166
  end
153
167
 
154
168
  private
155
169
 
170
+ def cnamed?
171
+ @uploader.s3_cnamed
172
+ end
173
+
174
+ def access_policy
175
+ @uploader.s3_access_policy
176
+ end
177
+
156
178
  def bucket
157
179
  @uploader.s3_bucket
158
180
  end
@@ -195,12 +217,14 @@ module CarrierWave
195
217
  end
196
218
 
197
219
  def connection
198
- @connection ||= Fog::AWS::S3.new(
220
+ @connection ||= Fog::AWS::Storage.new(
199
221
  :aws_access_key_id => uploader.s3_access_key_id,
200
- :aws_secret_access_key => uploader.s3_secret_access_key
222
+ :aws_secret_access_key => uploader.s3_secret_access_key,
223
+ :region => uploader.s3_region
201
224
  )
202
225
  end
203
226
 
204
227
  end # S3
205
228
  end # Storage
206
229
  end # CarrierWave
230
+
@@ -154,7 +154,7 @@ module CarrierWave
154
154
  end
155
155
 
156
156
  def initialize(filename)
157
- @image = ::MiniMagick::Image.from_file(filename)
157
+ @image = ::MiniMagick::Image.open(filename)
158
158
  end
159
159
  end
160
160
 
@@ -45,7 +45,7 @@ module CarrierWave
45
45
  Dir.glob(File.expand_path(File.join(cache_dir, '*'), CarrierWave.root)).each do |dir|
46
46
  time = dir.scan(/(\d{4})(\d{2})(\d{2})-(\d{2})(\d{2})/).first.map { |t| t.to_i }
47
47
  time = Time.utc(*time)
48
- if time < (Time.now - (60*60*24))
48
+ if time < (Time.now.utc - (60*60*24))
49
49
  FileUtils.rm_rf(dir)
50
50
  end
51
51
  end
@@ -8,17 +8,18 @@ module CarrierWave
8
8
  add_config :root
9
9
  add_config :permissions
10
10
  add_config :storage_engines
11
- add_config :s3_access # for old s3 support
12
- add_config :s3_access_policy # for new s3 support
11
+ add_config :s3_access_policy
13
12
  add_config :s3_bucket
14
13
  add_config :s3_access_key_id
15
14
  add_config :s3_secret_access_key
16
15
  add_config :s3_cnamed
17
16
  add_config :s3_headers
17
+ add_config :s3_region
18
18
  add_config :cloud_files_username
19
19
  add_config :cloud_files_api_key
20
20
  add_config :cloud_files_container
21
21
  add_config :cloud_files_cdn_host
22
+ add_config :grid_fs_connection
22
23
  add_config :grid_fs_database
23
24
  add_config :grid_fs_host
24
25
  add_config :grid_fs_port
@@ -29,14 +30,15 @@ module CarrierWave
29
30
  add_config :cache_dir
30
31
  add_config :enable_processing
31
32
  add_config :ensure_multipart_form
32
-
33
+ add_config :delete_tmp_file_after_storage
34
+
33
35
  # Mounting
34
36
  add_config :ignore_integrity_errors
35
37
  add_config :ignore_processing_errors
36
38
  add_config :validate_integrity
37
39
  add_config :validate_processing
38
40
  add_config :mount_on
39
-
41
+
40
42
  configure do |config|
41
43
  config.permissions = 0644
42
44
  config.storage_engines = {
@@ -48,11 +50,14 @@ module CarrierWave
48
50
  }
49
51
  config.storage = :file
50
52
  config.s3_headers = {}
53
+ config.s3_access_policy = :public_read
54
+ config.s3_region = 'us-east-1'
51
55
  config.grid_fs_database = 'carrierwave'
52
56
  config.grid_fs_host = 'localhost'
53
57
  config.grid_fs_port = 27017
54
58
  config.store_dir = 'uploads'
55
59
  config.cache_dir = 'uploads/tmp'
60
+ config.delete_tmp_file_after_storage = true
56
61
  config.ignore_integrity_errors = true
57
62
  config.ignore_processing_errors = true
58
63
  config.validate_integrity = true
@@ -56,7 +56,9 @@ module CarrierWave
56
56
  cache!(new_file) if new_file
57
57
  if @file and @cache_id
58
58
  with_callbacks(:store, new_file) do
59
- @file = storage.store!(@file)
59
+ new_file = storage.store!(@file)
60
+ @file.delete if delete_tmp_file_after_storage
61
+ @file = new_file
60
62
  @cache_id = nil
61
63
  end
62
64
  end
@@ -24,8 +24,8 @@ module CarrierWave
24
24
  #
25
25
  # [String] A JSON serializtion containing this uploader's URL
26
26
  #
27
- def to_json(*args)
28
- { 'url' => url }.to_json(*args)
27
+ def as_json(options = nil)
28
+ { :url => url }
29
29
  end
30
30
 
31
31
  end # Url
@@ -1,3 +1,3 @@
1
1
  module CarrierWave
2
- VERSION = "0.5.0"
2
+ VERSION = "0.5.1"
3
3
  end
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carrierwave
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 9
4
5
  prerelease: false
5
6
  segments:
6
7
  - 0
7
8
  - 5
8
- - 0
9
- version: 0.5.0
9
+ - 1
10
+ version: 0.5.1
10
11
  platform: ruby
11
12
  authors:
12
13
  - Jonas Nicklas
@@ -14,7 +15,7 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2010-09-23 00:00:00 +02:00
18
+ date: 2010-12-01 00:00:00 +00:00
18
19
  default_executable:
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
@@ -25,11 +26,11 @@ dependencies:
25
26
  requirements:
26
27
  - - ~>
27
28
  - !ruby/object:Gem::Version
29
+ hash: 7
28
30
  segments:
29
31
  - 3
30
32
  - 0
31
- - 0
32
- version: 3.0.0
33
+ version: "3.0"
33
34
  type: :runtime
34
35
  version_requirements: *id001
35
36
  - !ruby/object:Gem::Dependency
@@ -40,6 +41,7 @@ dependencies:
40
41
  requirements:
41
42
  - - ~>
42
43
  - !ruby/object:Gem::Version
44
+ hash: 7
43
45
  segments:
44
46
  - 3
45
47
  - 0
@@ -55,6 +57,7 @@ dependencies:
55
57
  requirements:
56
58
  - - ~>
57
59
  - !ruby/object:Gem::Version
60
+ hash: 27
58
61
  segments:
59
62
  - 1
60
63
  - 3
@@ -70,11 +73,12 @@ dependencies:
70
73
  requirements:
71
74
  - - ~>
72
75
  - !ruby/object:Gem::Version
76
+ hash: 29
73
77
  segments:
74
78
  - 0
75
- - 2
76
- - 30
77
- version: 0.2.30
79
+ - 3
80
+ - 7
81
+ version: 0.3.7
78
82
  type: :development
79
83
  version_requirements: *id004
80
84
  - !ruby/object:Gem::Dependency
@@ -85,6 +89,7 @@ dependencies:
85
89
  requirements:
86
90
  - - ">="
87
91
  - !ruby/object:Gem::Version
92
+ hash: 3
88
93
  segments:
89
94
  - 0
90
95
  version: "0"
@@ -98,6 +103,7 @@ dependencies:
98
103
  requirements:
99
104
  - - ">="
100
105
  - !ruby/object:Gem::Version
106
+ hash: 3
101
107
  segments:
102
108
  - 0
103
109
  version: "0"
@@ -111,6 +117,7 @@ dependencies:
111
117
  requirements:
112
118
  - - ">="
113
119
  - !ruby/object:Gem::Version
120
+ hash: 3
114
121
  segments:
115
122
  - 0
116
123
  version: "0"
@@ -124,6 +131,7 @@ dependencies:
124
131
  requirements:
125
132
  - - ">="
126
133
  - !ruby/object:Gem::Version
134
+ hash: 3
127
135
  segments:
128
136
  - 0
129
137
  version: "0"
@@ -137,6 +145,7 @@ dependencies:
137
145
  requirements:
138
146
  - - ">="
139
147
  - !ruby/object:Gem::Version
148
+ hash: 3
140
149
  segments:
141
150
  - 0
142
151
  version: "0"
@@ -150,6 +159,7 @@ dependencies:
150
159
  requirements:
151
160
  - - ">="
152
161
  - !ruby/object:Gem::Version
162
+ hash: 3
153
163
  segments:
154
164
  - 0
155
165
  version: "0"
@@ -163,6 +173,7 @@ dependencies:
163
173
  requirements:
164
174
  - - ">="
165
175
  - !ruby/object:Gem::Version
176
+ hash: 3
166
177
  segments:
167
178
  - 0
168
179
  version: "0"
@@ -176,6 +187,7 @@ dependencies:
176
187
  requirements:
177
188
  - - ">="
178
189
  - !ruby/object:Gem::Version
190
+ hash: 3
179
191
  segments:
180
192
  - 0
181
193
  version: "0"
@@ -189,6 +201,7 @@ dependencies:
189
201
  requirements:
190
202
  - - ">="
191
203
  - !ruby/object:Gem::Version
204
+ hash: 3
192
205
  segments:
193
206
  - 0
194
207
  version: "0"
@@ -202,6 +215,7 @@ dependencies:
202
215
  requirements:
203
216
  - - ">="
204
217
  - !ruby/object:Gem::Version
218
+ hash: 3
205
219
  segments:
206
220
  - 0
207
221
  version: "0"
@@ -215,40 +229,45 @@ dependencies:
215
229
  requirements:
216
230
  - - ~>
217
231
  - !ruby/object:Gem::Version
232
+ hash: 5
218
233
  segments:
219
234
  - 2
220
- - 1
221
- version: "2.1"
235
+ - 3
236
+ version: "2.3"
222
237
  type: :development
223
238
  version_requirements: *id015
224
239
  - !ruby/object:Gem::Dependency
225
- name: mongoid
240
+ name: bson_ext
226
241
  prerelease: false
227
242
  requirement: &id016 !ruby/object:Gem::Requirement
228
243
  none: false
229
244
  requirements:
230
245
  - - "="
231
246
  - !ruby/object:Gem::Version
247
+ hash: 17
232
248
  segments:
233
- - 2
234
- - 0
235
- - 0
236
- - beta
237
- - 17
238
- version: 2.0.0.beta.17
249
+ - 1
250
+ - 1
251
+ - 1
252
+ version: 1.1.1
239
253
  type: :development
240
254
  version_requirements: *id016
241
255
  - !ruby/object:Gem::Dependency
242
- name: bson_ext
256
+ name: mongoid
243
257
  prerelease: false
244
258
  requirement: &id017 !ruby/object:Gem::Requirement
245
259
  none: false
246
260
  requirements:
247
- - - ">="
261
+ - - "="
248
262
  - !ruby/object:Gem::Version
263
+ hash: 62196421
249
264
  segments:
265
+ - 2
250
266
  - 0
251
- version: "0"
267
+ - 0
268
+ - beta
269
+ - 19
270
+ version: 2.0.0.beta.19
252
271
  type: :development
253
272
  version_requirements: *id017
254
273
  - !ruby/object:Gem::Dependency
@@ -259,6 +278,7 @@ dependencies:
259
278
  requirements:
260
279
  - - ">="
261
280
  - !ruby/object:Gem::Version
281
+ hash: 3
262
282
  segments:
263
283
  - 0
264
284
  version: "0"
@@ -272,6 +292,7 @@ dependencies:
272
292
  requirements:
273
293
  - - ">="
274
294
  - !ruby/object:Gem::Version
295
+ hash: 3
275
296
  segments:
276
297
  - 0
277
298
  version: "0"
@@ -340,6 +361,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
340
361
  requirements:
341
362
  - - ">="
342
363
  - !ruby/object:Gem::Version
364
+ hash: 3
343
365
  segments:
344
366
  - 0
345
367
  version: "0"
@@ -348,6 +370,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
348
370
  requirements:
349
371
  - - ">="
350
372
  - !ruby/object:Gem::Version
373
+ hash: 3
351
374
  segments:
352
375
  - 0
353
376
  version: "0"