carrierwave-rails3 0.4.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. data/README.rdoc +527 -0
  2. data/lib/carrierwave.rb +103 -0
  3. data/lib/carrierwave/compatibility/paperclip.rb +95 -0
  4. data/lib/carrierwave/core_ext/file.rb +11 -0
  5. data/lib/carrierwave/mount.rb +359 -0
  6. data/lib/carrierwave/orm/activerecord.rb +75 -0
  7. data/lib/carrierwave/orm/datamapper.rb +27 -0
  8. data/lib/carrierwave/orm/mongoid.rb +23 -0
  9. data/lib/carrierwave/orm/mongomapper.rb +27 -0
  10. data/lib/carrierwave/orm/sequel.rb +45 -0
  11. data/lib/carrierwave/processing/image_science.rb +116 -0
  12. data/lib/carrierwave/processing/mini_magick.rb +261 -0
  13. data/lib/carrierwave/processing/rmagick.rb +278 -0
  14. data/lib/carrierwave/sanitized_file.rb +273 -0
  15. data/lib/carrierwave/storage/abstract.rb +30 -0
  16. data/lib/carrierwave/storage/cloud_files.rb +169 -0
  17. data/lib/carrierwave/storage/file.rb +48 -0
  18. data/lib/carrierwave/storage/grid_fs.rb +104 -0
  19. data/lib/carrierwave/storage/right_s3.rb +3 -0
  20. data/lib/carrierwave/storage/s3.rb +206 -0
  21. data/lib/carrierwave/test/matchers.rb +164 -0
  22. data/lib/carrierwave/uploader.rb +44 -0
  23. data/lib/carrierwave/uploader/cache.rb +146 -0
  24. data/lib/carrierwave/uploader/callbacks.rb +41 -0
  25. data/lib/carrierwave/uploader/configuration.rb +134 -0
  26. data/lib/carrierwave/uploader/default_url.rb +19 -0
  27. data/lib/carrierwave/uploader/download.rb +60 -0
  28. data/lib/carrierwave/uploader/extension_whitelist.rb +38 -0
  29. data/lib/carrierwave/uploader/mountable.rb +39 -0
  30. data/lib/carrierwave/uploader/processing.rb +84 -0
  31. data/lib/carrierwave/uploader/proxy.rb +62 -0
  32. data/lib/carrierwave/uploader/remove.rb +23 -0
  33. data/lib/carrierwave/uploader/store.rb +90 -0
  34. data/lib/carrierwave/uploader/url.rb +33 -0
  35. data/lib/carrierwave/uploader/versions.rb +147 -0
  36. data/lib/generators/templates/uploader.rb +47 -0
  37. data/lib/generators/uploader_generator.rb +13 -0
  38. data/spec/compatibility/paperclip_spec.rb +52 -0
  39. data/spec/mount_spec.rb +538 -0
  40. data/spec/orm/activerecord_spec.rb +271 -0
  41. data/spec/orm/datamapper_spec.rb +168 -0
  42. data/spec/orm/mongoid_spec.rb +202 -0
  43. data/spec/orm/mongomapper_spec.rb +202 -0
  44. data/spec/orm/sequel_spec.rb +183 -0
  45. data/spec/processing/image_science_spec.rb +56 -0
  46. data/spec/processing/mini_magick_spec.rb +76 -0
  47. data/spec/processing/rmagick_spec.rb +75 -0
  48. data/spec/sanitized_file_spec.rb +623 -0
  49. data/spec/spec_helper.rb +92 -0
  50. data/spec/storage/cloudfiles_spec.rb +78 -0
  51. data/spec/storage/grid_fs_spec.rb +86 -0
  52. data/spec/storage/s3_spec.rb +118 -0
  53. data/spec/uploader/cache_spec.rb +209 -0
  54. data/spec/uploader/callback_spec.rb +24 -0
  55. data/spec/uploader/configuration_spec.rb +105 -0
  56. data/spec/uploader/default_url_spec.rb +85 -0
  57. data/spec/uploader/download_spec.rb +75 -0
  58. data/spec/uploader/extension_whitelist_spec.rb +44 -0
  59. data/spec/uploader/mountable_spec.rb +33 -0
  60. data/spec/uploader/paths_spec.rb +22 -0
  61. data/spec/uploader/processing_spec.rb +73 -0
  62. data/spec/uploader/proxy_spec.rb +54 -0
  63. data/spec/uploader/remove_spec.rb +70 -0
  64. data/spec/uploader/store_spec.rb +264 -0
  65. data/spec/uploader/url_spec.rb +102 -0
  66. data/spec/uploader/versions_spec.rb +298 -0
  67. metadata +128 -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,60 @@
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
+ @file ||= StringIO.new(Net::HTTP.get_response(@uri).body)
35
+ end
36
+
37
+ def method_missing(*args, &block)
38
+ file.send(*args, &block)
39
+ end
40
+ end
41
+
42
+ ##
43
+ # Caches the file by downloading it from the given URL.
44
+ #
45
+ # === Parameters
46
+ #
47
+ # [url (String)] The URL where the remote file is stored
48
+ #
49
+ def download!(uri)
50
+ unless uri.blank?
51
+ file = RemoteFile.new(uri)
52
+ raise CarrierWave::DownloadError, "trying to download a file which is not served over HTTP" unless file.http?
53
+ cache!(file)
54
+ end
55
+ end
56
+
57
+ end # Download
58
+ end # Uploader
59
+ end # CarrierWave
60
+
@@ -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,84 @@
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
+ end
13
+
14
+ module ClassMethods
15
+
16
+ ##
17
+ # Lists processor callbacks declared
18
+ #
19
+ # === Returns
20
+ #
21
+ # [Array[Array[Symbol, Array]]] a list of processor callbacks which have been declared for this uploader
22
+ #
23
+ def processors
24
+ @processors ||= []
25
+ end
26
+
27
+ ##
28
+ # Adds a processor callback which applies operations as a file is uploaded.
29
+ # The argument may be the name of any method of the uploader, expressed as a symbol,
30
+ # or a list of such methods, or a hash where the key is a method and the value is
31
+ # an array of arguments to call the method with
32
+ #
33
+ # === Parameters
34
+ #
35
+ # args (*Symbol, Hash{Symbol => Array[]})
36
+ #
37
+ # === Examples
38
+ #
39
+ # class MyUploader < CarrierWave::Uploader::Base
40
+ #
41
+ # process :sepiatone, :vignette
42
+ # process :scale => [200, 200]
43
+ #
44
+ # def sepiatone
45
+ # ...
46
+ # end
47
+ #
48
+ # def vignette
49
+ # ...
50
+ # end
51
+ #
52
+ # def scale(height, width)
53
+ # ...
54
+ # end
55
+ # end
56
+ #
57
+ def process(*args)
58
+ args.each do |arg|
59
+ if arg.is_a?(Hash)
60
+ arg.each do |method, args|
61
+ processors.push([method, args])
62
+ end
63
+ else
64
+ processors.push([arg, []])
65
+ end
66
+ end
67
+ end
68
+
69
+ end # ClassMethods
70
+
71
+ ##
72
+ # Apply all process callbacks added through CarrierWave.process
73
+ #
74
+ def process!(new_file=nil)
75
+ if enable_processing
76
+ self.class.processors.each do |method, args|
77
+ self.send(method, *args)
78
+ end
79
+ end
80
+ end
81
+
82
+ end # Processing
83
+ end # Uploader
84
+ 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