locomotive_carrierwave 0.5.0.1 → 0.5.4.beta1

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 +714 -0
  2. data/lib/carrierwave.rb +9 -3
  3. data/lib/carrierwave/compatibility/paperclip.rb +18 -18
  4. data/lib/carrierwave/mount.rb +30 -25
  5. data/lib/carrierwave/orm/activerecord.rb +14 -4
  6. data/lib/carrierwave/orm/mongoid.rb +29 -8
  7. data/lib/carrierwave/processing/image_science.rb +25 -2
  8. data/lib/carrierwave/processing/mini_magick.rb +4 -3
  9. data/lib/carrierwave/processing/rmagick.rb +3 -2
  10. data/lib/carrierwave/sanitized_file.rb +23 -38
  11. data/lib/carrierwave/storage/abstract.rb +0 -3
  12. data/lib/carrierwave/storage/cloud_files.rb +30 -10
  13. data/lib/carrierwave/storage/file.rb +1 -8
  14. data/lib/carrierwave/storage/fog.rb +332 -0
  15. data/lib/carrierwave/storage/grid_fs.rb +6 -6
  16. data/lib/carrierwave/storage/s3.rb +31 -34
  17. data/lib/carrierwave/test/matchers.rb +1 -1
  18. data/lib/carrierwave/uploader.rb +0 -1
  19. data/lib/carrierwave/uploader/cache.rb +28 -7
  20. data/lib/carrierwave/uploader/callbacks.rb +11 -17
  21. data/lib/carrierwave/uploader/configuration.rb +62 -34
  22. data/lib/carrierwave/uploader/download.rb +16 -5
  23. data/lib/carrierwave/uploader/extension_whitelist.rb +14 -3
  24. data/lib/carrierwave/uploader/mountable.rb +1 -1
  25. data/lib/carrierwave/uploader/processing.rb +20 -15
  26. data/lib/carrierwave/uploader/proxy.rb +16 -1
  27. data/lib/carrierwave/uploader/store.rb +3 -9
  28. data/lib/carrierwave/uploader/url.rb +3 -2
  29. data/lib/carrierwave/uploader/versions.rb +78 -25
  30. data/lib/carrierwave/version.rb +1 -1
  31. data/lib/generators/templates/uploader.rb +2 -1
  32. metadata +64 -102
  33. data/README.rdoc +0 -532
  34. data/lib/carrierwave/orm/datamapper.rb +0 -37
  35. data/lib/carrierwave/orm/sequel.rb +0 -45
  36. data/lib/carrierwave/uploader/rename.rb +0 -62
@@ -11,7 +11,7 @@ module CarrierWave
11
11
  # connection or you reuse an existing connection.
12
12
  #
13
13
  # Creating a connection looks something like this:
14
- #
14
+ #
15
15
  # CarrierWave.configure do |config|
16
16
  # config.storage = :grid_fs
17
17
  # config.grid_fs_host = "your-host.com"
@@ -23,7 +23,7 @@ module CarrierWave
23
23
  # end
24
24
  #
25
25
  # In the above example your documents url will look like:
26
- #
26
+ #
27
27
  # http://your-app.com/images/:document-identifier-here
28
28
  #
29
29
  # When you already have a Mongo connection object (for example through Mongoid)
@@ -45,7 +45,7 @@ module CarrierWave
45
45
  end
46
46
 
47
47
  def path
48
- nil
48
+ @path
49
49
  end
50
50
 
51
51
  def url
@@ -61,7 +61,7 @@ module CarrierWave
61
61
  end
62
62
 
63
63
  def write(file)
64
- grid.open(@uploader.store_path, 'w', :content_type => file.content_type) do |f|
64
+ grid.open(@uploader.store_path, 'w', :content_type => file.content_type) do |f|
65
65
  f.write(file.read)
66
66
  end
67
67
  end
@@ -73,7 +73,7 @@ module CarrierWave
73
73
  def content_type
74
74
  grid.open(@path, 'r').content_type
75
75
  end
76
-
76
+
77
77
  def file_length
78
78
  grid.open(@path, 'r').file_length
79
79
  end
@@ -92,7 +92,7 @@ module CarrierWave
92
92
  db
93
93
  end
94
94
  end
95
-
95
+
96
96
  def grid
97
97
  @grid ||= Mongo::GridFileSystem.new(database)
98
98
  end
@@ -53,6 +53,19 @@ module CarrierWave
53
53
  #
54
54
  # http://bucketname.domain.tld.s3.amazonaws.com/path/to/file
55
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
+ #
56
69
  class S3 < Abstract
57
70
 
58
71
  class File
