plowdawg-carrierwave 0.5.8

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 +674 -0
  2. data/lib/carrierwave.rb +109 -0
  3. data/lib/carrierwave/compatibility/paperclip.rb +95 -0
  4. data/lib/carrierwave/locale/en.yml +5 -0
  5. data/lib/carrierwave/mount.rb +382 -0
  6. data/lib/carrierwave/orm/activerecord.rb +46 -0
  7. data/lib/carrierwave/processing/mime_types.rb +58 -0
  8. data/lib/carrierwave/processing/mini_magick.rb +253 -0
  9. data/lib/carrierwave/processing/rmagick.rb +279 -0
  10. data/lib/carrierwave/sanitized_file.rb +302 -0
  11. data/lib/carrierwave/storage/abstract.rb +30 -0
  12. data/lib/carrierwave/storage/cloud_files.rb +188 -0
  13. data/lib/carrierwave/storage/file.rb +47 -0
  14. data/lib/carrierwave/storage/fog.rb +332 -0
  15. data/lib/carrierwave/storage/right_s3.rb +1 -0
  16. data/lib/carrierwave/storage/s3.rb +240 -0
  17. data/lib/carrierwave/test/matchers.rb +164 -0
  18. data/lib/carrierwave/uploader.rb +44 -0
  19. data/lib/carrierwave/uploader/cache.rb +160 -0
  20. data/lib/carrierwave/uploader/callbacks.rb +35 -0
  21. data/lib/carrierwave/uploader/configuration.rb +162 -0
  22. data/lib/carrierwave/uploader/default_url.rb +19 -0
  23. data/lib/carrierwave/uploader/download.rb +75 -0
  24. data/lib/carrierwave/uploader/extension_whitelist.rb +49 -0
  25. data/lib/carrierwave/uploader/mountable.rb +39 -0
  26. data/lib/carrierwave/uploader/processing.rb +90 -0
  27. data/lib/carrierwave/uploader/proxy.rb +77 -0
  28. data/lib/carrierwave/uploader/remove.rb +23 -0
  29. data/lib/carrierwave/uploader/store.rb +113 -0
  30. data/lib/carrierwave/uploader/url.rb +45 -0
  31. data/lib/carrierwave/uploader/versions.rb +237 -0
  32. data/lib/carrierwave/validations/active_model.rb +79 -0
  33. data/lib/carrierwave/version.rb +3 -0
  34. data/lib/generators/templates/uploader.rb +49 -0
  35. data/lib/generators/uploader_generator.rb +7 -0
  36. metadata +215 -0
