thorsson_carrierwave 0.4.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (109) hide show
  1. data/Generators +4 -0
  2. data/History.txt +125 -0
  3. data/Manifest.txt +111 -0
  4. data/README.rdoc +528 -0
  5. data/Rakefile +39 -0
  6. data/cucumber.yml +2 -0
  7. data/features/caching.feature +28 -0
  8. data/features/download.feature +20 -0
  9. data/features/file_storage.feature +37 -0
  10. data/features/file_storage_overridden_filename.feature +38 -0
  11. data/features/file_storage_overridden_store_dir.feature +38 -0
  12. data/features/file_storage_reversing_processor.feature +43 -0
  13. data/features/fixtures/bork.txt +1 -0
  14. data/features/fixtures/monkey.txt +1 -0
  15. data/features/grid_fs_storage.feature +32 -0
  16. data/features/mount_activerecord.feature +46 -0
  17. data/features/mount_datamapper.feature +46 -0
  18. data/features/step_definitions/activerecord_steps.rb +22 -0
  19. data/features/step_definitions/caching_steps.rb +14 -0
  20. data/features/step_definitions/datamapper_steps.rb +29 -0
  21. data/features/step_definitions/download_steps.rb +4 -0
  22. data/features/step_definitions/file_steps.rb +53 -0
  23. data/features/step_definitions/general_steps.rb +85 -0
  24. data/features/step_definitions/mount_steps.rb +19 -0
  25. data/features/step_definitions/store_steps.rb +18 -0
  26. data/features/support/activerecord.rb +30 -0
  27. data/features/support/datamapper.rb +7 -0
  28. data/features/support/env.rb +22 -0
  29. data/features/versions_basics.feature +50 -0
  30. data/features/versions_nested_versions.feature +70 -0
  31. data/features/versions_overridden_filename.feature +51 -0
  32. data/features/versions_overriden_store_dir.feature +41 -0
  33. data/lib/carrierwave/compatibility/paperclip.rb +95 -0
  34. data/lib/carrierwave/core_ext/blank.rb +46 -0
  35. data/lib/carrierwave/core_ext/inheritable_attributes.rb +104 -0
  36. data/lib/carrierwave/core_ext/module_setup.rb +51 -0
  37. data/lib/carrierwave/mount.rb +359 -0
  38. data/lib/carrierwave/orm/activerecord.rb +73 -0
  39. data/lib/carrierwave/orm/datamapper.rb +27 -0
  40. data/lib/carrierwave/orm/mongoid.rb +23 -0
  41. data/lib/carrierwave/orm/mongomapper.rb +27 -0
  42. data/lib/carrierwave/orm/sequel.rb +45 -0
  43. data/lib/carrierwave/processing/image_science.rb +101 -0
  44. data/lib/carrierwave/processing/mini_magick.rb +265 -0
  45. data/lib/carrierwave/processing/rmagick.rb +282 -0
  46. data/lib/carrierwave/sanitized_file.rb +273 -0
  47. data/lib/carrierwave/storage/abstract.rb +30 -0
  48. data/lib/carrierwave/storage/cloud_files.rb +169 -0
  49. data/lib/carrierwave/storage/file.rb +48 -0
  50. data/lib/carrierwave/storage/grid_fs.rb +97 -0
  51. data/lib/carrierwave/storage/right_s3.rb +167 -0
  52. data/lib/carrierwave/storage/s3.rb +199 -0
  53. data/lib/carrierwave/test/matchers.rb +128 -0
  54. data/lib/carrierwave/uploader/cache.rb +145 -0
  55. data/lib/carrierwave/uploader/callbacks.rb +42 -0
  56. data/lib/carrierwave/uploader/configuration.rb +130 -0
  57. data/lib/carrierwave/uploader/default_url.rb +19 -0
  58. data/lib/carrierwave/uploader/download.rb +59 -0
  59. data/lib/carrierwave/uploader/extension_whitelist.rb +37 -0
  60. data/lib/carrierwave/uploader/mountable.rb +39 -0
  61. data/lib/carrierwave/uploader/processing.rb +83 -0
  62. data/lib/carrierwave/uploader/proxy.rb +62 -0
  63. data/lib/carrierwave/uploader/remove.rb +22 -0
  64. data/lib/carrierwave/uploader/store.rb +89 -0
  65. data/lib/carrierwave/uploader/url.rb +33 -0
  66. data/lib/carrierwave/uploader/versions.rb +146 -0
  67. data/lib/carrierwave/uploader.rb +44 -0
  68. data/lib/carrierwave.rb +99 -0
  69. data/merb_generators/uploader_generator.rb +22 -0
  70. data/rails_generators/uploader/USAGE +2 -0
  71. data/rails_generators/uploader/templates/uploader.rb +47 -0
  72. data/rails_generators/uploader/uploader_generator.rb +21 -0
  73. data/script/console +10 -0
  74. data/script/destroy +14 -0
  75. data/script/generate +14 -0
  76. data/spec/compatibility/paperclip_spec.rb +52 -0
  77. data/spec/fixtures/bork.txt +1 -0
  78. data/spec/fixtures/landscape.jpg +0 -0
  79. data/spec/fixtures/portrait.jpg +0 -0
  80. data/spec/fixtures/test.jpeg +1 -0
  81. data/spec/fixtures/test.jpg +1 -0
  82. data/spec/mount_spec.rb +538 -0
  83. data/spec/orm/activerecord_spec.rb +271 -0
  84. data/spec/orm/datamapper_spec.rb +168 -0
  85. data/spec/orm/mongoid_spec.rb +202 -0
  86. data/spec/orm/mongomapper_spec.rb +202 -0
  87. data/spec/orm/sequel_spec.rb +183 -0
  88. data/spec/processing/image_science_spec.rb +56 -0
  89. data/spec/processing/mini_magick_spec.rb +76 -0
  90. data/spec/processing/rmagick_spec.rb +75 -0
  91. data/spec/sanitized_file_spec.rb +623 -0
  92. data/spec/spec_helper.rb +92 -0
  93. data/spec/storage/grid_fs_spec.rb +83 -0
  94. data/spec/storage/right_s3_spec.rb +83 -0
  95. data/spec/storage/s3_spec.rb +95 -0
  96. data/spec/uploader/cache_spec.rb +209 -0
  97. data/spec/uploader/configuration_spec.rb +105 -0
  98. data/spec/uploader/default_url_spec.rb +85 -0
  99. data/spec/uploader/download_spec.rb +75 -0
  100. data/spec/uploader/extension_whitelist_spec.rb +44 -0
  101. data/spec/uploader/mountable_spec.rb +33 -0
  102. data/spec/uploader/paths_spec.rb +22 -0
  103. data/spec/uploader/processing_spec.rb +73 -0
  104. data/spec/uploader/proxy_spec.rb +54 -0
  105. data/spec/uploader/remove_spec.rb +70 -0
  106. data/spec/uploader/store_spec.rb +264 -0
  107. data/spec/uploader/url_spec.rb +102 -0
  108. data/spec/uploader/versions_spec.rb +298 -0
  109. metadata +436 -0