@@ -94,17 +107,6 @@ module CarrierWave
94
107
  connection.delete_object(bucket, @path)
95
108
  end
96
109
 
97
- def rename(new_path)
98
- file = connection.put_object(bucket, new_path, read,
99
- {
100
- 'x-amz-acl' => access_policy.to_s.gsub('_', '-'),
101
- 'Content-Type' => content_type
102
- }.merge(@uploader.s3_headers)
103
- )
104
- delete
105
- file
106
- end
107
-
108
110
  ##
109
111
  # Returns the url on Amazon's S3 service
110
112
  #
@@ -121,15 +123,16 @@ module CarrierWave
121
123
  end
122
124
 
123
125
  def public_url
126
+ scheme = use_ssl? ? 'https' : 'http'
124
127
  if cnamed?
125
- ["http://#{bucket}", path].compact.join('/')
128
+ ["#{scheme}://#{bucket}", path].compact.join('/')
126
129
  else
127
- ["http://#{bucket}.s3.amazonaws.com", path].compact.join('/')
130
+ ["#{scheme}://#{bucket}.s3.amazonaws.com", path].compact.join('/')
128
131
  end
129
132
  end
130
133
 
131
134
  def authenticated_url
132
- connection.get_object_url(bucket, path, Time.now + 60 * 10)
135
+ connection.get_object_url(bucket, path, Time.now + authentication_timeout)
133
136
  end
134
137
 
135
138
  def store(file)
@@ -165,6 +168,10 @@ module CarrierWave
165
168
 
166
169
  private
167
170
 
171
+ def use_ssl?
172
+ @uploader.s3_use_ssl
173
+ end
174
+
168
175
  def cnamed?
169
176
  @uploader.s3_cnamed
170
177
  end
@@ -177,6 +184,10 @@ module CarrierWave
177
184
  @uploader.s3_bucket
178
185
  end
179
186
 
187
+ def authentication_timeout
188
+ @uploader.s3_authentication_timeout
189
+ end
190
+
180
191
  def connection
181
192
  @base.connection
182
193
  end
@@ -214,30 +225,16 @@ module CarrierWave
214
225
  CarrierWave::Storage::S3::File.new(uploader, self, uploader.store_path(identifier))
215
226
  end
216
227
 
217
- ##
218
- # Rename a file on S3
219
- #
220
- # === Parameters
221
- #
222
- # [file (CarrierWave::Storage::S3::File)] the file to rename
223
- #
224
- # === Returns
225
- #
226
- # [CarrierWave::Storage::S3::File] the renamed file
227
- #
228
- def rename!(file)
229
- path = uploader.store_path
230
- file.rename(path)
231
- CarrierWave::Storage::S3::File.new(uploader, self, path)
232
- end
233
-
234
228
  def connection
235
- @connection ||= Fog::AWS::Storage.new(
236
- :aws_access_key_id => uploader.s3_access_key_id,
237
- :aws_secret_access_key => uploader.s3_secret_access_key
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
238
234
  )
239
235
  end
240
236
 
241
237
  end # S3
242
238
  end # Storage
243
239
  end # CarrierWave
240
+
@@ -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
 
@@ -38,7 +38,6 @@ module CarrierWave
38
38
  include CarrierWave::Uploader::Versions
39
39
  include CarrierWave::Uploader::DefaultUrl
40
40
  include CarrierWave::Uploader::Configuration
41
- include CarrierWave::Uploader::Rename
42
41
  end # Base
43
42
 
44
43
  end # Uploader
@@ -41,11 +41,11 @@ module CarrierWave
41
41
  # This only works as long as you haven't done anything funky with your cache_dir.
42
42
  # It's recommended that you keep cache files in one place only.
43
43
  #
44
- def clean_cached_files!
44
+ def clean_cached_files!(seconds=60*60*24)
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 - seconds)
49
49
  FileUtils.rm_rf(dir)
50
50
  end
51
51
  end
@@ -63,6 +63,19 @@ module CarrierWave
63
63
  @cache_id
64
64
  end
65
65
 
66
+ ##
67
+ # Caches the remotely stored file
68
+ #
69
+ # This is useful when about to process images. Most processing solutions
70
+ # require the file to be stored on the local filesystem.
71
+ #
72
+ def cache_stored_file!
73
+ sanitized = SanitizedFile.new :tempfile => StringIO.new(file.read),
74
+ :filename => File.basename(path), :content_type => file.content_type
75
+
76
+ cache! sanitized
77
+ end
78
+
66
79
  ##
67
80
  # Returns a String which uniquely identifies the currently cached file for later retrieval
68
81
  #
@@ -87,12 +100,11 @@ module CarrierWave
87
100
  #