@@ -0,0 +1,45 @@
1
+ # encoding: utf-8
2
+
3
+ module CarrierWave
4
+ module Uploader
5
+ module Url
6
+
7
+ ##
8
+ # === Returns
9
+ #
10
+ # [String] the location where this file is accessible via a url
11
+ #
12
+ def url
13
+ if file.respond_to?(:url) and not file.url.blank?
14
+ file.url
15
+ elsif current_path
16
+ File.expand_path(current_path).gsub(File.expand_path(root), '')
17
+ end
18
+ end
19
+
20
+ alias_method :to_s, :url
21
+
22
+ ##
23
+ # === Returns
24
+ #
25
+ # [Hash] the locations where this file and versions are accessible via a url
26
+ #
27
+ def as_json(options = nil)
28
+ h = { :url => url }
29
+ h.merge Hash[versions.map { |name, version| [name, { :url => version.url }] }]
30
+ end
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
+
43
+ end # Url
44
+ end # Uploader
45
+ end # CarrierWave
@@ -0,0 +1,237 @@
1
+ # encoding: utf-8
2
+
3
+ require 'active_support/deprecation'
4
+
5
+ module CarrierWave
6
+ module Uploader
7
+ module Versions
8
+ extend ActiveSupport::Concern
9
+
10
+ include CarrierWave::Uploader::Callbacks
11
+
12
+ included do
13
+ ##
14
+ # Add configuration options for versions
15
+ # class_inheritable_accessor was deprecated in Rails 3.1 and removed for 3.2.
16
+ # class_attribute was added in 3.0, but doesn't support omitting the instance_reader until 3.0.10
17
+ # For max compatibility, always use class_inheritable_accessor when possible
18
+ if respond_to?(:class_inheritable_accessor)
19
+ ActiveSupport::Deprecation.silence do
20
+ class_inheritable_accessor :versions, :version_names, :instance_reader => false, :instance_writer => false
21
+ end
22
+ else
23
+ class_attribute :versions, :version_names, :instance_reader => false, :instance_writer => false
24
+ end
25
+
26
+ self.versions = {}
27
+ self.version_names = []
28
+
29
+ attr_accessor :parent_cache_id
30
+
31
+ after :cache, :assign_parent_cache_id
32
+ after :cache, :cache_versions!
33
+ after :store, :store_versions!
34
+ after :remove, :remove_versions!
35
+ after :retrieve_from_cache, :retrieve_versions_from_cache!
36
+ after :retrieve_from_store, :retrieve_versions_from_store!
37
+ end
38
+
39
+ module ClassMethods
40
+
41
+ ##
42
+ # Adds a new version to this uploader
43
+ #
44
+ # === Parameters
45
+ #
46
+ # [name (#to_sym)] name of the version
47
+ # [options (Hash)] optional options hash
48
+ # [&block (Proc)] a block to eval on this version of the uploader
49
+ #
50
+ # === Examples
51
+ #
52
+ # class MyUploader < CarrierWave::Uploader::Base
53
+ #
54
+ # version :thumb do
55
+ # process :scale => [200, 200]
56
+ # end
57
+ #
58
+ # version :preview, :if => :image? do
59
+ # process :scale => [200, 200]
60
+ # end
61
+ #
62
+ # end
63
+ #
64
+ def version(name, options = {}, &block)
65
+ name = name.to_sym
66
+ unless versions[name]
67
+ uploader = Class.new(self)
68
+ uploader.versions = {}
69
+
70
+ # Define the enable_processing method for versions so they get the
71
+ # value from the parent class unless explicitly overwritten
72
+ uploader.class_eval <<-RUBY, __FILE__, __LINE__ + 1
73
+ def self.enable_processing(value=nil)
74
+ self.enable_processing = value if value
75
+ if !@enable_processing.nil?
76
+ @enable_processing
77
+ else
78
+ superclass.enable_processing
79
+ end
80
+ end
81
+ RUBY
82
+
83
+ # Add the current version hash to class attribute :versions
84
+ current_version = {}
85
+ current_version[name] = {
86
+ :uploader => uploader,
87
+ :options => options
88
+ }
89
+ self.versions = versions.merge(current_version)
90
+
91
+ versions[name][:uploader].version_names += [name]
92
+
93
+ class_eval <<-RUBY
94
+ def #{name}
95
+ versions[:#{name}]
96
+ end
97
+ RUBY
98
+ # as the processors get the output from the previous processors as their
99
+ # input we must not stack the processors here
100
+ versions[name][:uploader].processors = versions[name][:uploader].processors.dup
101
+ versions[name][:uploader].processors.clear
102
+ end
103
+ versions[name][:uploader].class_eval(&block) if block
104
+ versions[name]
105
+ end
106
+
107
+ def recursively_apply_block_to_versions(&block)
108
+ versions.each do |name, version|
109
+ version[:uploader].class_eval(&block)
110
+ version[:uploader].recursively_apply_block_to_versions(&block)
111
+ end
112
+ end
113
+ end # ClassMethods
114
+
115
+ ##
116
+ # Returns a hash mapping the name of each version of the uploader to an instance of it
117
+ #
118
+ # === Returns
119
+ #
120
+ # [Hash{Symbol => CarrierWave::Uploader}] a list of uploader instances
121
+ #
122
+ def versions
123
+ return @versions if @versions
124
+ @versions = {}
125
+ self.class.versions.each do |name, version|
126
+ @versions[name] = version[:uploader].new(model, mounted_as)
127
+ end
128
+ @versions
129
+ end
130
+
131
+ ##
132
+ # === Returns
133
+ #
134
+ # [String] the name of this version of the uploader
135
+ #
136
+ def version_name
137
+ self.class.version_names.join('_').to_sym unless self.class.version_names.blank?
138
+ end
139
+
140
+ ##
141
+ # When given a version name as a parameter, will return the url for that version
142
+ # This also works with nested versions.
143
+ #
144
+ # === Example
145
+ #
146
+ # my_uploader.url # => /path/to/my/uploader.gif
147
+ # my_uploader.url(:thumb) # => /path/to/my/thumb_uploader.gif
148
+ # my_uploader.url(:thumb, :small) # => /path/to/my/thumb_small_uploader.gif
149
+ #
150
+ # === Parameters
151
+ #
152
+ # [*args (Symbol)] any number of versions
153
+ #
154
+ # === Returns
155
+ #
156
+ # [String] the location where this file is accessible via a url
157
+ #
158
+ def url(*args)
159
+ if(args.first)
160
+ raise ArgumentError, "Version #{args.first} doesn't exist!" if versions[args.first.to_sym].nil?
161
+ # recursively proxy to version
162
+ versions[args.first.to_sym].url(*args[1..-1])
163
+ else
164
+ super()
165
+ end
166
+ end
167
+
168
+ ##
169
+ # Recreate versions and reprocess them. This can be used to recreate
170
+ # versions if their parameters somehow have changed.
171
+ #
172
+ def recreate_versions!
173
+ # Some files could possibly not be stored on the local disk. This
174
+ # doesn't play nicely with processing. Make sure that we're only
175
+ # processing a cached file
176
+ #
177
+ # The call to store! will trigger the necessary callbacks to both
178
+ # process this version and all sub-versions
179
+ cache_stored_file! if !cached?
180
+
181
+ store!
182
+ end
183
+
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
190
+
191
+ def active_versions
192
+ versions.select do |name, uploader|
193
+ condition = self.class.versions[name][:options][:if]
194
+ not condition or send(condition, file)
195
+ end
196
+ end
197
+
198
+ def full_filename(for_file)
199
+ [version_name, super(for_file)].compact.join('_')
200
+ end
201
+
202
+ def full_original_filename
203
+ [version_name, super].compact.join('_')
204
+ end
205
+
206
+ def cache_versions!(new_file)
207
+ # We might have processed the new_file argument after the callbacks were
208
+ # initialized, so get the actual file based off of the current state of
209
+ # our file
210
+ processed_parent = SanitizedFile.new :tempfile => self.file,
211
+ :filename => new_file.original_filename
212
+
213
+ active_versions.each do |name, v|
214
+ v.send(:cache_id=, cache_id)
215
+ v.cache!(processed_parent)
216
+ end
217
+ end
218
+
219
+ def store_versions!(new_file)
220
+ active_versions.each { |name, v| v.store!(new_file) }
221
+ end
222
+
223
+ def remove_versions!
224
+ versions.each { |name, v| v.remove! }
225
+ end
226
+
227
+ def retrieve_versions_from_cache!(cache_name)
228
+ versions.each { |name, v| v.retrieve_from_cache!(cache_name) }
229
+ end
230
+
231
+ def retrieve_versions_from_store!(identifier)
232
+ versions.each { |name, v| v.retrieve_from_store!(identifier) }
233
+ end
234
+
235
+ end # Versions
236
+ end # Uploader
237
+ end # CarrierWave
@@ -0,0 +1,79 @@
1
+ # encoding: utf-8
2
+
3
+ require 'active_model/validator'
4
+ require 'active_support/concern'
5
+
6
+
7
+ module CarrierWave
8
+
9
+ # == Active Model Presence Validator
10
+ module Validations
11
+ module ActiveModel
12
+ extend ActiveSupport::Concern
13
+
14
+ # class ProcessingValidator < ::ActiveModel::EachValidator
15
+ #
16
+ # def validate_each(record, attribute, value)
17
+ # if record.send("#{attribute}_processing_error")
18
+ # record.errors.add(attribute, :carrierwave_processing_error)
19
+ # end
20
+ # end
21
+ # end
22
+
23
+ class IntegrityValidator < ::ActiveModel::EachValidator
24
+
25
+ def validate_each(record, attribute, value)
26
+ if record.send("#{attribute}_integrity_error")
27
+ record.errors.add(attribute, :carrierwave_integrity_error)
28
+ end
29
+ end
30
+ end
31
+
32
+ module HelperMethods
33
+
34
+ ##
35
+ # Makes the record invalid if the file couldn't be uploaded due to an integrity error
36
+ #
37
+ # Accepts the usual parameters for validations in Rails (:if, :unless, etc...)
38
+ #
39
+ # === Note
40
+ #
41
+ # Set this key in your translations file for I18n:
42
+ #
43
+ # carrierwave:
44
+ # errors:
45
+ # integrity: 'Here be an error message'
46
+ #
47
+ def validates_integrity_of(*attr_names)
48
+ validates_with IntegrityValidator, _merge_attributes(attr_names)
49
+ end
50
+
51
+ ##
52
+ # Makes the record invalid if the file couldn't be processed (assuming the process failed
53
+ # with a CarrierWave::ProcessingError)
54
+ #
55
+ # Accepts the usual parameters for validations in Rails (:if, :unless, etc...)
56
+ #
57
+ # === Note
58
+ #
59
+ # Set this key in your translations file for I18n:
60
+ #
61
+ # carrierwave:
62
+ # errors:
63
+ # processing: 'Here be an error message'
64
+ #
65
+ def validates_processing_of(*attr_names)
66
+ # validates_with ProcessingValidator, _merge_attributes(attr_names)
67
+ end
68
+ end
69
+
70
+ included do
71
+ extend HelperMethods
72
+ include HelperMethods
73
+ end
74
+ end
75
+ end
76
+ end
77
+
78
+ I18n.load_path << File.join(File.dirname(__FILE__), "..", "locale", 'en.yml')
79
+
@@ -0,0 +1,3 @@
1
+ module CarrierWave
2
+ VERSION = "0.5.8"
3
+ end
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ class <%= class_name %>Uploader < CarrierWave::Uploader::Base
4
+
5
+ # Include RMagick or ImageScience support:
6
+ # include CarrierWave::RMagick
7
+ # include CarrierWave::MiniMagick
8
+ # include CarrierWave::ImageScience
9
+
10
+ # Choose what kind of storage to use for this uploader:
11
+ storage :file
12
+ # storage :fog
13
+
14
+ # Override the directory where uploaded files will be stored.
15
+ # This is a sensible default for uploaders that are meant to be mounted:
16
+ def store_dir
17
+ "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
18
+ end
19
+
20
+ # Provide a default URL as a default if there hasn't been a file uploaded:
21
+ # def default_url
22
+ # "/images/fallback/" + [version_name, "default.png"].compact.join('_')
23
+ # end
24
+
25
+ # Process files as they are uploaded:
26
+ # process :scale => [200, 300]
27
+ #
28
+ # def scale(width, height)
29
+ # # do something
30
+ # end
31
+
32
+ # Create different versions of your uploaded files:
33
+ # version :thumb do
34
+ # process :scale => [50, 50]
35
+ # end
36
+
37
+ # Add a white list of extensions which are allowed to be uploaded.
38
+ # For images you might use something like this:
39
+ # def extension_white_list
40
+ # %w(jpg jpeg gif png)
41
+ # end
42
+
43
+ # Override the filename of the uploaded files:
44
+ # Avoid using model.id or version_name here, see uploader/store.rb for details.
45
+ # def filename
46
+ # "something.jpg" if original_filename
47
+ # end
48
+
49
+ end
@@ -0,0 +1,7 @@
1
+ class UploaderGenerator < Rails::Generators::NamedBase
2
+ source_root File.expand_path("../templates", __FILE__)
3
+
4
+ def create_uploader_file
5
+ template "uploader.rb", "app/uploaders/#{file_name}_uploader.rb"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,215 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: plowdawg-carrierwave
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.8
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jonas Nicklas
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-08-15 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: &19315860 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *19315860
25
+ - !ruby/object:Gem::Dependency
26
+ name: rails
27
+ requirement: &19315440 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '3.0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *19315440
36
+ - !ruby/object:Gem::Dependency
37
+ name: sqlite3
38
+ requirement: &19315140 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *19315140
47
+ - !ruby/object:Gem::Dependency
48
+ name: cucumber
49
+ requirement: &19314792 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *19314792
58
+ - !ruby/object:Gem::Dependency
59
+ name: json
60
+ requirement: &19314468 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *19314468
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: &19298616 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: '2.0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *19298616
80
+ - !ruby/object:Gem::Dependency
81
+ name: sham_rack
82
+ requirement: &19298352 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *19298352
91
+ - !ruby/object:Gem::Dependency
92
+ name: timecop
93
+ requirement: &19298076 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ type: :development
100
+ prerelease: false
101
+ version_requirements: *19298076
102
+ - !ruby/object:Gem::Dependency
103
+ name: cloudfiles
104
+ requirement: &19297824 !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: *19297824
113
+ - !ruby/object:Gem::Dependency
114
+ name: fog
115
+ requirement: &19297512 !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ! '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ type: :development
122
+ prerelease: false
123
+ version_requirements: *19297512
124
+ - !ruby/object:Gem::Dependency
125
+ name: mini_magick
126
+ requirement: &19297164 !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ! '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: *19297164
135
+ - !ruby/object:Gem::Dependency
136
+ name: rmagick
137
+ requirement: &19296900 !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ! '>='
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ type: :development
144
+ prerelease: false
145
+ version_requirements: *19296900
146
+ description: Upload files in your Ruby applications, map them to a range of ORMs,
147
+ store them on different backends.
148
+ email:
149
+ - jonas.nicklas@gmail.com
150
+ executables: []
151
+ extensions: []
152
+ extra_rdoc_files:
153
+ - README.md
154
+ files:
155
+ - lib/carrierwave/compatibility/paperclip.rb
156
+ - lib/carrierwave/locale/en.yml
157
+ - lib/carrierwave/mount.rb
158
+ - lib/carrierwave/orm/activerecord.rb
159
+ - lib/carrierwave/processing/mime_types.rb
160
+ - lib/carrierwave/processing/mini_magick.rb
161
+ - lib/carrierwave/processing/rmagick.rb
162
+ - lib/carrierwave/sanitized_file.rb
163
+ - lib/carrierwave/storage/abstract.rb
164
+ - lib/carrierwave/storage/cloud_files.rb
165
+ - lib/carrierwave/storage/file.rb
166
+ - lib/carrierwave/storage/fog.rb
167
+ - lib/carrierwave/storage/right_s3.rb
168
+ - lib/carrierwave/storage/s3.rb
169
+ - lib/carrierwave/test/matchers.rb
170
+ - lib/carrierwave/uploader/cache.rb
171
+ - lib/carrierwave/uploader/callbacks.rb
172
+ - lib/carrierwave/uploader/configuration.rb
173
+ - lib/carrierwave/uploader/default_url.rb
174
+ - lib/carrierwave/uploader/download.rb
175
+ - lib/carrierwave/uploader/extension_whitelist.rb
176
+ - lib/carrierwave/uploader/mountable.rb
177
+ - lib/carrierwave/uploader/processing.rb
178
+ - lib/carrierwave/uploader/proxy.rb
179
+ - lib/carrierwave/uploader/remove.rb
180
+ - lib/carrierwave/uploader/store.rb
181
+ - lib/carrierwave/uploader/url.rb
182
+ - lib/carrierwave/uploader/versions.rb
183
+ - lib/carrierwave/uploader.rb
184
+ - lib/carrierwave/validations/active_model.rb
185
+ - lib/carrierwave/version.rb
186
+ - lib/carrierwave.rb
187
+ - lib/generators/templates/uploader.rb
188
+ - lib/generators/uploader_generator.rb
189
+ - README.md
190
+ homepage: https://github.com/jnicklas/carrierwave
191
+ licenses: []
192
+ post_install_message:
193
+ rdoc_options:
194
+ - --main
195
+ require_paths:
196
+ - lib
197
+ required_ruby_version: !ruby/object:Gem::Requirement
198
+ none: false
199
+ requirements:
200
+ - - ! '>='
201
+ - !ruby/object:Gem::Version
202
+ version: '0'
203
+ required_rubygems_version: !ruby/object:Gem::Requirement
204
+ none: false
205
+ requirements:
206
+ - - ! '>='
207
+ - !ruby/object:Gem::Version
208
+ version: '0'
209
+ requirements: []
210
+ rubyforge_project: carrierwave
211
+ rubygems_version: 1.8.6
212
+ signing_key:
213
+ specification_version: 3
214
+ summary: Ruby file upload library
215
+ test_files: []