jswanner-carrierwave 0.5.0.beta3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. data/README.rdoc +553 -0
  2. data/lib/carrierwave.rb +101 -0
  3. data/lib/carrierwave/compatibility/paperclip.rb +95 -0
  4. data/lib/carrierwave/mount.rb +359 -0
  5. data/lib/carrierwave/orm/activerecord.rb +79 -0
  6. data/lib/carrierwave/orm/datamapper.rb +37 -0
  7. data/lib/carrierwave/orm/mongoid.rb +23 -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 +273 -0
  13. data/lib/carrierwave/storage/abstract.rb +30 -0
  14. data/lib/carrierwave/storage/cloud_files.rb +168 -0
  15. data/lib/carrierwave/storage/file.rb +48 -0
  16. data/lib/carrierwave/storage/grid_fs.rb +108 -0
  17. data/lib/carrierwave/storage/right_s3.rb +3 -0
  18. data/lib/carrierwave/storage/s3.rb +206 -0
  19. data/lib/carrierwave/test/matchers.rb +164 -0
  20. data/lib/carrierwave/uploader.rb +44 -0
  21. data/lib/carrierwave/uploader/cache.rb +146 -0
  22. data/lib/carrierwave/uploader/callbacks.rb +41 -0
  23. data/lib/carrierwave/uploader/configuration.rb +135 -0
  24. data/lib/carrierwave/uploader/default_url.rb +19 -0
  25. data/lib/carrierwave/uploader/download.rb +75 -0
  26. data/lib/carrierwave/uploader/extension_whitelist.rb +38 -0
  27. data/lib/carrierwave/uploader/mountable.rb +39 -0
  28. data/lib/carrierwave/uploader/processing.rb +85 -0
  29. data/lib/carrierwave/uploader/proxy.rb +62 -0
  30. data/lib/carrierwave/uploader/remove.rb +23 -0
  31. data/lib/carrierwave/uploader/store.rb +90 -0
  32. data/lib/carrierwave/uploader/url.rb +33 -0
  33. data/lib/carrierwave/uploader/versions.rb +157 -0
  34. data/lib/generators/templates/uploader.rb +47 -0
  35. data/lib/generators/uploader_generator.rb +13 -0
  36. metadata +374 -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 'net/http'
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.parse(uri)
17
+ end
18
+
19
+ def original_filename
20
+ File.basename(@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
+ fetch(@uri, 10)
35
+ end
36
+
37
+ def fetch(uri, limit)
38
+ # You should choose better exception.
39
+ raise ArgumentError, 'HTTP redirect too deep' if limit == 0
40
+
41
+ response = Net::HTTP.get_response(uri)
42
+ case response
43
+ when Net::HTTPSuccess then response
44
+ when Net::HTTPRedirection then fetch(URI.parse(response['location']), limit - 1)
45
+ else
46
+ response.error!
47
+ end
48
+ @uri = uri
49
+ @file ||= StringIO.new(response.body)
50
+ end
51
+
52
+ def method_missing(*args, &block)
53
+ file.send(*args, &block)
54
+ end
55
+ end
56
+
57
+ ##
58
+ # Caches the file by downloading it from the given URL.
59
+ #
60
+ # === Parameters
61
+ #
62
+ # [url (String)] The URL where the remote file is stored
63
+ #
64
+ def download!(uri)
65
+ unless uri.blank?
66
+ file = RemoteFile.new(uri)
67
+ raise CarrierWave::DownloadError, "trying to download a file which is not served over HTTP" unless file.http?
68
+ cache!(file)
69
+ end
70
+ end
71
+
72
+ end # Download
73
+ end # Uploader
74
+ end # CarrierWave
75
+
@@ -0,0 +1,38 @@
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.
15
+ #
16
+ # === Returns
17
+ #
18
+ # [NilClass, Array[String]] a white list of extensions which are allowed to be uploaded
19
+ #
20
+ # === Examples
21
+ #
22
+ # def extension_white_list
23
+ # %w(jpg jpeg gif png)
24
+ # end
25
+ #
26
+ def extension_white_list; end
27
+
28
+ private
29
+
30
+ def check_whitelist!(new_file)
31
+ if extension_white_list and not extension_white_list.include?(new_file.extension.to_s)
32
+ raise CarrierWave::IntegrityError, "You are not allowed to upload #{new_file.extension.inspect} files, allowed types: #{extension_white_list.inspect}"
33
+ end
34
+ end
35
+
36
+ end # ExtensionWhitelist
37
+ end # Uploader
38
+ 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,85 @@
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
+ after :cache, :process!
12
+ after :recreate_versions, :process!
13
+ end
14
+
15
+ module ClassMethods
16
+
17
+ ##
18
+ # Lists processor callbacks declared
19
+ #
20
+ # === Returns
21
+ #
22
+ # [Array[Array[Symbol, Array]]] a list of processor callbacks which have been declared for this uploader
23
+ #
24
+ def processors
25
+ @processors ||= []
26
+ end
27
+
28
+ ##
29
+ # Adds a processor callback which applies operations as a file is uploaded.
30
+ # The argument may be the name of any method of the uploader, expressed as a symbol,
31
+ # or a list of such methods, or a hash where the key is a method and the value is
32
+ # an array of arguments to call the method with
33
+ #
34
+ # === Parameters
35
+ #
36
+ # args (*Symbol, Hash{Symbol => Array[]})
37
+ #
38
+ # === Examples
39
+ #
40
+ # class MyUploader < CarrierWave::Uploader::Base
41
+ #
42
+ # process :sepiatone, :vignette
43
+ # process :scale => [200, 200]
44
+ #
45
+ # def sepiatone
46
+ # ...
47
+ # end
48
+ #
49
+ # def vignette
50
+ # ...
51
+ # end
52
+ #
53
+ # def scale(height, width)
54
+ # ...
55
+ # end
56
+ # end
57
+ #
58
+ def process(*args)
59
+ args.each do |arg|
60
+ if arg.is_a?(Hash)
61
+ arg.each do |method, args|
62
+ processors.push([method, args])
63
+ end
64
+ else
65
+ processors.push([arg, []])
66
+ end
67
+ end
68
+ end
69
+
70
+ end # ClassMethods
71
+
72
+ ##
73
+ # Apply all process callbacks added through CarrierWave.process
74
+ #
75
+ def process!(new_file=nil)
76
+ if enable_processing
77
+ self.class.processors.each do |method, args|
78
+ self.send(method, *args)
79
+ end
80
+ end
81
+ end
82
+
83
+ end # Processing
84
+ end # Uploader
85
+ end # CarrierWave
@@ -0,0 +1,62 @@
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
+ end # Proxy
61
+ end # Uploader
62
+ 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,90 @@
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
57
+ if @file and @cache_id
58
+ with_callbacks(:store, new_file) do
59
+ @file = storage.store!(@file)
60
+ @cache_id = nil
61
+ end
62
+ end
63
+ end
64
+
65
+ ##
66
+ # Retrieves the file from the storage.
67
+ #
68
+ # === Parameters
69
+ #
70
+ # [identifier (String)] uniquely identifies the file to retrieve
71
+ #
72
+ def retrieve_from_store!(identifier)
73
+ with_callbacks(:retrieve_from_store, identifier) do
74
+ @file = storage.retrieve!(identifier)
75
+ end
76
+ end
77
+
78
+ private
79
+
80
+ def full_filename(for_file)
81
+ for_file
82
+ end
83
+
84
+ def storage
85
+ @storage ||= self.class.storage.new(self)
86
+ end
87
+
88
+ end # Store
89
+ end # Uploader
90
+ end # CarrierWave
@@ -0,0 +1,33 @@
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
+ # [String] A JSON serializtion containing this uploader's URL
26
+ #
27
+ def to_json(*args)
28
+ { 'url' => url }.to_json(*args)
29
+ end
30
+
31
+ end # Url
32
+ end # Uploader
33
+ end # CarrierWave