class Shrine::Plugins::KitheAcceptRemoteUrl
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.attachment_column = { "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 shrine at Janko
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?
Constants
- METADATA_KEY
- STORAGE_KEY
Public Class Methods
# File lib/shrine/plugins/kithe_accept_remote_url.rb, line 36 def self.configure(uploader, options = {}) # 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
# 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