Class: CarrierWave::FileCardUploader
- Inherits:
-
Uploader::Base
- Object
- Uploader::Base
- CarrierWave::FileCardUploader
- Includes:
- Card::Env::Location
- Defined in:
- mod/carrierwave/lib/carrier_wave/file_card_uploader.rb
Overview
Takes care of the file upload for cards with attached files. Most of the upload behaviour depends on the card itself. (e.g. card type and storage option chosen for the card). So in contrary to CarrierWave's default uploader we depend very much on the model (= card object) to get the correct paths for retrieving and storing the file.
Cards that support attachments (by default those are cards of type "file" and "image") accept a file handle as a card attribute.
It's possible to upload files using a url. The card attribute for that is
remote_
Storage types
You can choose between four different storage options
- coded: These files are in the codebase, like the default logo. Every view is a decko request.
- local: Uploaded files which are stored in a local upload directory (upload path is configurable via config.paths["files"]). If read permissions are set such that "Anyone" can read, then there is a symlink from the public directory. Otherwise every view is a wagn request.
- cloud: You can configure buckets that refer to an external storage service. Link is rendered as absolute url
- web: A fixed url (to external source). No upload or other file processing. Link is just the saved url.
Currently, there is no web interface that let's a user or administrator choose a storage option for a specific card or set of cards. There is only a global config option to set the storage type for all new uploads (config.storage_type). On the *admin card it's possible to update all existing file cards according to the current global config.
Storage types for single cards can be changed by developers using the card attributes "storage_type", "bucket", and "mod".
Depending on the storage type the uploader uses the following paths and identifiers.
Identifier (stored in the database as db_content)
- coded: :codename/mod_name.ext
- local: ~card_id/action_id.ext
- cloud: (bucket)/card_id/action_id.ext
- web: http://url
Storage path
- coded: mod_dir/file/codename/type_code(-variant).ext (no colon on codename!)
- local: files_dir/card_id/action_id(-variant).ext (no tilde on id!)
- cloud: bucket/bucket_subdir/id/action_id(-variant).ext
- web: no storage
Variants are only used for images. Possible options are icon|small|medium|large|original. files_dir, bucket, and bucket_subdir can be changed via config options.
Supported url patterns
mark.ext mark/revision.ext mark/revision-variant.ext /files/mark/revision-variant.ext # <- public symlink if readable by # "Anyone"
can be one of the following options
- ~
- :
Examples: *logo.png ~22/33-medium.png # local :yeti_skin/standard-large.png # coded
Direct Known Subclasses
Constant Summary collapse
- STORAGE_TYPES =
[:cloud, :web, :coded, :local].freeze
- CONFIG_OPTIONS =
[:provider, :attributes, :directory, :public, :credentials, :authenticated_url_expiration, :use_ssl_for_aws].freeze
- CONFIG_CREDENTIAL_OPTIONS =
[ :provider, :aws_access_key_id, :aws_secret_access_key, :region, :host, :endpoint, :google_access_key_id, :google_secret_access_key ].freeze
Instance Attribute Summary collapse
-
#mod ⇒ Object
Returns the value of attribute mod.
Instance Method Summary collapse
- #action_id ⇒ Object
- #asset_host ⇒ Object
- #bucket_config(option) ⇒ Object
- #cache_dir ⇒ Object
- #create_versions?(new_file) ⇒ Boolean
-
#db_content(opts = {}) ⇒ Object
generate identifier that gets stored in the card's db_content field.
- #extension ⇒ Object
- #filename ⇒ Object
- #local_url(opts = {}) ⇒ Object
- #local_url_base(opts = {}) ⇒ Object
- #original_filename ⇒ Object
-
#path(version = nil) ⇒ Object
paperclip compatibility used in type/file.rb#core (base format).
- #public_path ⇒ Object
- #retrieve_path ⇒ Object
-
#store_path(for_file = nil) ⇒ Object
Carrierwave calls store_path without argument when it stores the file and with the identifier from the db when it retrieves the file.
- #tmp_path ⇒ Object
- #url(opts = {}) ⇒ Object
- #url_filename(opts = {}) ⇒ Object
- #valid? ⇒ Boolean
Methods included from Card::Env::Location
#card_path, #card_url, #cardname_from_url, #protocol_and_host
Instance Attribute Details
#mod ⇒ Object
Returns the value of attribute mod.
163 164 165 |
# File 'mod/carrierwave/lib/carrier_wave/file_card_uploader.rb', line 163 def mod @mod end |
Instance Method Details
#action_id ⇒ Object
286 287 288 |
# File 'mod/carrierwave/lib/carrier_wave/file_card_uploader.rb', line 286 def action_id model.selected_content_action_id || action_id_stand_in end |
#asset_host ⇒ Object
299 300 301 |
# File 'mod/carrierwave/lib/carrier_wave/file_card_uploader.rb', line 299 def asset_host bucket_config(:asset_host) || super end |
#bucket_config(option) ⇒ Object
295 296 297 |
# File 'mod/carrierwave/lib/carrier_wave/file_card_uploader.rb', line 295 def bucket_config option @model.bucket_config[option] end |
#cache_dir ⇒ Object
246 247 248 |
# File 'mod/carrierwave/lib/carrier_wave/file_card_uploader.rb', line 246 def cache_dir @model.files_base_dir + "/cache" end |
#create_versions?(new_file) ⇒ Boolean
272 273 274 |
# File 'mod/carrierwave/lib/carrier_wave/file_card_uploader.rb', line 272 def create_versions? new_file model.create_versions? new_file end |
#db_content(opts = {}) ⇒ Object
generate identifier that gets stored in the card's db_content field
204 205 206 207 208 209 210 |
# File 'mod/carrierwave/lib/carrier_wave/file_card_uploader.rb', line 204 def db_content opts={} model. opts do return model.content if model.web? return "" unless file.present? "%s/%s" % [file_dir, url_filename] end end |
#extension ⇒ Object
188 189 190 191 192 193 194 195 |
# File 'mod/carrierwave/lib/carrier_wave/file_card_uploader.rb', line 188 def extension case when file&.extension.present? then ".#{file.extension}" when card_content = model.content then File.extname(card_content) when orig = original_filename then File.extname(orig) else "" end.downcase end |
#filename ⇒ Object
180 181 182 183 184 185 186 |
# File 'mod/carrierwave/lib/carrier_wave/file_card_uploader.rb', line 180 def filename if model.coded? "#{model.type_code}#{extension}" else "#{action_id}#{extension}" end end |
#local_url(opts = {}) ⇒ Object
233 234 235 |
# File 'mod/carrierwave/lib/carrier_wave/file_card_uploader.rb', line 233 def local_url opts={} "%s/%s/%s" % [local_url_base(opts), file_dir, full_filename(url_filename(opts))] end |
#local_url_base(opts = {}) ⇒ Object
237 238 239 240 |
# File 'mod/carrierwave/lib/carrier_wave/file_card_uploader.rb', line 237 def local_url_base opts={} web_path = Card.config.files_web_path opts.delete(:absolute) ? card_url(web_path) : card_path(web_path) end |
#original_filename ⇒ Object
281 282 283 284 |
# File 'mod/carrierwave/lib/carrier_wave/file_card_uploader.rb', line 281 def original_filename @original_filename ||= model.selected_action && model.selected_action.comment end |
#path(version = nil) ⇒ Object
paperclip compatibility used in type/file.rb#core (base format)
277 278 279 |
# File 'mod/carrierwave/lib/carrier_wave/file_card_uploader.rb', line 277 def path version=nil version ? versions[version].path : super() end |
#public_path ⇒ Object
242 243 244 |
# File 'mod/carrierwave/lib/carrier_wave/file_card_uploader.rb', line 242 def public_path File.join Cardio.paths["public"].existent.first, url end |
#retrieve_path ⇒ Object
263 264 265 |
# File 'mod/carrierwave/lib/carrier_wave/file_card_uploader.rb', line 263 def retrieve_path File.join([retrieve_dir, full_filename(filename)].compact) end |
#store_path(for_file = nil) ⇒ Object
Carrierwave calls store_path without argument when it stores the file and with the identifier from the db when it retrieves the file. In our case the first part of our identifier is not part of the path but we can construct the filename from db data. So we don't need the identifier.
255 256 257 258 259 260 261 |
# File 'mod/carrierwave/lib/carrier_wave/file_card_uploader.rb', line 255 def store_path for_file=nil if for_file retrieve_path else File.join([store_dir, full_filename(filename)].compact) end end |
#tmp_path ⇒ Object
267 268 269 270 |
# File 'mod/carrierwave/lib/carrier_wave/file_card_uploader.rb', line 267 def tmp_path Dir.mkdir model.tmp_upload_dir unless Dir.exist? model.tmp_upload_dir File.join model.tmp_upload_dir, filename end |
#url(opts = {}) ⇒ Object
223 224 225 226 227 228 229 230 231 |
# File 'mod/carrierwave/lib/carrier_wave/file_card_uploader.rb', line 223 def url opts={} if model.cloud? file&.url elsif model.web? model.content else local_url opts end end |
#url_filename(opts = {}) ⇒ Object
212 213 214 215 216 217 218 219 220 |
# File 'mod/carrierwave/lib/carrier_wave/file_card_uploader.rb', line 212 def url_filename opts={} model. opts do if model.coded? "#{model.mod}#{extension}" else "#{action_id}#{extension}" end end end |
#valid? ⇒ Boolean
176 177 178 |
# File 'mod/carrierwave/lib/carrier_wave/file_card_uploader.rb', line 176 def valid? extension.present? end |