locomotive_carrierwave 0.5.0.1.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. data/README.rdoc +532 -0
  2. data/lib/carrierwave/compatibility/paperclip.rb +95 -0
  3. data/lib/carrierwave/locale/en.yml +5 -0
  4. data/lib/carrierwave/mount.rb +376 -0
  5. data/lib/carrierwave/orm/activerecord.rb +36 -0
  6. data/lib/carrierwave/orm/datamapper.rb +37 -0
  7. data/lib/carrierwave/orm/mongoid.rb +36 -0
  8. data/lib/carrierwave/orm/sequel.rb +45 -0
  9. data/lib/carrierwave/processing/image_science.rb +116 -0
  10. data/lib/carrierwave/processing/mini_magick.rb +261 -0
  11. data/lib/carrierwave/processing/rmagick.rb +278 -0
  12. data/lib/carrierwave/sanitized_file.rb +306 -0
  13. data/lib/carrierwave/storage/abstract.rb +33 -0
  14. data/lib/carrierwave/storage/cloud_files.rb +168 -0
  15. data/lib/carrierwave/storage/file.rb +54 -0
  16. data/lib/carrierwave/storage/grid_fs.rb +136 -0
  17. data/lib/carrierwave/storage/right_s3.rb +1 -0
  18. data/lib/carrierwave/storage/s3.rb +249 -0
  19. data/lib/carrierwave/test/matchers.rb +164 -0
  20. data/lib/carrierwave/uploader/cache.rb +148 -0
  21. data/lib/carrierwave/uploader/callbacks.rb +41 -0
  22. data/lib/carrierwave/uploader/configuration.rb +134 -0
  23. data/lib/carrierwave/uploader/default_url.rb +19 -0
  24. data/lib/carrierwave/uploader/download.rb +64 -0
  25. data/lib/carrierwave/uploader/extension_whitelist.rb +38 -0
  26. data/lib/carrierwave/uploader/mountable.rb +39 -0
  27. data/lib/carrierwave/uploader/processing.rb +85 -0
  28. data/lib/carrierwave/uploader/proxy.rb +62 -0
  29. data/lib/carrierwave/uploader/remove.rb +23 -0
  30. data/lib/carrierwave/uploader/rename.rb +62 -0
  31. data/lib/carrierwave/uploader/store.rb +98 -0
  32. data/lib/carrierwave/uploader/url.rb +33 -0
  33. data/lib/carrierwave/uploader/versions.rb +157 -0
  34. data/lib/carrierwave/uploader.rb +45 -0
  35. data/lib/carrierwave/validations/active_model.rb +79 -0
  36. data/lib/carrierwave/version.rb +3 -0
  37. data/lib/carrierwave.rb +101 -0
  38. data/lib/generators/templates/uploader.rb +47 -0
  39. data/lib/generators/uploader_generator.rb +7 -0
  40. metadata +390 -0
