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,19 @@
1
+ # encoding: utf-8
2
+
3
+ module CarrierWave
4
+ module Uploader
5
+ module DefaultUrl
6
+
7
+ def url(*args)
8
+ super || default_url
9
+ end
10
+
11
+ ##
12
+ # Override this method in your uploader to provide a default url
13
+ # in case no file has been cached/stored yet.
14
+ #
15
+ def default_url; end
16
+
17
+ end # DefaultPath
18
+ end # Uploader
19
+ end # CarrierWave
@@ -0,0 +1,75 @@
1
+ # encoding: utf-8
2
+
3
+ require 'open-uri'
4
+
5
+ module CarrierWave
6
+ module Uploader
7
+ module Download
8
+ extend ActiveSupport::Concern
9
+
10
+ include CarrierWave::Uploader::Callbacks
11
+ include CarrierWave::Uploader::Configuration
12
+ include CarrierWave::Uploader::Cache
13
+
14
+ class RemoteFile
15
+ def initialize(uri)
16
+ @uri = uri
17
+ end
18
+
19
+ def original_filename
20
+ File.basename(file.base_uri.path)
21
+ end
22
+
23
+ def respond_to?(*args)
24
+ super or file.respond_to?(*args)
25
+ end
26
+
27
+ def http?
28
+ @uri.scheme =~ /^https?$/
29
+ end
30
+
31
+ private
32
+
33
+ def file
34
+ if @file.blank?
35
+ @file = Kernel.open(@uri.to_s)
36
+ @file = @file.is_a?(String) ? StringIO.new(@file) : @file
37
+ end
38
+ @file
39
+ end
40
+
41
+ def method_missing(*args, &block)
42
+ file.send(*args, &block)
43
+ end
44
+ end
45
+
46
+ ##
47
+ # Caches the file by downloading it from the given URL.
48
+ #
49
+ # === Parameters
50
+ #
51
+ # [url (String)] The URL where the remote file is stored
52
+ #
53
+ def download!(uri)
54
+ unless uri.blank?
55
+ processed_uri = process_uri(uri)
56
+ file = RemoteFile.new(processed_uri)
57
+ raise CarrierWave::DownloadError, "trying to download a file which is not served over HTTP" unless file.http?
58
+ cache!(file)
59
+ end
60
+ end
61
+
62
+ ##
63
+ # Processes the given URL by parsing and escaping it. Public to allow overriding.
64
+ #
65
+ # === Parameters
66
+ #
67
+ # [url (String)] The URL where the remote file is stored
68
+ #
69
+ def process_uri(uri)
70
+ URI.parse(URI.escape(URI.unescape(uri)))
71
+ end
72
+
73
+ end # Download
74
+ end # Uploader
75
+ end # CarrierWave
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ module CarrierWave
4
+ module Uploader
5
+ module ExtensionWhitelist
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ before :cache, :check_whitelist!
10
+ end
11
+
12
+ ##
13
+ # Override this method in your uploader to provide a white list of extensions which
14
+ # are allowed to be uploaded. Compares the file's extension case insensitive.
15
+ # Furthermore, not only strings but Regexp are allowed as well.
16
+ #
17
+ # When using a Regexp in the white list, `\A` and `\z` are automatically added to
18
+ # the Regexp expression, also case insensitive.
19
+ #
20
+ # === Returns
21
+ #
22
+ # [NilClass, Array[String,Regexp]] a white list of extensions which are allowed to be uploaded
23
+ #
24
+ # === Examples
25
+ #
26
+ # def extension_white_list
27
+ # %w(jpg jpeg gif png)
28
+ # end
29
+ #
30
+ # Basically the same, but using a Regexp:
31
+ #
32
+ # def extension_white_list
33
+ # [/jpe?g/, 'gif', 'png']
34
+ # end
35
+ #
36
+ def extension_white_list; end
37
+
38
+ private
39
+
40
+ def check_whitelist!(new_file)
41
+ extension = new_file.extension.to_s
42
+ if extension_white_list and not extension_white_list.detect { |item| extension =~ /\A#{item}\z/i }
43
+ raise CarrierWave::IntegrityError, "You are not allowed to upload #{new_file.extension.inspect} files, allowed types: #{extension_white_list.inspect}"
44
+ end
45
+ end
46
+
47
+ end # ExtensionWhitelist
48
+ end # Uploader
49
+ end # CarrierWave
@@ -0,0 +1,39 @@
1
+ # encoding: utf-8
2
+
3
+ module CarrierWave
4
+ module Uploader
5
+ module Mountable
6
+
7
+ attr_reader :model, :mounted_as
8
+
9
+ ##
10
+ # If a model is given as the first parameter, it will stored in the uploader, and
11
+ # available throught +#model+. Likewise, mounted_as stores the name of the column
12
+ # where this instance of the uploader is mounted. These values can then be used inside
13
+ # your uploader.
14
+ #
15
+ # If you do not wish to mount your uploaders with the ORM extensions in -more then you
16
+ # can override this method inside your uploader. Just be sure to call +super+
17
+ #
18
+ # === Parameters
19
+ #
20
+ # [model (Object)] Any kind of model object
21
+ # [mounted_as (Symbol)] The name of the column where this uploader is mounted
22
+ #
23
+ # === Examples
24
+ #
25
+ # class MyUploader < CarrierWave::Uploader::Base
26
+ #
27
+ # def store_dir
28
+ # File.join('public', 'files', mounted_as, model.permalink)
29
+ # end
30
+ # end
31
+ #
32
+ def initialize(model=nil, mounted_as=nil)
33
+ @model = model
34
+ @mounted_as = mounted_as
35
+ end
36
+
37
+ end # Mountable
38
+ end # Uploader
39
+ end # CarrierWave
@@ -0,0 +1,90 @@
1
+ # encoding: utf-8
2
+
3
+ module CarrierWave
4
+ module Uploader
5
+ module Processing
6
+ extend ActiveSupport::Concern
7
+
8
+ include CarrierWave::Uploader::Callbacks
9
+
10
+ included do
11
+ class_attribute :processors, :instance_writer => false
12
+ self.processors = []
13
+
14
+ after :cache, :process!
15
+ end
16
+
17
+ module ClassMethods
18
+
19
+ ##
20
+ # Adds a processor callback which applies operations as a file is uploaded.
21
+ # The argument may be the name of any method of the uploader, expressed as a symbol,
22
+ # or a list of such methods, or a hash where the key is a method and the value is
23
+ # an array of arguments to call the method with
24
+ #
25
+ # === Parameters
26
+ #
27
+ # args (*Symbol, Hash{Symbol => Array[]})
28
+ #
29
+ # === Examples
30
+ #
31
+ # class MyUploader < CarrierWave::Uploader::Base
32
+ #
33
+ # process :sepiatone, :vignette
34
+ # process :scale => [200, 200]
35
+ # process :scale => [200, 200], :if => :image?
36
+ # process :sepiatone, :if => :image?
37
+ #
38
+ # def sepiatone
39
+ # ...
40
+ # end
41
+ #
42
+ # def vignette
43
+ # ...
44
+ # end
45
+ #
46
+ # def scale(height, width)
47
+ # ...
48
+ # end
49
+ #
50
+ # def image?
51
+ # ...
52
+ # end
53
+ #
54
+ # end
55
+ #
56
+ def process(*args)
57
+ if !args.first.is_a?(Hash) && args.last.is_a?(Hash)
58
+ conditions = args.pop
59
+ args.map!{ |arg| {arg => []}.merge(conditions) }
60
+ end
61
+
62
+ args.each do |arg|
63
+ if arg.is_a?(Hash)
64
+ condition = arg.delete(:if)
65
+ arg.each do |method, args|
66
+ self.processors += [[method, args, condition]]
67
+ end
68
+ else
69
+ self.processors += [[arg, [], nil]]
70
+ end
71
+ end
72
+ end
73
+
74
+ end # ClassMethods
75
+
76
+ ##
77
+ # Apply all process callbacks added through CarrierWave.process
78
+ #
79
+ def process!(new_file=nil)
80
+ if enable_processing
81
+ self.class.processors.each do |method, args, condition|
82
+ next if condition && !self.send(condition, new_file)
83
+ self.send(method, *args)
84
+ end
85
+ end
86
+ end
87
+
88
+ end # Processing
89
+ end # Uploader
90
+ end # CarrierWave
@@ -0,0 +1,77 @@
1
+ # encoding: utf-8
2
+
3
+ module CarrierWave
4
+ module Uploader
5
+ module Proxy
6
+
7
+ ##
8
+ # === Returns
9
+ #
10
+ # [Boolean] Whether the uploaded file is blank
11
+ #
12
+ def blank?
13
+ file.blank?
14
+ end
15
+
16
+ ##
17
+ # === Returns
18
+ #
19
+ # [String] the path where the file is currently located.
20
+ #
21
+ def current_path
22
+ file.path if file.respond_to?(:path)
23
+ end
24
+
25
+ alias_method :path, :current_path
26
+
27
+ ##
28
+ # Returns a string that uniquely identifies the last stored file
29
+ #
30
+ # === Returns
31
+ #
32
+ # [String] uniquely identifies a file
33
+ #
34
+ def identifier
35
+ storage.identifier if storage.respond_to?(:identifier)
36
+ end
37
+
38
+ ##
39
+ # Read the contents of the file
40
+ #
41
+ # === Returns
42
+ #
43
+ # [String] contents of the file
44
+ #
45
+ def read
46
+ file.read if file.respond_to?(:read)
47
+ end
48
+
49
+ ##
50
+ # Fetches the size of the currently stored/cached file
51
+ #
52
+ # === Returns
53
+ #
54
+ # [Integer] size of the file
55
+ #
56
+ def size
57
+ file.respond_to?(:size) ? file.size : 0
58
+ end
59
+
60
+ ##
61
+ # Return the size of the file when asked for its length
62
+ #
63
+ # === Returns
64
+ #
65
+ # [Integer] size of the file
66
+ #
67
+ # === Note
68
+ #
69
+ # This was added because of the way Rails handles length/size validations in 3.0.6 and above.
70
+ #
71
+ def length
72
+ size
73
+ end
74
+
75
+ end # Proxy
76
+ end # Uploader
77
+ end # CarrierWave
@@ -0,0 +1,23 @@
1
+ # encoding: utf-8
2
+
3
+ module CarrierWave
4
+ module Uploader
5
+ module Remove
6
+ extend ActiveSupport::Concern
7
+
8
+ include CarrierWave::Uploader::Callbacks
9
+
10
+ ##
11
+ # Removes the file and reset it
12
+ #
13
+ def remove!
14
+ with_callbacks(:remove) do
15
+ @file.delete if @file
16
+ @file = nil
17
+ @cache_id = nil
18
+ end
19
+ end
20
+
21
+ end # Remove
22
+ end # Uploader
23
+ end # CarrierWave
@@ -0,0 +1,113 @@
1
+ # encoding: utf-8
2
+
3
+ module CarrierWave
4
+ module Uploader
5
+ module Store
6
+ extend ActiveSupport::Concern
7
+
8
+ include CarrierWave::Uploader::Callbacks
9
+ include CarrierWave::Uploader::Configuration
10
+ include CarrierWave::Uploader::Cache
11
+
12
+ ##
13
+ # Override this in your Uploader to change the filename.
14
+ #
15
+ # Be careful using record ids as filenames. If the filename is stored in the database
16
+ # the record id will be nil when the filename is set. Don't use record ids unless you
17
+ # understand this limitation.
18
+ #
19
+ # Do not use the version_name in the filename, as it will prevent versions from being
20
+ # loaded correctly.
21
+ #
22
+ # === Returns
23
+ #
24
+ # [String] a filename
25
+ #
26
+ def filename
27
+ @filename
28
+ end
29
+
30
+ ##
31
+ # Calculates the path where the file should be stored. If +for_file+ is given, it will be
32
+ # used as the filename, otherwise +CarrierWave::Uploader#filename+ is assumed.
33
+ #
34
+ # === Parameters
35
+ #
36
+ # [for_file (String)] name of the file <optional>
37
+ #
38
+ # === Returns
39
+ #
40
+ # [String] the store path
41
+ #
42
+ def store_path(for_file=filename)
43
+ File.join([store_dir, full_filename(for_file)].compact)
44
+ end
45
+
46
+ ##
47
+ # Stores the file by passing it to this Uploader's storage engine.
48
+ #
49
+ # If new_file is omitted, a previously cached file will be stored.
50
+ #
51
+ # === Parameters
52
+ #
53
+ # [new_file (File, IOString, Tempfile)] any kind of file object
54
+ #
55
+ def store!(new_file=nil)
56
+ cache!(new_file) if new_file && ((@cache_id != parent_cache_id) || @cache_id.nil?)
57
+ if @file and @cache_id
58
+ with_callbacks(:store, new_file) do
59
+ new_file = storage.store!(@file)
60
+ @file.delete if delete_tmp_file_after_storage
61
+ delete_cache_id
62
+ @file = new_file
63
+ @cache_id = nil
64
+ end
65
+ end
66
+ end
67
+
68
+ ##
69
+ # Deletes a cache id (tmp dir in cache)
70
+ #
71
+ def delete_cache_id
72
+ if @cache_id
73
+ path = File.expand_path(File.join(cache_dir, @cache_id), CarrierWave.root)
74
+ begin
75
+ Dir.rmdir(path)
76
+ rescue Errno::ENOENT
77
+ # Ignore: path does not exist
78
+ rescue Errno::ENOTDIR
79
+ # Ignore: path is not a dir
80
+ rescue Errno::ENOTEMPTY, Errno::EEXIST
81
+ # Ignore: dir is not empty
82
+ rescue SystemCallError
83
+ # no such directory on JRuby
84
+ end
85
+ end
86
+ end
87
+
88
+ ##
89
+ # Retrieves the file from the storage.
90
+ #
91
+ # === Parameters
92
+ #
93
+ # [identifier (String)] uniquely identifies the file to retrieve
94
+ #
95
+ def retrieve_from_store!(identifier)
96
+ with_callbacks(:retrieve_from_store, identifier) do
97
+ @file = storage.retrieve!(identifier)
98
+ end
99
+ end
100
+
101
+ private
102
+
103
+ def full_filename(for_file)
104
+ for_file
105
+ end
106
+
107
+ def storage
108
+ @storage ||= self.class.storage.new(self)
109
+ end
110
+
111
+ end # Store
112
+ end # Uploader
113
+ end # CarrierWave