88
101
  def cache!(new_file)
89
102
  new_file = CarrierWave::SanitizedFile.new(new_file)
90
- raise CarrierWave::FormNotMultipart if new_file.is_path? && ensure_multipart_form
91
103
 
92
104
  unless new_file.empty?
93
- with_callbacks(:cache, new_file) do
94
- @original_file ||= self.file.clone if self.file
105
+ raise CarrierWave::FormNotMultipart if new_file.is_path? && ensure_multipart_form
95
106
 
107
+ with_callbacks(:cache, new_file) do
96
108
  self.cache_id = CarrierWave.generate_cache_id unless cache_id
97
109
 
98
110
  @filename = new_file.filename
@@ -118,7 +130,16 @@ module CarrierWave
118
130
  with_callbacks(:retrieve_from_cache, cache_name) do
119
131
  self.cache_id, self.original_filename = cache_name.to_s.split('/', 2)
120
132
  @filename = original_filename
121
- @file = CarrierWave::SanitizedFile.new(cache_path)
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
122
143
  end
123
144
  end
124
145
 
@@ -139,7 +160,7 @@ module CarrierWave
139
160
  end
140
161
 
141
162
  def original_filename=(filename)
142
- raise CarrierWave::InvalidParameter, "invalid filename" unless filename =~ /\A[a-z0-9\.\-\+_]+\z/i
163
+ raise CarrierWave::InvalidParameter, "invalid filename" if filename =~ CarrierWave::SanitizedFile.sanitize_regexp
143
164
  @original_filename = filename
144
165
  end
145
166
 
@@ -6,33 +6,27 @@ module CarrierWave
6
6
  extend ActiveSupport::Concern
7
7
 
8
8
  included do
9
- class_inheritable_accessor :_before_callbacks, :_after_callbacks
9
+ class_attribute :_before_callbacks, :_after_callbacks,
10
+ :instance_writer => false
11
+ self._before_callbacks = Hash.new []
12
+ self._after_callbacks = Hash.new []
10
13
  end
11
14
 
12
15
  def with_callbacks(kind, *args)
13
- self.class._before_callbacks_for(kind).each { |callback| self.send(callback, *args) }
16
+ self.class._before_callbacks[kind].each { |c| send c, *args }
14
17
  yield
15
- self.class._after_callbacks_for(kind).each { |callback| self.send(callback, *args) }
18
+ self.class._after_callbacks[kind].each { |c| send c, *args }
16
19
  end
17
20
 
18
21
  module ClassMethods
19
-
20
- def _before_callbacks_for(kind) #:nodoc:
21
- (self._before_callbacks || { kind => [] })[kind] || []
22
- end
23
-
24
- def _after_callbacks_for(kind) #:nodoc:
25
- (self._after_callbacks || { kind => [] })[kind] || []
26
- end
27
-
28
- def before(kind, callback)
29
- self._before_callbacks ||= {}
30
- self._before_callbacks[kind] = _before_callbacks_for(kind) + [callback]
22
+ def before(kind, callback)
23
+ self._before_callbacks = self._before_callbacks.
24
+ merge kind => _before_callbacks[kind] + [callback]
31
25
  end
32
26
 
33
27
  def after(kind, callback)
34
- self._after_callbacks ||= {}
35
- self._after_callbacks[kind] = _after_callbacks_for(kind) + [callback]
28
+ self._after_callbacks = self._after_callbacks.
29
+ merge kind => _after_callbacks[kind] + [callback]
36
30
  end
37
31
  end # ClassMethods
38
32
 
@@ -5,6 +5,8 @@ module CarrierWave
5
5
  extend ActiveSupport::Concern
6
6
 
7
7
  included do
8
+ class_inheritable_accessor :_storage, :instance_reader => false, :instance_writer => false
9
+
8
10
  add_config :root
9
11
  add_config :permissions
10
12
  add_config :storage_engines
@@ -14,10 +16,15 @@ module CarrierWave
14
16
  add_config :s3_secret_access_key
15
17
  add_config :s3_cnamed
16
18
  add_config :s3_headers
19
+ add_config :s3_region
20
+ add_config :s3_use_ssl
21
+ add_config :s3_authentication_timeout
17
22
  add_config :cloud_files_username
18
23
  add_config :cloud_files_api_key
19
24
  add_config :cloud_files_container
20
25
  add_config :cloud_files_cdn_host
26
+ add_config :cloud_files_auth_url
27
+ add_config :cloud_files_snet
21
28
  add_config :grid_fs_connection
22
29
  add_config :grid_fs_database
23
30
  add_config :grid_fs_host
@@ -29,6 +36,16 @@ module CarrierWave
29
36
  add_config :cache_dir