@@ -0,0 +1,306 @@
1
+ # encoding: utf-8
2
+
3
+ require 'pathname'
4
+
5
+ module CarrierWave
6
+
7
+ ##
8
+ # SanitizedFile is a base class which provides a common API around all
9
+ # the different quirky Ruby File libraries. It has support for Tempfile,
10
+ # File, StringIO, Merb-style upload Hashes, as well as paths given as
11
+ # Strings and Pathnames.
12
+ #
13
+ # It's probably needlessly comprehensive and complex. Help is appreciated.
14
+ #
15
+ class SanitizedFile
16
+
17
+ attr_accessor :file
18
+
19
+ def initialize(file)
20
+ self.file = file
21
+ end
22
+
23
+ ##
24
+ # Returns the filename as is, without sanizting it.
25
+ #
26
+ # === Returns
27
+ #
28
+ # [String] the unsanitized filename
29
+ #
30
+ def original_filename
31
+ return @original_filename if @original_filename
32
+ if @file and @file.respond_to?(:original_filename)
33
+ @file.original_filename
34
+ elsif path
35
+ File.basename(path)
36
+ end
37
+ end
38
+
39
+ ##
40
+ # Returns the filename, sanitized to strip out any evil characters.
41
+ #
42
+ # === Returns
43
+ #
44
+ # [String] the sanitized filename
45
+ #
46
+ def filename
47
+ sanitize(original_filename) if original_filename
48
+ end
49
+
50
+ alias_method :identifier, :filename
51
+
52
+ ##
53
+ # Returns the part of the filename before the extension. So if a file is called 'test.jpeg'
54
+ # this would return 'test'
55
+ #
56
+ # === Returns
57
+ #
58
+ # [String] the first part of the filename
59
+ #
60
+ def basename
61
+ split_extension(filename)[0] if filename
62
+ end
63
+
64
+ ##
65
+ # Returns the file extension
66
+ #
67
+ # === Returns
68
+ #
69
+ # [String] the extension
70
+ #
71
+ def extension
72
+ split_extension(filename)[1] if filename
73
+ end
74
+
75
+ ##
76
+ # Returns the file's size.
77
+ #
78
+ # === Returns
79
+ #
80
+ # [Integer] the file's size in bytes.
81
+ #
82
+ def size
83
+ if is_path?
84
+ exists? ? File.size(path) : 0
85
+ elsif @file.respond_to?(:size)
86
+ @file.size
87
+ elsif path
88
+ exists? ? File.size(path) : 0
89
+ else
90
+ 0
91
+ end
92
+ end
93
+
94
+ ##
95
+ # Returns the full path to the file. If the file has no path, it will return nil.
96
+ #
97
+ # === Returns
98
+ #
99
+ # [String, nil] the path where the file is located.
100
+ #
101
+ def path
102
+ unless @file.blank?
103
+ if is_path?
104
+ File.expand_path(@file)
105
+ elsif @file.respond_to?(:path) and not @file.path.blank?
106
+ File.expand_path(@file.path)
107
+ end
108
+ end
109
+ end
110
+
111
+ ##
112
+ # === Returns
113
+ #
114
+ # [Boolean] whether the file is supplied as a pathname or string.
115
+ #
116
+ def is_path?
117
+ !!((@file.is_a?(String) || @file.is_a?(Pathname)) && !@file.blank?)
118
+ end
119
+
120
+ ##
121
+ # === Returns
122
+ #
123
+ # [Boolean] whether the file is valid and has a non-zero size
124
+ #
125
+ def empty?
126
+ @file.nil? || self.size.nil? || self.size.zero?
127
+ end
128
+
129
+ alias_method :blank?, :empty?
130
+
131
+ ##
132
+ # === Returns
133
+ #
134
+ # [Boolean] Whether the file exists
135
+ #
136
+ def exists?
137
+ return File.exists?(self.path) if self.path
138
+ return false
139
+ end
140
+
141
+ ##
142
+ # Returns the contents of the file.
143
+ #
144
+ # === Returns
145
+ #
146
+ # [String] contents of the file
147
+ #
148
+ def read
149
+ if is_path?
150
+ File.open(@file, "rb").read
151
+ else
152
+ @file.rewind if @file.respond_to?(:rewind)
153
+ @file.read
154
+ end
155
+ end
156
+
157
+ ##
158
+ # Moves the file to the given path
159
+ #
160
+ # === Parameters
161
+ #
162
+ # [new_path (String)] The path where the file should be moved.
163
+ # [permissions (Integer)] permissions to set on the file in its new location.
164
+ #
165
+ def move_to(new_path, permissions=nil)
166
+ return if self.empty?
167
+ new_path = File.expand_path(new_path)
168
+
169
+ mkdir!(new_path)
170
+ if exists?
171
+ FileUtils.mv(path, new_path) unless new_path == path
172
+ else
173
+ File.open(new_path, "wb") { |f| f.write(read) }
174
+ end
175
+ chmod!(new_path, permissions)
176
+ self.file = new_path
177
+ end
178
+
179
+ ##
180
+ # Creates a copy of this file and moves it to the given path. Returns the copy.
181
+ #
182
+ # === Parameters
183
+ #
184
+ # [new_path (String)] The path where the file should be copied to.
185
+ # [permissions (Integer)] permissions to set on the copy
186
+ #
187
+ # === Returns
188
+ #
189
+ # @return [CarrierWave::SanitizedFile] the location where the file will be stored.
190
+ #
191
+ def copy_to(new_path, permissions=nil)
192
+ return if self.empty?
193
+ new_path = File.expand_path(new_path)
194
+
195
+ mkdir!(new_path)
196
+ if exists?
197
+ FileUtils.cp(path, new_path) unless new_path == path
198
+ else
199
+ File.open(new_path, "wb") { |f| f.write(read) }
200
+ end
201
+ chmod!(new_path, permissions)
202
+ self.class.new({:tempfile => new_path, :content_type => content_type})
203
+ end
204
+
205
+ # ##
206
+ # # Rename this move. Returns the a copy of the file
207
+ # #
208
+ # # === Parameters
209
+ # #
210
+ # # [new_path (String)] The path where the file should be copied to.
211
+ # # [permissions (Integer)] permissions to set on the copy
212
+ # #
213
+ # # === Returns
214
+ # #
215
+ # # @return [CarrierWave::SanitizedFile] the location where the file will be stored.
216
+ # #
217
+ # def rename(new_path, permissions=nil)
218
+ # f = self.copy_to(new_path, permissions=nil)
219
+ # self.delete
220
+ # f
221
+ # end
222
+
223
+ ##
224
+ # Removes the file from the filesystem.
225
+ #
226
+ def delete
227
+ FileUtils.rm(self.path) if exists?
228
+ end
229
+
230
+ # ##
231
+ # # Rename the file in the filesystem
232
+ # #
233
+ # def rename(new_path, permissions=nil)
234
+ # return if self.empty?
235
+ # new_path = File.expand_path(new_path)
236
+ #
237
+ # mkdir!(new_path)
238
+ # if exists?
239
+ # FileUtils.mv(path, new_path) unless new_path == path
240
+ # end
241
+ # chmod!(new_path, permissions)
242
+ # self.file = new_path
243
+ # end
244
+
245
+ ##
246
+ # Returns the content type of the file.
247
+ #
248
+ # === Returns
249
+ #
250
+ # [String] the content type of the file
251
+ #
252
+ def content_type
253
+ return @content_type if @content_type
254
+ @file.content_type.chomp if @file.respond_to?(:content_type) and @file.content_type
255
+ end
256
+
257
+ private
258
+
259
+ def file=(file)
260
+ if file.is_a?(Hash)
261
+ @file = file["tempfile"] || file[:tempfile]
262
+ @original_filename = file["filename"] || file[:filename]
263
+ @content_type = file["content_type"] || file[:content_type]
264
+ else
265
+ @file = file
266
+ @original_filename = nil
267
+ @content_type = nil
268
+ end
269
+ end
270
+
271
+ # create the directory if it doesn't exist
272
+ def mkdir!(path)
273
+ FileUtils.mkdir_p(File.dirname(path)) unless File.exists?(File.dirname(path))
274
+ end
275
+
276
+ def chmod!(path, permissions)
277
+ File.chmod(permissions, path) if permissions
278
+ end
279
+
280
+ # Sanitize the filename, to prevent hacking
281
+ def sanitize(name)
282
+ name = name.gsub("\\", "/") # work-around for IE
283
+ name = File.basename(name)
284
+ name = name.gsub(/[^a-zA-Z0-9\.\-\+_]/,"_")
285
+ name = "_#{name}" if name =~ /\A\.+\z/
286
+ name = "unnamed" if name.size == 0
287
+ return name.downcase
288
+ end
289
+
290
+ def split_extension(filename)
291
+ # regular expressions to try for identifying extensions
292
+ extension_matchers = [
293
+ /\A(.+)\.(tar\.gz)\z/, # matches "something.tar.gz"
294
+ /\A(.+)\.([^\.]+)\z/ # matches "something.jpg"
295
+ ]
296
+
297
+ extension_matchers.each do |regexp|
298
+ if filename =~ regexp
299
+ return $1, $2
300
+ end
301
+ end
302
+ return filename, "" # In case we weren't able to split the extension
303
+ end
304
+
305
+ end # SanitizedFile
306
+ end # CarrierWave
@@ -0,0 +1,33 @@
1
+ # encoding: utf-8
2
+
3
+ module CarrierWave
4
+ module Storage
5
+
6
+ ##
7
+ # This file serves mostly as a specification for Storage engines. There is no requirement
8
+ # that storage engines must be a subclass of this class.
9
+ #
10
+ class Abstract
11
+
12
+ attr_reader :uploader
13
+
14
+ def initialize(uploader)
15
+ @uploader = uploader
16
+ end
17
+
18
+ def identifier
19
+ uploader.filename
20
+ end
21
+
22
+ def store!(file)
23
+ end
24
+
25
+ def retrieve!(identifier)
26
+ end
27
+
28
+ def rename!(file)
29
+ end
30
+
31
+ end # Abstract
32
+ end # Storage
33
+ end # CarrierWave
@@ -0,0 +1,168 @@
1
+ # encoding: utf-8
2
+ require 'cloudfiles'
3
+
4
+ module CarrierWave
5
+ module Storage
6
+
7
+ ##
8
+ # Uploads things to Rackspace Cloud Files webservices using the Rackspace libraries (cloudfiles gem).
9
+ # In order for CarrierWave to connect to Cloud Files, you'll need to specify an username, api key
10
+ # and container
11
+ #
12
+ # CarrierWave.configure do |config|
13
+ # config.cloud_files_username = "xxxxxx"
14
+ # config.cloud_files_api_key = "xxxxxx"
15
+ # config.cloud_files_container = "my_container"
16
+ # end
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.
21
+ #
22
+ # config.cloud_files_cdn_host = "c000000.cdn.rackspacecloud.com"
23
+ #
24
+ #
25
+ class CloudFiles < Abstract
26
+
27
+ class File
28
+
29
+ def initialize(uploader, base, path)
30
+ @uploader = uploader
31
+ @path = path
32
+ @base = base
33
+ end
34
+
35
+ ##
36
+ # Returns the current path/filename of the file on Cloud Files.
37
+ #
38
+ # === Returns
39
+ #
40
+ # [String] A path
41
+ #
42
+ def path
43
+ @path
44
+ end
45
+
46
+ ##
47
+ # Reads the contents of the file from Cloud Files
48
+ #
49
+ # === Returns
50
+ #
51
+ # [String] contents of the file
52
+ #
53
+ def read
54
+ object = cf_container.object(@path)
55
+ @content_type = object.content_type
56
+ object.data
57
+ end
58
+
59
+ ##
60
+ # Remove the file from Cloud Files
61
+ #
62
+ def delete
63
+ cf_container.delete_object(@path)
64
+ end
65
+
66
+ ##
67
+ # Returns the url on the Cloud Files CDN. Note that the parent container must be marked as
68
+ # public for this to work.
69
+ #
70
+ # === Returns
71
+ #
72
+ # [String] file's url
73
+ #
74
+ def 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
80
+ end
81
+
82
+ def content_type
83
+ cf_container.object(@path).content_type
84
+ end
85
+
86
+ def content_type=(new_content_type)
87
+ headers["content-type"] = new_content_type
88
+ end
89
+
90
+ ##
91
+ # Writes the supplied data into the object on Cloud Files.
92
+ #
93
+ # === Returns
94
+ #
95
+ # boolean
96
+ #
97
+ def store(data,headers={})
98
+ object = cf_container.create_object(@path)
99
+ object.write(data,headers)
100
+ end
101
+
102
+ private
103
+
104
+ def headers
105
+ @headers ||= { }
106
+ end
107
+
108
+ def container
109
+ @uploader.cloud_files_container
110
+ end
111
+
112
+ def connection
113
+ @base.connection
114
+ end
115
+
116
+ def cf_connection
117
+ @cf_connection ||= ::CloudFiles::Connection.new(@uploader.cloud_files_username, @uploader.cloud_files_api_key)
118
+ end
119
+
120
+ def cf_container
121
+ if @cf_container
122
+ @cf_container
123
+ else
124
+ @cf_container = cf_connection.create_container(container)
125
+ @cf_container.make_public
126
+ @cf_container
127
+ end
128
+ end
129
+
130
+
131
+ end
132
+
133
+ ##
134
+ # Store the file on Cloud Files
135
+ #
136
+ # === Parameters
137
+ #
138
+ # [file (CarrierWave::SanitizedFile)] the file to store
139
+ #
140
+ # === Returns
141
+ #
142
+ # [CarrierWave::Storage::CloudFiles::File] the stored file
143
+ #
144
+ def store!(file)
145
+ cloud_files_options = {'Content-Type' => file.content_type}
146
+ f = CarrierWave::Storage::CloudFiles::File.new(uploader, self, uploader.store_path)
147
+ f.store(file.read,cloud_files_options)
148
+ f
149
+ end
150
+
151
+ # Do something to retrieve the file
152
+ #
153
+ # @param [String] identifier uniquely identifies the file
154
+ #
155
+ # [identifier (String)] uniquely identifies the file
156
+ #
157
+ # === Returns
158
+ #
159
+ # [CarrierWave::Storage::CloudFiles::File] the stored file
160
+ #
161
+ def retrieve!(identifier)
162
+ CarrierWave::Storage::CloudFiles::File.new(uploader, self, uploader.store_path(identifier))
163
+ end
164
+
165
+
166
+ end # CloudFiles
167
+ end # Storage
168
+ end # CarrierWave
@@ -0,0 +1,54 @@
1
+ # encoding: utf-8
2
+
3
+ module CarrierWave
4
+ module Storage
5
+
6
+ ##
7
+ # File storage stores file to the Filesystem (surprising, no?). There's really not much
8
+ # to it, it uses the store_dir defined on the uploader as the storage location. That's
9
+ # pretty much it.
10
+ #
11
+ class File < Abstract
12
+
13
+ ##
14
+ # Move the file to the uploader's store path.
15
+ #
16
+ # === Parameters
17
+ #
18
+ # [file (CarrierWave::SanitizedFile)] the file to store
19
+ #
20
+ # === Returns
21
+ #
22
+ # [CarrierWave::SanitizedFile] a sanitized file
23
+ #
24
+ def store!(file)
25
+ path = ::File.expand_path(uploader.store_path, uploader.root)
26
+ file.move_to(path, uploader.permissions)
27
+ file
28
+ end
29
+
30
+ ##
31
+ # Retrieve the file from its store path
32
+ #
33
+ # === Parameters
34
+ #
35
+ # [identifier (String)] the filename of the file
36
+ #
37
+ # === Returns
38
+ #
39
+ # [CarrierWave::SanitizedFile] a sanitized file
40
+ #
41
+ def retrieve!(identifier)
42
+ path = ::File.expand_path(uploader.store_path(identifier), uploader.root)
43
+ CarrierWave::SanitizedFile.new(path)
44
+ end
45
+
46
+ def rename!(file)
47
+ path = ::File.expand_path(uploader.store_path, uploader.root)
48
+ file.move_to(path, uploader.permissions)
49
+ file
50
+ end
51
+
52
+ end # File
53
+ end # Storage
54
+ end # CarrierWave
@@ -0,0 +1,136 @@
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
+ nil
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
@@ -0,0 +1 @@
1
+ raise "The right_aws library is no longer supported. Please install the 'fog' gem instead."