module Shrine::Plugins::DownloadEndpoint

  1. lib/shrine/plugins/download_endpoint.rb

The download_endpoint plugin provides a Rack endpoint for downloading uploaded files from specified storages. This can be useful when files from your storages aren't accessible over URL (e.g. database storages) or if you want to authenticate your downloads. It requires the Roda gem.

plugin :download_endpoint, storages: [:store], prefix: "attachments"

After loading the plugin the endpoint should be mounted on the specified prefix:

# (Rack)
map "/attachments" do
  run Shrine.download_endpoint

# OR

# config/routes.rb (Rails)
Rails.application.routes.draw do
  mount Shrine.download_endpoint => "/attachments"

Now all stored files can be downloaded through the endpoint, and the endpoint will efficiently stream the file from the storage when the storage supports it. UploadedFile#url will automatically return the URL to the endpoint for files uploaded to specified storages:

user.avatar.url #=> "/attachments/eyJpZCI6ImFkdzlyeTM5ODJpandoYWla"

An array of storage keys for which UploadedFile#url should generate download endpoint URLs.


The location where the download endpoint was mounted. If it was mounted at the root level, this should be set to nil.


The host that you want the download URLs to use (e.g. your app's domain name or a CDN). By default URLs are relative.


Can be set to “attachment” if you want that the user is always prompted to download the file when visiting the download URL. The default is “inline”.

Note that streaming the file through your app might impact the request throughput of your app, depending on which web server is used. In any case, it's recommended to use some kind of cache in front of the web server.

If you want to authenticate the downloads, it's recommended you use the rack_response plugin directly. With it you can return file responses from inside your router/controller.


Public Class

  1. configure
  2. load_dependencies

Public Class methods

configure (uploader, opts = {})
[show source]
# File lib/shrine/plugins/download_endpoint.rb, line 70
def self.configure(uploader, opts = {})
  uploader.opts[:download_endpoint_storages] = opts.fetch(:storages, uploader.opts[:download_endpoint_storages])
  uploader.opts[:download_endpoint_prefix] = opts.fetch(:prefix, uploader.opts[:download_endpoint_prefix])
  uploader.opts[:download_endpoint_disposition] = opts.fetch(:disposition, uploader.opts.fetch(:download_endpoint_disposition, "inline"))
  uploader.opts[:download_endpoint_host] = opts.fetch(:host, uploader.opts[:download_endpoint_host])

  raise Error, "The :storages option is required for download_endpoint plugin" if uploader.opts[:download_endpoint_storages].nil?

  uploader.assign_download_endpoint(App) unless uploader.const_defined?(:DownloadEndpoint)
load_dependencies (uploader, opts = {})
[show source]
# File lib/shrine/plugins/download_endpoint.rb, line 66
def self.load_dependencies(uploader, opts = {})
  uploader.plugin :rack_response