30
37
  add_config :enable_processing
31
38
  add_config :ensure_multipart_form
39
+ add_config :delete_tmp_file_after_storage
40
+ add_config :remove_previously_stored_files_after_update
41
+
42
+ # fog
43
+ add_config :fog_attributes
44
+ add_config :fog_credentials
45
+ add_config :fog_directory
46
+ add_config :fog_host
47
+ add_config :fog_public
48
+ add_config :fog_authenticated_url_expiration
32
49
 
33
50
  # Mounting
34
51
  add_config :ignore_integrity_errors
@@ -37,31 +54,8 @@ module CarrierWave
37
54
  add_config :validate_processing
38
55
  add_config :mount_on
39
56
 
40
- configure do |config|
41
- config.permissions = 0644
42
- config.storage_engines = {
43
- :file => "CarrierWave::Storage::File",
44
- :s3 => "CarrierWave::Storage::S3",
45
- :grid_fs => "CarrierWave::Storage::GridFS",
46
- :right_s3 => "CarrierWave::Storage::RightS3",
47
- :cloud_files => "CarrierWave::Storage::CloudFiles"
48
- }
49
- config.storage = :file
50
- config.s3_headers = {}
51
- config.s3_access_policy = :public_read
52
- config.grid_fs_database = 'carrierwave'
53
- config.grid_fs_host = 'localhost'
54
- config.grid_fs_port = 27017
55
- config.store_dir = 'uploads'
56
- config.cache_dir = 'uploads/tmp'
57
- config.ignore_integrity_errors = true
58
- config.ignore_processing_errors = true
59
- config.validate_integrity = true
60
- config.validate_processing = true
61
- config.root = CarrierWave.root
62
- config.enable_processing = true
63
- config.ensure_multipart_form = true
64
- end
57
+ # set default values
58
+ reset_config
65
59
  end
66
60
 
67
61
  module ClassMethods
@@ -90,19 +84,13 @@ module CarrierWave
90
84
  # storage MyCustomStorageEngine
91
85
  #
92
86
  def storage(storage = nil)
93
- if storage.is_a?(Symbol)
94
- @storage = eval(storage_engines[storage])
95
- elsif storage
96
- @storage = storage
97
- elsif @storage.nil?
98
- # Get the storage from the superclass if there is one
99
- @storage = superclass.storage rescue nil
87
+ if storage
88
+ self._storage = storage.is_a?(Symbol) ? eval(storage_engines[storage]) : storage
100
89
  end
101
- return @storage
90
+ _storage
102
91
  end
103
92
  alias_method :storage=, :storage
104
93
 
105
-
106
94
  def add_config(name)
107
95
  class_eval <<-RUBY, __FILE__, __LINE__ + 1
108
96
  def self.#{name}(value=nil)
@@ -126,6 +114,46 @@ module CarrierWave
126
114
  def configure
127
115
  yield self
128
116
  end
117
+
118
+ ##
119
+ # sets configuration back to default
120
+ #
121
+ def reset_config
122
+ configure do |config|
123
+ config.permissions = 0644
124
+ config.storage_engines = {
125
+ :file => "CarrierWave::Storage::File",
126
+ :fog => "CarrierWave::Storage::Fog",
127
+ :s3 => "CarrierWave::Storage::S3",
128
+ :grid_fs => "CarrierWave::Storage::GridFS",
129
+ :right_s3 => "CarrierWave::Storage::RightS3",
130
+ :cloud_files => "CarrierWave::Storage::CloudFiles"
131
+ }
132
+ config.storage = :file
133
+ config.s3_headers = {}
134
+ config.s3_access_policy = :public_read
135
+ config.s3_region = 'us-east-1'
136
+ config.s3_authentication_timeout = 600
137
+ config.grid_fs_database = 'carrierwave'
138
+ config.grid_fs_host = 'localhost'
139
+ config.grid_fs_port = 27017
140
+ config.fog_attributes = {}
141
+ config.fog_credentials = {}
142
+ config.fog_public = true
143
+ config.fog_authenticated_url_expiration = 600
144
+ config.store_dir = 'uploads'
145
+ config.cache_dir = 'uploads/tmp'
146
+ config.delete_tmp_file_after_storage = true
147
+ config.remove_previously_stored_files_after_update = true
148
+ config.ignore_integrity_errors = true
149
+ config.ignore_processing_errors = true
150
+ config.validate_integrity = true
151
+ config.validate_processing = true
152
+ config.root = CarrierWave.root
153
+ config.enable_processing = true
154
+ config.ensure_multipart_form = true
155
+ end
156
+ end
129
157
  end
130
158
 
131
159
  end