potatosalad-carrierwave 0.5.7
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +716 -0
- data/lib/carrierwave/compatibility/paperclip.rb +95 -0
- data/lib/carrierwave/locale/en.yml +5 -0
- data/lib/carrierwave/mount.rb +386 -0
- data/lib/carrierwave/orm/activerecord.rb +46 -0
- data/lib/carrierwave/orm/mongoid.rb +70 -0
- data/lib/carrierwave/processing/image_science.rb +135 -0
- data/lib/carrierwave/processing/mime_types.rb +58 -0
- data/lib/carrierwave/processing/mini_magick.rb +253 -0
- data/lib/carrierwave/processing/rmagick.rb +279 -0
- data/lib/carrierwave/sanitized_file.rb +302 -0
- data/lib/carrierwave/storage/abstract.rb +30 -0
- data/lib/carrierwave/storage/cloud_files.rb +188 -0
- data/lib/carrierwave/storage/file.rb +47 -0
- data/lib/carrierwave/storage/fog.rb +332 -0
- data/lib/carrierwave/storage/grid_fs.rb +136 -0
- data/lib/carrierwave/storage/right_s3.rb +1 -0
- data/lib/carrierwave/storage/s3.rb +240 -0
- data/lib/carrierwave/test/matchers.rb +164 -0
- data/lib/carrierwave/uploader/cache.rb +169 -0
- data/lib/carrierwave/uploader/callbacks.rb +35 -0
- data/lib/carrierwave/uploader/configuration.rb +164 -0
- data/lib/carrierwave/uploader/default_url.rb +19 -0
- data/lib/carrierwave/uploader/download.rb +75 -0
- data/lib/carrierwave/uploader/extension_whitelist.rb +49 -0
- data/lib/carrierwave/uploader/mountable.rb +39 -0
- data/lib/carrierwave/uploader/processing.rb +90 -0
- data/lib/carrierwave/uploader/proxy.rb +77 -0
- data/lib/carrierwave/uploader/remove.rb +23 -0
- data/lib/carrierwave/uploader/store.rb +111 -0
- data/lib/carrierwave/uploader/url.rb +34 -0
- data/lib/carrierwave/uploader/versions.rb +229 -0
- data/lib/carrierwave/uploader.rb +44 -0
- data/lib/carrierwave/validations/active_model.rb +79 -0
- data/lib/carrierwave/version.rb +3 -0
- data/lib/carrierwave.rb +111 -0
- data/lib/generators/templates/uploader.rb +49 -0
- data/lib/generators/uploader_generator.rb +7 -0
- data/lib/potatosalad-carrierwave.rb +1 -0
- metadata +280 -0
@@ -0,0 +1,188 @@
|
|
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. Optional arguments are config.cloud_files_snet (using the private internal
|
11
|
+
# Rackspace network for communication) and config.cloud_files_auth_url (for connecting to Rackspace's
|
12
|
+
# UK infrastructure or an OpenStack Swift installation)
|
13
|
+
#
|
14
|
+
# CarrierWave.configure do |config|
|
15
|
+
# config.cloud_files_username = "xxxxxx"
|
16
|
+
# config.cloud_files_api_key = "xxxxxx"
|
17
|
+
# config.cloud_files_container = "my_container"
|
18
|
+
# config.cloud_files_auth_url = "https://lon.auth.api.rackspacecloud.com/v1.0"
|
19
|
+
# config.cloud_files_snet = true
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
# You can optionally include your CDN host name in the configuration.
|
23
|
+
# This is *highly* recommended, as without it every request requires a lookup
|
24
|
+
# of this information.
|
25
|
+
#
|
26
|
+
# config.cloud_files_cdn_host = "c000000.cdn.rackspacecloud.com"
|
27
|
+
#
|
28
|
+
#
|
29
|
+
class CloudFiles < Abstract
|
30
|
+
|
31
|
+
class File
|
32
|
+
|
33
|
+
def initialize(uploader, base, path)
|
34
|
+
@uploader = uploader
|
35
|
+
@path = path
|
36
|
+
@base = base
|
37
|
+
end
|
38
|
+
|
39
|
+
##
|
40
|
+
# Returns the current path/filename of the file on Cloud Files.
|
41
|
+
#
|
42
|
+
# === Returns
|
43
|
+
#
|
44
|
+
# [String] A path
|
45
|
+
#
|
46
|
+
def path
|
47
|
+
@path
|
48
|
+
end
|
49
|
+
|
50
|
+
##
|
51
|
+
# Reads the contents of the file from Cloud Files
|
52
|
+
#
|
53
|
+
# === Returns
|
54
|
+
#
|
55
|
+
# [String] contents of the file
|
56
|
+
#
|
57
|
+
def read
|
58
|
+
object = cf_container.object(@path)
|
59
|
+
@content_type = object.content_type
|
60
|
+
object.data
|
61
|
+
end
|
62
|
+
|
63
|
+
##
|
64
|
+
# Remove the file from Cloud Files
|
65
|
+
#
|
66
|
+
def delete
|
67
|
+
begin
|
68
|
+
cf_container.delete_object(@path)
|
69
|
+
rescue ::CloudFiles::Exception::NoSuchObject
|
70
|
+
# If the file's not there, don't panic
|
71
|
+
nil
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
##
|
76
|
+
# Returns the url on the Cloud Files CDN. Note that the parent container must be marked as
|
77
|
+
# public for this to work.
|
78
|
+
#
|
79
|
+
# === Returns
|
80
|
+
#
|
81
|
+
# [String] file's url
|
82
|
+
#
|
83
|
+
def url
|
84
|
+
if @uploader.cloud_files_cdn_host
|
85
|
+
"http://" + @uploader.cloud_files_cdn_host + "/" + @path
|
86
|
+
else
|
87
|
+
begin
|
88
|
+
cf_container.object(@path).public_url
|
89
|
+
rescue ::CloudFiles::Exception::NoSuchObject
|
90
|
+
nil
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def content_type
|
96
|
+
cf_container.object(@path).content_type
|
97
|
+
end
|
98
|
+
|
99
|
+
def content_type=(new_content_type)
|
100
|
+
headers["content-type"] = new_content_type
|
101
|
+
end
|
102
|
+
|
103
|
+
##
|
104
|
+
# Writes the supplied data into the object on Cloud Files.
|
105
|
+
#
|
106
|
+
# === Returns
|
107
|
+
#
|
108
|
+
# boolean
|
109
|
+
#
|
110
|
+
def store(data,headers={})
|
111
|
+
object = cf_container.create_object(@path)
|
112
|
+
object.write(data,headers)
|
113
|
+
end
|
114
|
+
|
115
|
+
private
|
116
|
+
|
117
|
+
def headers
|
118
|
+
@headers ||= { }
|
119
|
+
end
|
120
|
+
|
121
|
+
def container
|
122
|
+
@uploader.cloud_files_container
|
123
|
+
end
|
124
|
+
|
125
|
+
def connection
|
126
|
+
@base.connection
|
127
|
+
end
|
128
|
+
|
129
|
+
def cf_connection
|
130
|
+
config = {:username => @uploader.cloud_files_username, :api_key => @uploader.cloud_files_api_key}
|
131
|
+
config[:auth_url] = @uploader.cloud_files_auth_url if @uploader.respond_to?(:cloud_files_auth_url)
|
132
|
+
config[:snet] = @uploader.cloud_files_snet if @uploader.respond_to?(:cloud_files_snet)
|
133
|
+
@cf_connection ||= ::CloudFiles::Connection.new(config)
|
134
|
+
end
|
135
|
+
|
136
|
+
def cf_container
|
137
|
+
if @cf_container
|
138
|
+
@cf_container
|
139
|
+
else
|
140
|
+
begin
|
141
|
+
@cf_container = cf_connection.container(container)
|
142
|
+
rescue NoSuchContainerException
|
143
|
+
@cf_container = cf_connection.create_container(container)
|
144
|
+
@cf_container.make_public
|
145
|
+
end
|
146
|
+
@cf_container
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
|
151
|
+
end
|
152
|
+
|
153
|
+
##
|
154
|
+
# Store the file on Cloud Files
|
155
|
+
#
|
156
|
+
# === Parameters
|
157
|
+
#
|
158
|
+
# [file (CarrierWave::SanitizedFile)] the file to store
|
159
|
+
#
|
160
|
+
# === Returns
|
161
|
+
#
|
162
|
+
# [CarrierWave::Storage::CloudFiles::File] the stored file
|
163
|
+
#
|
164
|
+
def store!(file)
|
165
|
+
cloud_files_options = {'Content-Type' => file.content_type}
|
166
|
+
f = CarrierWave::Storage::CloudFiles::File.new(uploader, self, uploader.store_path)
|
167
|
+
f.store(file.read,cloud_files_options)
|
168
|
+
f
|
169
|
+
end
|
170
|
+
|
171
|
+
# Do something to retrieve the file
|
172
|
+
#
|
173
|
+
# @param [String] identifier uniquely identifies the file
|
174
|
+
#
|
175
|
+
# [identifier (String)] uniquely identifies the file
|
176
|
+
#
|
177
|
+
# === Returns
|
178
|
+
#
|
179
|
+
# [CarrierWave::Storage::CloudFiles::File] the stored file
|
180
|
+
#
|
181
|
+
def retrieve!(identifier)
|
182
|
+
CarrierWave::Storage::CloudFiles::File.new(uploader, self, uploader.store_path(identifier))
|
183
|
+
end
|
184
|
+
|
185
|
+
|
186
|
+
end # CloudFiles
|
187
|
+
end # Storage
|
188
|
+
end # CarrierWave
|
@@ -0,0 +1,47 @@
|
|
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.copy_to(path, uploader.permissions)
|
27
|
+
end
|
28
|
+
|
29
|
+
##
|
30
|
+
# Retrieve the file from its store path
|
31
|
+
#
|
32
|
+
# === Parameters
|
33
|
+
#
|
34
|
+
# [identifier (String)] the filename of the file
|
35
|
+
#
|
36
|
+
# === Returns
|
37
|
+
#
|
38
|
+
# [CarrierWave::SanitizedFile] a sanitized file
|
39
|
+
#
|
40
|
+
def retrieve!(identifier)
|
41
|
+
path = ::File.expand_path(uploader.store_path(identifier), uploader.root)
|
42
|
+
CarrierWave::SanitizedFile.new(path)
|
43
|
+
end
|
44
|
+
|
45
|
+
end # File
|
46
|
+
end # Storage
|
47
|
+
end # CarrierWave
|
@@ -0,0 +1,332 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'fog'
|
5
|
+
rescue LoadError
|
6
|
+
raise "You don't have the 'fog' gem installed"
|
7
|
+
end
|
8
|
+
|
9
|
+
module CarrierWave
|
10
|
+
module Storage
|
11
|
+
|
12
|
+
##
|
13
|
+
# Stores things using the "fog" gem.
|
14
|
+
#
|
15
|
+
# fog supports storing files with AWS, Google, Local and Rackspace
|
16
|
+
#
|
17
|
+
# You need to setup some options to configure your usage:
|
18
|
+
#
|
19
|
+
# [:fog_credentials] credentials for service
|
20
|
+
# [:fog_directory] specifies name of directory to store data in, assumed to already exist
|
21
|
+
#
|
22
|
+
# [:fog_attributes] (optional) additional attributes to set on files
|
23
|
+
# [:fog_host] (optional) non-default host to serve files from
|
24
|
+
# [:fog_public] (optional) public readability, defaults to true
|
25
|
+
# [:fog_authenticated_url_expiration] (optional) time (in seconds) that authenticated urls
|
26
|
+
# will be valid, when fog_public is false and provider is AWS or Google, defaults to 600
|
27
|
+
#
|
28
|
+
#
|
29
|
+
# AWS credentials contain the following keys:
|
30
|
+
#
|
31
|
+
# [:aws_access_key_id]
|
32
|
+
# [:aws_secret_access_key]
|
33
|
+
# [:region] (optional) defaults to 'us-east-1'
|
34
|
+
# :region should be one of ['eu-west-1', 'us-east-1', 'ap-southeast-1', 'us-west-1', 'ap-northeast-1']
|
35
|
+
#
|
36
|
+
#
|
37
|
+
# Google credentials contain the following keys:
|
38
|
+
# [:google_storage_access_key_id]
|
39
|
+
# [:google_storage_secrete_access_key]
|
40
|
+
#
|
41
|
+
#
|
42
|
+
# Local credentials contain the following keys:
|
43
|
+
#
|
44
|
+
# [:local_root] local path to files
|
45
|
+
#
|
46
|
+
#
|
47
|
+
# Rackspace credentials contain the following keys:
|
48
|
+
#
|
49
|
+
# [:rackspace_username]
|
50
|
+
# [:rackspace_api_key]
|
51
|
+
#
|
52
|
+
#
|
53
|
+
# A full example with AWS credentials:
|
54
|
+
# CarrierWave.configure do |config|
|
55
|
+
# config.fog_credentials = {
|
56
|
+
# :aws_access_key_id => 'xxxxxx',
|
57
|
+
# :aws_secret_access_key => 'yyyyyy',
|
58
|
+
# :provider => 'AWS'
|
59
|
+
# }
|
60
|
+
# config.fog_directory = 'directoryname'
|
61
|
+
# config.fog_public = true
|
62
|
+
# end
|
63
|
+
#
|
64
|
+
class Fog < Abstract
|
65
|
+
|
66
|
+
##
|
67
|
+
# Store a file
|
68
|
+
#
|
69
|
+
# === Parameters
|
70
|
+
#
|
71
|
+
# [file (CarrierWave::SanitizedFile)] the file to store
|
72
|
+
#
|
73
|
+
# === Returns
|
74
|
+
#
|
75
|
+
# [CarrierWave::Storage::Fog::File] the stored file
|
76
|
+
#
|
77
|
+
def store!(file)
|
78
|
+
f = CarrierWave::Storage::Fog::File.new(uploader, self, uploader.store_path)
|
79
|
+
f.store(file)
|
80
|
+
f
|
81
|
+
end
|
82
|
+
|
83
|
+
##
|
84
|
+
# Retrieve a file
|
85
|
+
#
|
86
|
+
# === Parameters
|
87
|
+
#
|
88
|
+
# [identifier (String)] unique identifier for file
|
89
|
+
#
|
90
|
+
# === Returns
|
91
|
+
#
|
92
|
+
# [CarrierWave::Storage::Fog::File] the stored file
|
93
|
+
#
|
94
|
+
def retrieve!(identifier)
|
95
|
+
CarrierWave::Storage::Fog::File.new(uploader, self, uploader.store_path(identifier))
|
96
|
+
end
|
97
|
+
|
98
|
+
def connection
|
99
|
+
@connection ||= begin
|
100
|
+
::Fog::Storage.new(uploader.fog_credentials)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
class File
|
105
|
+
|
106
|
+
##
|
107
|
+
# Current local path to file
|
108
|
+
#
|
109
|
+
# === Returns
|
110
|
+
#
|
111
|
+
# [String] a path to file
|
112
|
+
#
|
113
|
+
attr_reader :path
|
114
|
+
|
115
|
+
##
|
116
|
+
# Return all attributes from file
|
117
|
+
#
|
118
|
+
# === Returns
|
119
|
+
#
|
120
|
+
# [Hash] attributes from file
|
121
|
+
#
|
122
|
+
def attributes
|
123
|
+
file.attributes
|
124
|
+
end
|
125
|
+
|
126
|
+
##
|
127
|
+
# Return a temporary authenticated url to a private file, if available
|
128
|
+
# Only supported for AWS and Google providers
|
129
|
+
#
|
130
|
+
# === Returns
|
131
|
+
#
|
132
|
+
# [String] temporary authenticated url
|
133
|
+
# or
|
134
|
+
# [NilClass] no authenticated url available
|
135
|
+
#
|
136
|
+
def authenticated_url
|
137
|
+
if ['AWS', 'Google'].include?(@uploader.fog_credentials[:provider])
|
138
|
+
# avoid a get by using local references
|
139
|
+
local_directory = connection.directories.new(:key => @uploader.fog_directory)
|
140
|
+
local_file = local_directory.files.new(:key => path)
|
141
|
+
local_file.url(::Fog::Time.now + @uploader.fog_authenticated_url_expiration)
|
142
|
+
else
|
143
|
+
nil
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
##
|
148
|
+
# Lookup value for file content-type header
|
149
|
+
#
|
150
|
+
# === Returns
|
151
|
+
#
|
152
|
+
# [String] value of content-type
|
153
|
+
#
|
154
|
+
def content_type
|
155
|
+
@content_type || file.content_type
|
156
|
+
end
|
157
|
+
|
158
|
+
##
|
159
|
+
# Set non-default content-type header (default is file.content_type)
|
160
|
+
#
|
161
|
+
# === Returns
|
162
|
+
#
|
163
|
+
# [String] returns new content type value
|
164
|
+
#
|
165
|
+
def content_type=(new_content_type)
|
166
|
+
@content_type = new_content_type
|
167
|
+
end
|
168
|
+
|
169
|
+
##
|
170
|
+
# Remove the file from service
|
171
|
+
#
|
172
|
+
# === Returns
|
173
|
+
#
|
174
|
+
# [Boolean] true for success or raises error
|
175
|
+
#
|
176
|
+
def delete
|
177
|
+
# avoid a get by just using local reference
|
178
|
+
directory.files.new(:key => path).destroy
|
179
|
+
end
|
180
|
+
|
181
|
+
##
|
182
|
+
# deprecated: All attributes from file (includes headers)
|
183
|
+
#
|
184
|
+
# === Returns
|
185
|
+
#
|
186
|
+
# [Hash] attributes from file
|
187
|
+
#
|
188
|
+
def headers
|
189
|
+
location = caller.first
|
190
|
+
warning = "[yellow][WARN] headers is deprecated, use attributes instead[/]"
|
191
|
+
warning << " [light_black](#{location})[/]"
|
192
|
+
Formatador.display_line(warning)
|
193
|
+
attributes
|
194
|
+
end
|
195
|
+
|
196
|
+
def initialize(uploader, base, path)
|
197
|
+
@uploader, @base, @path = uploader, base, path
|
198
|
+
end
|
199
|
+
|
200
|
+
##
|
201
|
+
# Read content of file from service
|
202
|
+
#
|
203
|
+
# === Returns
|
204
|
+
#
|
205
|
+
# [String] contents of file
|
206
|
+
def read
|
207
|
+
file.body
|
208
|
+
end
|
209
|
+
|
210
|
+
##
|
211
|
+
# Return size of file body
|
212
|
+
#
|
213
|
+
# === Returns
|
214
|
+
#
|
215
|
+
# [Integer] size of file body
|
216
|
+
#
|
217
|
+
def size
|
218
|
+
file.content_length
|
219
|
+
end
|
220
|
+
|
221
|
+
##
|
222
|
+
# Write file to service
|
223
|
+
#
|
224
|
+
# === Returns
|
225
|
+
#
|
226
|
+
# [Boolean] true on success or raises error
|
227
|
+
def store(new_file)
|
228
|
+
@content_type ||= new_file.content_type
|
229
|
+
@file = directory.files.create({
|
230
|
+
:body => new_file.read,
|
231
|
+
:content_type => @content_type,
|
232
|
+
:key => path,
|
233
|
+
:public => @uploader.fog_public
|
234
|
+
}.merge(@uploader.fog_attributes))
|
235
|
+
true
|
236
|
+
end
|
237
|
+
|
238
|
+
##
|
239
|
+
# Return a url to a public file, if available
|
240
|
+
#
|
241
|
+
# === Returns
|
242
|
+
#
|
243
|
+
# [String] public url
|
244
|
+
# or
|
245
|
+
# [NilClass] no public url available
|
246
|
+
#
|
247
|
+
def public_url
|
248
|
+
if host = @uploader.fog_host
|
249
|
+
"#{host}/#{path}"
|
250
|
+
else
|
251
|
+
# AWS/Google optimized for speed over correctness
|
252
|
+
case @uploader.fog_credentials[:provider]
|
253
|
+
when 'AWS'
|
254
|
+
# if directory is a valid subdomain, use that style for access
|
255
|
+
if @uploader.fog_directory.to_s =~ /^(?:[a-z]|\d(?!\d{0,2}(?:\.\d{1,3}){3}$))(?:[a-z0-9]|\.(?![\.\-])|\-(?![\.])){1,61}[a-z0-9]$/
|
256
|
+
"https://#{@uploader.fog_directory}.s3.amazonaws.com/#{path}"
|
257
|
+
else
|
258
|
+
# directory is not a valid subdomain, so use path style for access
|
259
|
+
"https://s3.amazonaws.com/#{@uploader.fog_directory}/#{path}"
|
260
|
+
end
|
261
|
+
when 'Google'
|
262
|
+
"https://commondatastorage.googleapis.com/#{@uploader.fog_directory}/#{path}"
|
263
|
+
else
|
264
|
+
# avoid a get by just using local reference
|
265
|
+
directory.files.new(:key => path).public_url
|
266
|
+
end
|
267
|
+
end
|
268
|
+
end
|
269
|
+
|
270
|
+
##
|
271
|
+
# Return url to file, if avaliable
|
272
|
+
#
|
273
|
+
# === Returns
|
274
|
+
#
|
275
|
+
# [String] url
|
276
|
+
# or
|
277
|
+
# [NilClass] no url available
|
278
|
+
#
|
279
|
+
def url
|
280
|
+
if !@uploader.fog_public
|
281
|
+
authenticated_url
|
282
|
+
else
|
283
|
+
public_url
|
284
|
+
end
|
285
|
+
end
|
286
|
+
|
287
|
+
private
|
288
|
+
|
289
|
+
##
|
290
|
+
# connection to service
|
291
|
+
#
|
292
|
+
# === Returns
|
293
|
+
#
|
294
|
+
# [Fog::#{provider}::Storage] connection to service
|
295
|
+
#
|
296
|
+
def connection
|
297
|
+
@base.connection
|
298
|
+
end
|
299
|
+
|
300
|
+
##
|
301
|
+
# local reference to directory containing file
|
302
|
+
#
|
303
|
+
# === Returns
|
304
|
+
#
|
305
|
+
# [Fog::#{provider}::Directory] containing directory
|
306
|
+
#
|
307
|
+
def directory
|
308
|
+
@directory ||= begin
|
309
|
+
connection.directories.new(
|
310
|
+
:key => @uploader.fog_directory,
|
311
|
+
:public => @uploader.fog_public
|
312
|
+
)
|
313
|
+
end
|
314
|
+
end
|
315
|
+
|
316
|
+
##
|
317
|
+
# lookup file
|
318
|
+
#
|
319
|
+
# === Returns
|
320
|
+
#
|
321
|
+
# [Fog::#{provider}::File] file data from remote service
|
322
|
+
#
|
323
|
+
def file
|
324
|
+
@file ||= directory.files.get(path)
|
325
|
+
end
|
326
|
+
|
327
|
+
end
|
328
|
+
|
329
|
+
end # Fog
|
330
|
+
|
331
|
+
end # Storage
|
332
|
+
end # CarrierWave
|
@@ -0,0 +1,136 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'mongo'
|
3
|
+
|
4
|
+
module CarrierWave
|
5
|
+
module Storage
|
6
|
+
|
7
|
+
##
|
8
|
+
# The GridFS store uses MongoDB's GridStore file storage system to store files
|
9
|
+
#
|
10
|
+
# There are two ways of configuring the GridFS connection. Either you create a
|
11
|
+
# connection or you reuse an existing connection.
|
12
|
+
#
|
13
|
+
# Creating a connection looks something like this:
|
14
|
+
#
|
15
|
+
# CarrierWave.configure do |config|
|
16
|
+
# config.storage = :grid_fs
|
17
|
+
# config.grid_fs_host = "your-host.com"
|
18
|
+
# config.grid_fs_port = "27017"
|
19
|
+
# config.grid_fs_database = "your_dbs_app_name"
|
20
|
+
# config.grid_fs_username = "user"
|
21
|
+
# config.grid_fs_password = "verysecret"
|
22
|
+
# config.grid_fs_access_url = "/images"
|
23
|
+
# end
|
24
|
+
#
|
25
|
+
# In the above example your documents url will look like:
|
26
|
+
#
|
27
|
+
# http://your-app.com/images/:document-identifier-here
|
28
|
+
#
|
29
|
+
# When you already have a Mongo connection object (for example through Mongoid)
|
30
|
+
# you can also reuse this connection:
|
31
|
+
#
|
32
|
+
# CarrierWave.configure do |config|
|
33
|
+
# config.storage = :grid_fs
|
34
|
+
# config.grid_fs_connection = Mongoid.database
|
35
|
+
# config.grid_fs_access_url = "/images"
|
36
|
+
# end
|
37
|
+
#
|
38
|
+
class GridFS < Abstract
|
39
|
+
|
40
|
+
class File
|
41
|
+
|
42
|
+
def initialize(uploader, path)
|
43
|
+
@path = path
|
44
|
+
@uploader = uploader
|
45
|
+
end
|
46
|
+
|
47
|
+
def path
|
48
|
+
@path
|
49
|
+
end
|
50
|
+
|
51
|
+
def url
|
52
|
+
unless @uploader.grid_fs_access_url
|
53
|
+
nil
|
54
|
+
else
|
55
|
+
[@uploader.grid_fs_access_url, @path].join("/")
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def read
|
60
|
+
grid.open(@path, 'r').data
|
61
|
+
end
|
62
|
+
|
63
|
+
def write(file)
|
64
|
+
grid.open(@uploader.store_path, 'w', :content_type => file.content_type) do |f|
|
65
|
+
f.write(file.read)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def delete
|
70
|
+
grid.delete(@path)
|
71
|
+
end
|
72
|
+
|
73
|
+
def content_type
|
74
|
+
grid.open(@path, 'r').content_type
|
75
|
+
end
|
76
|
+
|
77
|
+
def file_length
|
78
|
+
grid.open(@path, 'r').file_length
|
79
|
+
end
|
80
|
+
|
81
|
+
protected
|
82
|
+
|
83
|
+
def database
|
84
|
+
@connection ||= @uploader.grid_fs_connection || begin
|
85
|
+
host = @uploader.grid_fs_host
|
86
|
+
port = @uploader.grid_fs_port
|
87
|
+
database = @uploader.grid_fs_database
|
88
|
+
username = @uploader.grid_fs_username
|
89
|
+
password = @uploader.grid_fs_password
|
90
|
+
db = Mongo::Connection.new(host, port).db(database)
|
91
|
+
db.authenticate(username, password) if username && password
|
92
|
+
db
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def grid
|
97
|
+
@grid ||= Mongo::GridFileSystem.new(database)
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
101
|
+
|
102
|
+
##
|
103
|
+
# Store the file in MongoDB's GridFS GridStore
|
104
|
+
#
|
105
|
+
# === Parameters
|
106
|
+
#
|
107
|
+
# [file (CarrierWave::SanitizedFile)] the file to store
|
108
|
+
#
|
109
|
+
# === Returns
|
110
|
+
#
|
111
|
+
# [CarrierWave::SanitizedFile] a sanitized file
|
112
|
+
#
|
113
|
+
def store!(file)
|
114
|
+
stored = CarrierWave::Storage::GridFS::File.new(uploader, uploader.store_path)
|
115
|
+
stored.write(file)
|
116
|
+
stored
|
117
|
+
end
|
118
|
+
|
119
|
+
##
|
120
|
+
# Retrieve the file from MongoDB's GridFS GridStore
|
121
|
+
#
|
122
|
+
# === Parameters
|
123
|
+
#
|
124
|
+
# [identifier (String)] the filename of the file
|
125
|
+
#
|
126
|
+
# === Returns
|
127
|
+
#
|
128
|
+
# [CarrierWave::Storage::GridFS::File] a sanitized file
|
129
|
+
#
|
130
|
+
def retrieve!(identifier)
|
131
|
+
CarrierWave::Storage::GridFS::File.new(uploader, uploader.store_path(identifier))
|
132
|
+
end
|
133
|
+
|
134
|
+
end # File
|
135
|
+
end # Storage
|
136
|
+
end # CarrierWave
|
@@ -0,0 +1 @@
|
|
1
|
+
raise "The right_aws library is no longer supported. Please install the 'fog' gem instead."
|