carrierwave 0.5.2 → 0.5.3
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 +104 -72
- data/lib/carrierwave.rb +7 -2
- data/lib/carrierwave/compatibility/paperclip.rb +11 -11
- data/lib/carrierwave/orm/activerecord.rb +10 -3
- data/lib/carrierwave/processing/image_science.rb +6 -0
- data/lib/carrierwave/processing/mini_magick.rb +1 -0
- data/lib/carrierwave/processing/rmagick.rb +1 -0
- data/lib/carrierwave/sanitized_file.rb +1 -1
- data/lib/carrierwave/storage/cloud_files.rb +20 -4
- data/lib/carrierwave/storage/fog.rb +317 -0
- data/lib/carrierwave/storage/s3.rb +10 -6
- data/lib/carrierwave/uploader/cache.rb +15 -1
- data/lib/carrierwave/uploader/configuration.rb +52 -28
- data/lib/carrierwave/uploader/download.rb +15 -4
- data/lib/carrierwave/uploader/url.rb +3 -2
- data/lib/carrierwave/uploader/versions.rb +4 -6
- data/lib/carrierwave/version.rb +1 -1
- metadata +112 -66
data/README.rdoc
CHANGED
@@ -1,9 +1,5 @@
|
|
1
1
|
= CarrierWave
|
2
2
|
|
3
|
-
http://carrierwave.rubyforge.org
|
4
|
-
|
5
|
-
== Summary
|
6
|
-
|
7
3
|
This gem provides a simple and extremely flexible way to upload files from Ruby applications.
|
8
4
|
It works well with Rack based web applications, such as Ruby on Rails.
|
9
5
|
|
@@ -15,10 +11,10 @@ It works well with Rack based web applications, such as Ruby on Rails.
|
|
15
11
|
|
16
12
|
== Getting Help
|
17
13
|
|
18
|
-
* Please
|
19
|
-
* Please report
|
14
|
+
* Please ask the {Groogle Group}[http://groups.google.com/group/carrierwave] for help if you have any questions.
|
15
|
+
* Please report bugs on the {issue tracker}[http://github.com/jnicklas/carrierwave/issues] but read the "getting help" section in the wiki first.
|
20
16
|
|
21
|
-
==
|
17
|
+
== Installation
|
22
18
|
|
23
19
|
Install the latest stable release:
|
24
20
|
|
@@ -28,12 +24,10 @@ In Rails, add it to your Gemfile:
|
|
28
24
|
|
29
25
|
gem 'carrierwave'
|
30
26
|
|
31
|
-
|
32
|
-
|
33
|
-
CarrierWave is only compatible with Rails 3 and later as of version 0.5. If you want to use
|
34
|
-
Rails 2, please use the latest gem in the 0.4.X series.
|
27
|
+
Note that CarrierWave is not compatible with Rails 2 as of version 0.5. If you want to use
|
28
|
+
Rails 2, please use the 0.4-stable branch on GitHub.
|
35
29
|
|
36
|
-
==
|
30
|
+
== Getting Started
|
37
31
|
|
38
32
|
Start off by generating an uploader:
|
39
33
|
|
@@ -96,6 +90,12 @@ automatically be stored when the record is saved.
|
|
96
90
|
u.avatar.url # => '/url/to/file.png'
|
97
91
|
u.avatar.current_path # => 'path/to/file.png'
|
98
92
|
|
93
|
+
If using DataMapper, make sure to disable auto_validation on the mounted column.
|
94
|
+
|
95
|
+
If using Mongoid, note that embedded documents files aren't saved when parent documents are saved.
|
96
|
+
You must explicitly call save on embedded documents in order to save their attached files.
|
97
|
+
You can read more about this {here}[https://github.com/jnicklas/carrierwave/issues#issue/81]
|
98
|
+
|
99
99
|
== Changing the storage directory
|
100
100
|
|
101
101
|
In order to change where uploaded files are put, just override the +store_dir+
|
@@ -154,7 +154,7 @@ contain Russian letters:
|
|
154
154
|
end
|
155
155
|
end
|
156
156
|
|
157
|
-
Also make sure that allowing non-latin characters won't cause a
|
157
|
+
Also make sure that allowing non-latin characters won't cause a compatibility issue with a third-party
|
158
158
|
plugins or client-side software.
|
159
159
|
|
160
160
|
== Adding versions
|
@@ -277,7 +277,7 @@ this easily by overriding the +default_url+ method in your uploader:
|
|
277
277
|
|
278
278
|
You might come to a situation where you want to retroactively change a version
|
279
279
|
or add a new one. You can use the recreate_versions! method to recreate the
|
280
|
-
versions from the base file. This uses a naive approach which will
|
280
|
+
versions from the base file. This uses a naive approach which will re-upload and
|
281
281
|
process all versions.
|
282
282
|
|
283
283
|
instance = MyUploader.new
|
@@ -309,7 +309,7 @@ If you're using Rails, create an initializer for this:
|
|
309
309
|
|
310
310
|
config/initializers/carrierwave.rb
|
311
311
|
|
312
|
-
== Testing CarrierWave
|
312
|
+
== Testing with CarrierWave
|
313
313
|
|
314
314
|
It's a good idea to test you uploaders in isolation. In order to speed up your
|
315
315
|
tests, it's recommended to switch off processing in your tests, and to use the
|
@@ -365,63 +365,87 @@ Fog[http://github.com/geemus/fog] is used to support Amazon S3. Ensure you have
|
|
365
365
|
|
366
366
|
gem install fog
|
367
367
|
|
368
|
-
You'll need to
|
368
|
+
You'll need to provide your fog_credentials and a fog_directory (also known as a bucket) in an initializer.
|
369
|
+
You can also pass in additional options, as documented fully in lib/storage/fog.rb. Here's a full example:
|
369
370
|
|
370
371
|
CarrierWave.configure do |config|
|
371
|
-
config.
|
372
|
-
|
373
|
-
|
372
|
+
config.fog_credentials = {
|
373
|
+
:provider => 'AWS', # required
|
374
|
+
:aws_access_key_id => 'xxx', # required
|
375
|
+
:aws_secret_access_key => 'yyy', # required
|
376
|
+
:region => 'eu-west-1' # optional, defaults to 'us-east-1'
|
377
|
+
}
|
378
|
+
config.fog_directory = 'name_of_directory' # required
|
379
|
+
config.fog_host = 'https://assets.example.com' # optional, defaults to nil
|
380
|
+
config.fog_public = false # optional, defaults to true
|
381
|
+
config.fog_attributes = {'Cache-Control'=>'max-age=315576000'} # optional, defaults to {}
|
374
382
|
end
|
375
383
|
|
376
|
-
|
377
|
-
|
378
|
-
In your uploader, set the storage to :s3
|
384
|
+
In your uploader, set the storage to :fog
|
379
385
|
|
380
386
|
class AvatarUploader < CarrierWave::Uploader::Base
|
381
|
-
storage :
|
387
|
+
storage :fog
|
382
388
|
end
|
383
389
|
|
384
|
-
You can
|
390
|
+
That's it! You can still use the <code>CarrierWave::Uploader#url</code> method to return the url to the file on Amazon S3.
|
385
391
|
|
386
|
-
|
387
|
-
config.s3_region = 'eu-west-1'
|
388
|
-
end
|
389
|
-
|
390
|
-
Available options are defined in Fog Storage[http://github.com/geemus/fog/blob/master/lib/fog/aws/storage.rb]
|
391
|
-
|
392
|
-
'eu-west-1' => 's3-eu-west-1.amazonaws.com'
|
393
|
-
'us-east-1' => 's3.amazonaws.com'
|
394
|
-
'ap-southeast-1' => 's3-ap-southeast-1.amazonaws.com'
|
395
|
-
'us-west-1' => 's3-us-west-1.amazonaws.com'
|
392
|
+
== Using Rackspace Cloud Files
|
396
393
|
|
397
|
-
|
398
|
-
the url to the file on Amazon S3.
|
394
|
+
Fog[http://github.com/geemus/fog] is used to support Rackspace Cloud Files. Ensure you have it installed:
|
399
395
|
|
400
|
-
|
396
|
+
gem install fog
|
401
397
|
|
402
|
-
|
403
|
-
You must also create a container for Carrierwave to use, and mark it public (publish it to the CDN)
|
398
|
+
You'll need to configure a directory (also known as a container), username and api key like this in an initializer:
|
404
399
|
|
405
400
|
CarrierWave.configure do |config|
|
406
|
-
config.
|
407
|
-
|
408
|
-
|
401
|
+
config.fog_credentials = {
|
402
|
+
:provider => 'Rackspace',
|
403
|
+
:rackspace_username => 'xxxxxx',
|
404
|
+
:rackspace_api_key => 'yyyyyy'
|
405
|
+
}
|
406
|
+
config.fog_directory = 'name_of_directory'
|
409
407
|
end
|
410
408
|
|
411
409
|
You can optionally include your CDN host name in the configuration.
|
412
410
|
This is *highly* recommended, as without it every request requires a lookup
|
413
411
|
of this information.
|
414
412
|
|
415
|
-
config.
|
413
|
+
config.fog_host = "c000000.cdn.rackspacecloud.com"
|
416
414
|
|
417
|
-
In your uploader, set the storage to :
|
415
|
+
In your uploader, set the storage to :fog
|
418
416
|
|
419
417
|
class AvatarUploader < CarrierWave::Uploader::Base
|
420
|
-
storage :
|
418
|
+
storage :fog
|
421
419
|
end
|
422
420
|
|
423
421
|
That's it! You can still use the <code>CarrierWave::Uploader#url</code> method to return
|
424
|
-
the url to the file on
|
422
|
+
the url to the file on Rackspace Cloud Files.
|
423
|
+
|
424
|
+
== Using Google Storage for Developers
|
425
|
+
|
426
|
+
Fog[http://github.com/geemus/fog] is used to support Google Storage for Developers. Ensure you have it installed:
|
427
|
+
|
428
|
+
gem install fog
|
429
|
+
|
430
|
+
You'll need to configure a directory (also known as a bucket), access key id and secret access key like this in an initializer:
|
431
|
+
|
432
|
+
CarrierWave.configure do |config|
|
433
|
+
config.fog_credentials = {
|
434
|
+
:provider => 'Google',
|
435
|
+
:google_storage_access_key_id => 'xxxxxx',
|
436
|
+
:google_storage_secret_access_key => 'yyyyyy'
|
437
|
+
}
|
438
|
+
config.fog_directory = 'name_of_directory'
|
439
|
+
end
|
440
|
+
|
441
|
+
In your uploader, set the storage to :fog
|
442
|
+
|
443
|
+
class AvatarUploader < CarrierWave::Uploader::Base
|
444
|
+
storage :fog
|
445
|
+
end
|
446
|
+
|
447
|
+
That's it! You can still use the <code>CarrierWave::Uploader#url</code> method to return
|
448
|
+
the url to the file on Google.
|
425
449
|
|
426
450
|
== Using MongoDB's GridFS store
|
427
451
|
|
@@ -512,7 +536,7 @@ for the RMagick processor.
|
|
512
536
|
process :resize_to_fill => [200, 200]
|
513
537
|
end
|
514
538
|
|
515
|
-
== Migrating
|
539
|
+
== Migrating from Paperclip
|
516
540
|
|
517
541
|
If you are using Paperclip, you can use the provided compatibility module:
|
518
542
|
|
@@ -521,19 +545,19 @@ If you are using Paperclip, you can use the provided compatibility module:
|
|
521
545
|
end
|
522
546
|
|
523
547
|
See the documentation for <code>CarrierWave::Compatibility::Paperclip</code> for more
|
524
|
-
|
548
|
+
details.
|
525
549
|
|
526
550
|
Be sure to use mount_on to specify the correct column:
|
527
551
|
|
528
552
|
mount_uploader :avatar, AvatarUploader, :mount_on => :avatar_file_name
|
529
553
|
|
530
|
-
Unfortunately
|
554
|
+
Unfortunately attachment_fu differs too much in philosophy for there to be a
|
531
555
|
sensible compatibility mode. Patches for migrating from other solutions will be
|
532
556
|
happily accepted.
|
533
557
|
|
534
558
|
== i18n
|
535
559
|
|
536
|
-
The
|
560
|
+
The Active Record validations use the Rails i18n framework. Add these keys to
|
537
561
|
your translations file:
|
538
562
|
|
539
563
|
errors:
|
@@ -541,6 +565,36 @@ your translations file:
|
|
541
565
|
carrierwave_processing_error: 'Cannot resize image.'
|
542
566
|
carrierwave_integrity_error: 'Not an image.'
|
543
567
|
|
568
|
+
== Contributing to CarrierWave
|
569
|
+
|
570
|
+
CarrierWave thrives on a large number of {contributors}[https://github.com/jnicklas/carrierwave/contributors],
|
571
|
+
and pull requests are very welcome. Before submitting a pull request, please make sure that your changes are well tested.
|
572
|
+
|
573
|
+
You'll need to install bundler and the gem dependencies:
|
574
|
+
|
575
|
+
gem install bundler
|
576
|
+
bundle install
|
577
|
+
|
578
|
+
You should now be able to run the local tests:
|
579
|
+
|
580
|
+
bundle exec rake
|
581
|
+
|
582
|
+
You can also run the remote specs by creating a ~/.fog file:
|
583
|
+
|
584
|
+
:carrierwave:
|
585
|
+
:aws_access_key_id: xxx
|
586
|
+
:aws_secret_access_key: yyy
|
587
|
+
:rackspace_username: xxx
|
588
|
+
:rackspace_api_key: yyy
|
589
|
+
:google_storage_access_key_id: xxx
|
590
|
+
:google_storage_secret_access_key: yyy
|
591
|
+
|
592
|
+
You should now be able to run the remote tests:
|
593
|
+
|
594
|
+
REMOTE=true bundle exec rake
|
595
|
+
|
596
|
+
Please test with the latest Ruby 1.8.x and 1.9.x versions using RVM if possible.
|
597
|
+
|
544
598
|
== License
|
545
599
|
|
546
600
|
Copyright (c) 2008 Jonas Nicklas
|
@@ -563,25 +617,3 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
563
617
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
564
618
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
565
619
|
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
566
|
-
|
567
|
-
== Development
|
568
|
-
|
569
|
-
In order to setup a development environment and run the specs, you'll
|
570
|
-
need to install bundler:
|
571
|
-
|
572
|
-
gem install bundler
|
573
|
-
|
574
|
-
And then install the dependencies:
|
575
|
-
|
576
|
-
bundle install
|
577
|
-
|
578
|
-
You should now be able to run the tests:
|
579
|
-
|
580
|
-
bundle exec rake
|
581
|
-
|
582
|
-
You can also run the remote specs for Amazon S3 and Rackspace Cloud Files like so:
|
583
|
-
|
584
|
-
S3_SPEC=true CARRIERWAVE_TEST_BUCKET= S3_ACCESS_KEY_ID= S3_SECRET_ACCESS_KEY= bundle exec rake
|
585
|
-
CLOUDFILES_SPEC=true CLOUD_FILES_USER_NAME= CLOUD_FILES_API_KEY= CARRIERWAVE_TEST_CONTAINER= bundle exec rake
|
586
|
-
|
587
|
-
Issues are reported on GitHub, pull requests are very welcome!
|
data/lib/carrierwave.rb
CHANGED
@@ -35,6 +35,7 @@ module CarrierWave
|
|
35
35
|
module Storage
|
36
36
|
autoload :Abstract, 'carrierwave/storage/abstract'
|
37
37
|
autoload :File, 'carrierwave/storage/file'
|
38
|
+
autoload :Fog, 'carrierwave/storage/fog'
|
38
39
|
autoload :S3, 'carrierwave/storage/s3'
|
39
40
|
autoload :GridFS, 'carrierwave/storage/grid_fs'
|
40
41
|
autoload :RightS3, 'carrierwave/storage/right_s3'
|
@@ -84,6 +85,12 @@ elsif defined?(Rails)
|
|
84
85
|
initializer "carrierwave.setup_paths" do
|
85
86
|
CarrierWave.root = Rails.root.join(Rails.public_path).to_s
|
86
87
|
end
|
88
|
+
|
89
|
+
initializer "carrierwave.active_record" do
|
90
|
+
ActiveSupport.on_load :active_record do
|
91
|
+
require 'carrierwave/orm/activerecord'
|
92
|
+
end
|
93
|
+
end
|
87
94
|
end
|
88
95
|
end
|
89
96
|
|
@@ -93,8 +100,6 @@ elsif defined?(Sinatra)
|
|
93
100
|
|
94
101
|
end
|
95
102
|
|
96
|
-
|
97
|
-
require 'carrierwave/orm/activerecord' if defined?(ActiveRecord)
|
98
103
|
require 'carrierwave/orm/datamapper' if defined?(DataMapper)
|
99
104
|
require 'carrierwave/orm/sequel' if defined?(Sequel)
|
100
105
|
require 'carrierwave/orm/mongoid' if defined?(Mongoid)
|
@@ -77,17 +77,17 @@ module CarrierWave
|
|
77
77
|
end
|
78
78
|
|
79
79
|
def mappings
|
80
|
-
|
81
|
-
:rails_root
|
82
|
-
:rails_env
|
83
|
-
:class
|
84
|
-
:
|
85
|
-
:
|
86
|
-
:attachment
|
87
|
-
:style
|
88
|
-
:basename
|
89
|
-
:extension
|
90
|
-
|
80
|
+
[
|
81
|
+
[:rails_root , lambda{|u, f| Rails.root }],
|
82
|
+
[:rails_env , lambda{|u, f| Rails.env }],
|
83
|
+
[:class , lambda{|u, f| u.model.class.name.underscore.pluralize}],
|
84
|
+
[:id_partition , lambda{|u, f| ("%09d" % u.model.id).scan(/\d{3}/).join("/")}],
|
85
|
+
[:id , lambda{|u, f| u.model.id }],
|
86
|
+
[:attachment , lambda{|u, f| u.mounted_as.to_s.downcase.pluralize }],
|
87
|
+
[:style , lambda{|u, f| u.paperclip_style }],
|
88
|
+
[:basename , lambda{|u, f| f.gsub(/#{File.extname(f)}$/, "") }],
|
89
|
+
[:extension , lambda{|u, f| File.extname(f).gsub(/^\.+/, "")}]
|
90
|
+
]
|
91
91
|
end
|
92
92
|
|
93
93
|
end # Paperclip
|
@@ -24,9 +24,16 @@ module CarrierWave
|
|
24
24
|
validates_integrity_of column if uploader_option(column.to_sym, :validate_integrity)
|
25
25
|
validates_processing_of column if uploader_option(column.to_sym, :validate_processing)
|
26
26
|
|
27
|
-
after_save "store_#{column}!"
|
28
|
-
before_save "write_#{column}_identifier"
|
29
|
-
after_destroy "remove_#{column}!"
|
27
|
+
after_save :"store_#{column}!"
|
28
|
+
before_save :"write_#{column}_identifier"
|
29
|
+
after_destroy :"remove_#{column}!"
|
30
|
+
|
31
|
+
class_eval <<-RUBY, __FILE__, __LINE__+1
|
32
|
+
def #{column}=(new_file)
|
33
|
+
#{column}_will_change!
|
34
|
+
super
|
35
|
+
end
|
36
|
+
RUBY
|
30
37
|
|
31
38
|
end
|
32
39
|
|
@@ -34,6 +34,8 @@ module CarrierWave
|
|
34
34
|
# [height (Integer)] the height to scale the image to
|
35
35
|
#
|
36
36
|
def resize_to_fit(new_width, new_height)
|
37
|
+
cache_stored_file! if !cached?
|
38
|
+
|
37
39
|
::ImageScience.with_image(self.current_path) do |img|
|
38
40
|
width, height = extract_dimensions(img.width, img.height, new_width, new_height)
|
39
41
|
img.resize( width, height ) do |file|
|
@@ -55,6 +57,8 @@ module CarrierWave
|
|
55
57
|
# [height (Integer)] the height to scale the image to
|
56
58
|
#
|
57
59
|
def resize_to_fill(new_width, new_height)
|
60
|
+
cache_stored_file! if !cached?
|
61
|
+
|
58
62
|
::ImageScience.with_image(self.current_path) do |img|
|
59
63
|
width, height = extract_dimensions_for_crop(img.width, img.height, new_width, new_height)
|
60
64
|
x_offset, y_offset = extract_placement_for_crop(width, height, new_width, new_height)
|
@@ -97,6 +101,8 @@ module CarrierWave
|
|
97
101
|
# [height (Integer)] the height to scale the image to
|
98
102
|
#
|
99
103
|
def resize_to_limit(new_width, new_height)
|
104
|
+
cache_stored_file! if !cached?
|
105
|
+
|
100
106
|
::ImageScience.with_image(self.current_path) do |img|
|
101
107
|
if img.width > new_width or img.height > new_height
|
102
108
|
resize_to_fit(new_width, new_height)
|
@@ -7,12 +7,16 @@ module CarrierWave
|
|
7
7
|
##
|
8
8
|
# Uploads things to Rackspace Cloud Files webservices using the Rackspace libraries (cloudfiles gem).
|
9
9
|
# In order for CarrierWave to connect to Cloud Files, you'll need to specify an username, api key
|
10
|
-
# and container
|
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)
|
11
13
|
#
|
12
14
|
# CarrierWave.configure do |config|
|
13
15
|
# config.cloud_files_username = "xxxxxx"
|
14
16
|
# config.cloud_files_api_key = "xxxxxx"
|
15
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
|
16
20
|
# end
|
17
21
|
#
|
18
22
|
# You can optionally include your CDN host name in the configuration.
|
@@ -60,7 +64,12 @@ module CarrierWave
|
|
60
64
|
# Remove the file from Cloud Files
|
61
65
|
#
|
62
66
|
def delete
|
63
|
-
|
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
|
64
73
|
end
|
65
74
|
|
66
75
|
##
|
@@ -75,7 +84,11 @@ module CarrierWave
|
|
75
84
|
if @uploader.cloud_files_cdn_host
|
76
85
|
"http://" + @uploader.cloud_files_cdn_host + "/" + @path
|
77
86
|
else
|
78
|
-
|
87
|
+
begin
|
88
|
+
cf_container.object(@path).public_url
|
89
|
+
rescue ::CloudFiles::Exception::NoSuchObject
|
90
|
+
nil
|
91
|
+
end
|
79
92
|
end
|
80
93
|
end
|
81
94
|
|
@@ -114,7 +127,10 @@ module CarrierWave
|
|
114
127
|
end
|
115
128
|
|
116
129
|
def cf_connection
|
117
|
-
|
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)
|
118
134
|
end
|
119
135
|
|
120
136
|
def cf_container
|