Class: Shrine::Plugins::KitheAcceptRemoteUrl
- Inherits:
-
Object
- Object
- Shrine::Plugins::KitheAcceptRemoteUrl
- Defined in:
- lib/shrine/plugins/kithe_accept_remote_url.rb
Overview
This plugin supports assigning remote URLs to shrine attachments, in uploaders that have normal default cache storage.
It also supports specifying custom request headers for those remote URLs, to support OAuth2 Authorization headers, with browse-everything or similar.
model. = {
"storate" => "remote_url",
"id" => "http://example.com/image.jpg",
"headers" => {
"Authorization" => "something"
}
}
If you provide the (optional) “headers” key, they will wind up stored with file data in “metadata” hash, as “remote_url_headers”. And they will be used with HTTP request to fetch the URL given, when promoting.
The implementation uses the shrine-url storage, registering it as storage with key “remote_url”; our custom kithe_multi_cache plugin to allow this alternate storage to be set as cache even though it’s not main cache; and a #promote override suggested by Janko@shrine to get request headers to be supported.
Testing is done in context of Asset model, see asset_spec.rb.
FUTURE. Need to whitelist allowed URLs/hostnames. :( A pain cause it’ll be different for different apps, so we need to allow uploader customization?
Defined Under Namespace
Modules: AttacherMethods, FileMethods
Constant Summary collapse
- STORAGE_KEY =
:remote_url
- METADATA_KEY =
"remote_url_headers"
Class Method Summary collapse
Class Method Details
.configure(uploader, options = {}) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/shrine/plugins/kithe_accept_remote_url.rb', line 36 def self.configure(uploader, = {}) # Ensure remote_url is registered as a storage # # Lazy default to make it easier to specify other if an app wants to. uploader.storages[STORAGE_KEY] ||= Shrine::Storage::Url.new end |
.load_dependencies(uploader) ⇒ Object
43 44 45 46 47 |
# File 'lib/shrine/plugins/kithe_accept_remote_url.rb', line 43 def self.load_dependencies(uploader, *) # Make sure the uploader will accept assignments/promotion from remote_url, using # our multi_cache plugin. uploader.plugin :kithe_multi_cache, additional_cache: :remote_url end |