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 can be mounted:

Rails.appliations.routes.draw do
  mount Shrine::DownloadEndpoint => "/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 specified storages, so it's not needed to change the code:

user.avatar.url #=> "/attachments/store/sdg0lsf8.jpg"

An array of storage keys which the download endpoint should be applied on.


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, because on most popular web servers (Puma, Unicorn, Passenger) workers handling this endpoint will not be able to serve new requests until the client has fully downloaded the response body.

To prevent download endpoint from impacting your request throughput, use a web server that handles streaming responses and slow clients well, like Thin, Rainbows or any other {EventMachine}[]-based web server that implements async.callback.


Public Class

  1. configure

Public Class methods

configure (uploader, opts = {})
[show source]
# File lib/shrine/plugins/download_endpoint.rb, line 61
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)