@@ -0,0 +1,30 @@
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
+ end # Abstract
29
+ end # Storage
30
+ end # CarrierWave
@@ -0,0 +1,169 @@
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
+ #
19
+ # You can set the access policy for the uploaded files:
20
+ #
21
+ # CarrierWave.configure do |config|
22
+ # config.s3_access_policy = 'public-read'
23
+ # end
24
+ #
25
+ #
26
+ class CloudFiles < Abstract
27
+
28
+ class File
29
+
30
+ def initialize(uploader, base, path)
31
+ @uploader = uploader
32
+ @path = path
33
+ @base = base
34
+ end
35
+
36
+ ##
37
+ # Returns the current path/filename of the file on Cloud Files.
38
+ #
39
+ # === Returns
40
+ #
41
+ # [String] A path
42
+ #
43
+ def path
44
+ @path
45
+ end
46
+
47
+ ##
48
+ # Reads the contents of the file from Cloud Files
49
+ #
50
+ # === Returns
51
+ #
52
+ # [String] contents of the file
53
+ #
54
+ def read
55
+ object = cf_container.object(@path)
56
+ @content_type = object.content_type
57
+ object.data
58
+ end
59
+
60
+ ##
61
+ # Remove the file from Cloud Files
62
+ #
63
+ def delete
64
+ cf_container.delete_object(@path)
65
+ end
66
+
67
+ ##
68
+ # Returns the url on the Cloud Files CDN. Note that the parent container must be marked as
69
+ # public for this to work.
70
+ #
71
+ # === Returns
72
+ #
73
+ # [String] file's url
74
+ #
75
+ def url
76
+ cf_container.object(@path).public_url
77
+ end
78
+
79
+ #def metadata
80
+ # s3_object.metadata
81
+ #end
82
+
83
+ def content_type
84
+ cf_container.object(@path).content_type
85
+ end
86
+
87
+ def content_type=(new_content_type)
88
+ headers["content-type"] = new_content_type
89
+ end
90
+
91
+ ##
92
+ # Writes the supplied data into the object on Cloud Files.
93
+ #
94
+ # === Returns
95
+ #
96
+ # boolean
97
+ #
98
+ def store(data,headers={})
99
+ object = cf_container.create_object(@path)
100
+ object.write(data,headers)
101
+ end
102
+
103
+ private
104
+
105
+ def headers
106
+ @headers ||= { }
107
+ end
108
+
109
+ def container
110
+ @uploader.cloud_files_container
111
+ end
112
+
113
+ def connection
114
+ @base.connection
115
+ end
116
+
117
+ def cf_connection
118
+ @cf_connection ||= ::CloudFiles::Connection.new(@uploader.cloud_files_username, @uploader.cloud_files_api_key)
119
+ end
120
+
121
+ def cf_container
122
+ if @cf_container
123
+ @cf_container
124
+ else
125
+ @cf_container = cf_connection.create_container(container)
126
+ @cf_container.make_public
127
+ @cf_container
128
+ end
129
+ end
130
+
131
+
132
+ end
133
+
134
+ ##
135
+ # Store the file on Cloud Files
136
+ #
137
+ # === Parameters
138
+ #
139
+ # [file (CarrierWave::SanitizedFile)] the file to store
140
+ #
141
+ # === Returns
142
+ #
143
+ # [CarrierWave::Storage::RightS3::File] the stored file
144
+ #
145
+ def store!(file)
146
+ cloud_files_options = {'Content-Type' => file.content_type}
147
+ f = CarrierWave::Storage::CloudFiles::File.new(uploader, self, uploader.store_path)
148
+ f.store(file.read,cloud_files_options)
149
+ f
150
+ end
151
+
152
+ # Do something to retrieve the file
153
+ #
154
+ # @param [String] identifier uniquely identifies the file
155
+ #
156
+ # [identifier (String)] uniquely identifies the file
157
+ #
158
+ # === Returns
159
+ #
160
+ # [CarrierWave::Storage::RightS3::File] the stored file
161
+ #
162
+ def retrieve!(identifier)
163
+ CarrierWave::Storage::CloudFiles::File.new(uploader, self, uploader.store_path(identifier))
164
+ end
165
+
166
+
167
+ end # CloudFiles
168
+ end # Storage
169
+ end # CarrierWave
@@ -0,0 +1,48 @@
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
+ end # File
47
+ end # Storage
48
+ end # CarrierWave
@@ -0,0 +1,97 @@
1
+ # encoding: utf-8
2
+ require 'mongo'
3
+ require 'mongo/gridfs'
4
+
5
+ module CarrierWave
6
+ module Storage
7
+
8
+ ##
9
+ # The GridFS store uses MongoDB's GridStore file storage system to store files
10
+ #
11
+ class GridFS < Abstract
12
+
13
+ class File
14
+
15
+ def initialize(uploader, database, path)
16
+ @database = database
17
+ @path = path
18
+ @uploader = uploader
19
+ end
20
+
21
+ def path
22
+ nil
23
+ end
24
+
25
+ def url
26
+ unless @uploader.grid_fs_access_url
27
+ nil
28
+ else
29
+ [@uploader.grid_fs_access_url, @path].join("/")
30
+ end
31
+ end
32
+
33
+ def read
34
+ ::GridFS::GridStore.read(@database, @path)
35
+ end
36
+
37
+ def delete
38
+ ::GridFS::GridStore.unlink(@database, @path)
39
+ end
40
+
41
+ def content_type
42
+ ::GridFS::GridStore.open(@database, @path, 'r') { |f| return f.content_type }
43
+ end
44
+
45
+ end
46
+
47
+ ##
48
+ # Store the file in MongoDB's GridFS GridStore
49
+ #
50
+ # === Parameters
51
+ #
52
+ # [file (CarrierWave::SanitizedFile)] the file to store
53
+ #
54
+ # === Returns
55
+ #
56
+ # [CarrierWave::SanitizedFile] a sanitized file
57
+ #
58
+ def store!(file)
59
+ ::GridFS::GridStore.open(database, uploader.store_path, 'w', :content_type => file.content_type) do |f|
60
+ f.write file.read
61
+ end
62
+ CarrierWave::Storage::GridFS::File.new(uploader, database, uploader.store_path)
63
+ end
64
+
65
+ ##
66
+ # Retrieve the file from MongoDB's GridFS GridStore
67
+ #
68
+ # === Parameters
69
+ #
70
+ # [identifier (String)] the filename of the file
71
+ #
72
+ # === Returns
73
+ #
74
+ # [CarrierWave::Storage::GridFS::File] a sanitized file
75
+ #
76
+ def retrieve!(identifier)
77
+ CarrierWave::Storage::GridFS::File.new(uploader, database, uploader.store_path(identifier))
78
+ end
79
+
80
+ private
81
+
82
+ def database
83
+ @connection ||= begin
84
+ host = uploader.grid_fs_host
85
+ port = uploader.grid_fs_port
86
+ database = uploader.grid_fs_database
87
+ username = uploader.grid_fs_username
88
+ password = uploader.grid_fs_password
89
+ db = Mongo::Connection.new(host, port).db(database)
90
+ db.authenticate(username, password) if username && password
91
+ db
92
+ end
93
+ end
94
+
95
+ end # File
96
+ end # Storage
97
+ end # CarrierWave
@@ -0,0 +1,167 @@
1
+ # encoding: utf-8
2
+ require 'right_aws'
3
+
4
+ module CarrierWave
5
+ module Storage
6
+
7
+ ##
8
+ # Uploads things to Amazon S3 webservices using the RightAWS libraries (right_aws gem).
9
+ # In order for CarrierWave to connect to Amazon S3, you'll need to specify an access key id, secret key
10
+ # and bucket
11
+ #
12
+ # CarrierWave.configure do |config|
13
+ # config.s3_access_key_id = "xxxxxx"
14
+ # config.s3_secret_access_key = "xxxxxx"
15
+ # config.s3_bucket = "my_bucket_name"
16
+ # end
17
+ #
18
+ # The RightAWS::S3Interface is used directly as opposed to the normal RightAWS::S3::Bucket et.al. classes.
19
+ # This gives much improved performance and avoids unnecessary requests.
20
+ #
21
+ # You can set the access policy for the uploaded files:
22
+ #
23
+ # CarrierWave.configure do |config|
24
+ # config.s3_access_policy = 'public-read'
25
+ # end
26
+ #
27
+ # The default is 'public-read'. For more options see:
28
+ #
29
+ # http://docs.amazonwebservices.com/AmazonS3/latest/RESTAccessPolicy.html#RESTCannedAccessPolicies
30
+ #
31
+ # You can change the generated url to a cnamed domain by setting the cnamed config:
32
+ #
33
+ # CarrierWave.configure do |config|
34
+ # config.s3_cnamed = true
35
+ # config.s3_bucket = 'bucketname.domain.tld'
36
+ # end
37
+ #
38
+ # Now the resulting url will be
39
+ #
40
+ # http://bucketname.domain.tld/path/to/file
41
+ #
42
+ # instead of
43
+ #
44
+ # http://bucketname.domain.tld.s3.amazonaws.com/path/to/file
45
+ #
46
+ class RightS3 < Abstract
47
+
48
+ class File
49
+
50
+ def initialize(uploader, base, path)
51
+ @uploader = uploader
52
+ @path = path
53
+ @base = base
54
+ end
55
+
56
+ ##
57
+ # Returns the current path of the file on S3
58
+ #
59
+ # === Returns
60
+ #
61
+ # [String] A path
62
+ #
63
+ def path
64
+ @path
65
+ end
66
+
67
+ ##
68
+ # Reads the contents of the file from S3
69
+ #
70
+ # === Returns
71
+ #
72
+ # [String] contents of the file
73
+ #
74
+ def read
75
+ result = connection.get(bucket, @path)
76
+ @headers = result[:headers]
77
+ result[:object]
78
+ end
79
+
80
+ ##
81
+ # Remove the file from Amazon S3
82
+ #
83
+ def delete
84
+ connection.delete(bucket, @path)
85
+ end
86
+
87
+ ##
88
+ # Returns the url on Amazon's S3 service
89
+ #
90
+ # === Returns
91
+ #
92
+ # [String] file's url
93
+ #
94
+ def url
95
+ if @uploader.s3_cnamed
96
+ ["http://", @uploader.s3_bucket, "/", @path].compact.join
97
+ else
98
+ ["http://s3.amazonaws.com/", @uploader.s3_bucket, "/", @path].compact.join
99
+ end
100
+ end
101
+
102
+ def content_type
103
+ headers["content-type"]
104
+ end
105
+
106
+ #def content_disposition
107
+ # s3_object.content_disposition
108
+ #end
109
+
110
+ def store(file)
111
+ connection.put(bucket, @path, file.read, {'x-amz-acl' => @uploader.s3_access_policy}.merge(@uploader.s3_headers)
112
+ end
113
+
114
+ private
115
+
116
+ def headers
117
+ @headers ||= @uploader.s3_headers
118
+ end
119
+
120
+ def bucket
121
+ @uploader.s3_bucket
122
+ end
123
+
124
+ def connection
125
+ @base.connection
126
+ end
127
+
128
+ end
129
+
130
+ ##
131
+ # Store the file on S3
132
+ #
133
+ # === Parameters
134
+ #
135
+ # [file (CarrierWave::SanitizedFile)] the file to store
136
+ #
137
+ # === Returns
138
+ #
139
+ # [CarrierWave::Storage::RightS3::File] the stored file
140
+ #
141
+ def store!(file)
142
+ f = CarrierWave::Storage::RightS3::File.new(uploader, self, uploader.store_path)
143
+ f.store(file)
144
+ f
145
+ end
146
+
147
+ # Do something to retrieve the file
148
+ #
149
+ # @param [String] identifier uniquely identifies the file
150
+ #
151
+ # [identifier (String)] uniquely identifies the file
152
+ #
153
+ # === Returns
154
+ #
155
+ # [CarrierWave::Storage::RightS3::File] the stored file
156
+ #
157
+ def retrieve!(identifier)
158
+ CarrierWave::Storage::RightS3::File.new(uploader, self, uploader.store_path(identifier))
159
+ end
160
+
161
+ def connection
162
+ @connection ||= RightAws::S3Interface.new(uploader.s3_access_key_id, uploader.s3_secret_access_key)
163
+ end
164
+
165
+ end # RightS3
166
+ end # Storage
167
+ end # CarrierWave