class MartenRailsBridge::ActiveStorage::Representations::ProxyHandler

Overview

Streams the bytes of a blob variant through the app, instead of redirecting to the storage service.

Proxying avoids a redirect and makes a variant easier to cache, since the response can be stored by a CDN sitting in front of the app.

Mount it on the route Rails uses, so that URLs issued by a Rails app keep resolving. Declare it after the redirect routes and before the pre-6.1 catch-all path, the way Rails declares them, or the catch-all swallows the proxy URLs:

path "/rails/active_storage/representations/proxy/<signed_blob_id:str>/<variation_key:str>/<filename:path>",
  MartenRailsBridge::ActiveStorage::Representations::ProxyHandler,
  name: "active_storage_representation_proxy"

The filename route parameter only shapes the URL: the filename served to the client is built from the blob filename and the variant format.

Like the redirect handler, a variation nothing has stored is transformed, stored and then streamed, so the first request for one writes. Which layout it is written to is ActiveStorage.track_variants; see ActiveStorage::Blob#variant.

WARNING like in Rails, this handler is publicly accessible. The signed IDs are hard to guess but permanent by design.

WARNING a GET that transforms is a GET that writes, so one unauthenticated request costs an image transformation, a storage upload and up to three rows. Put a before_dispatch guard on the mounted handler to gate it. https://github.com/rails/rails/blob/v8.1.3/activestorage/app/controllers/active_storage/representations/proxy_controller.rb https://github.com/rails/rails/blob/v8.1.3/activestorage/app/controllers/active_storage/representations/base_controller.rb

Defined in:

active_storage/representations/proxy_handler.cr

Constant Summary

DEFAULT_BLOB_STREAMING_DISPOSITION = "inline"

Disposition used when neither the variant nor the request forces one.

https://github.com/rails/rails/blob/v8.1.3/activestorage/app/controllers/concerns/active_storage/streaming.rb

DEFAULT_STREAMING_CONTENT_TYPE = "application/octet-stream"

Content type used when the variant carries none and none can be guessed from its filename.

https://github.com/rails/rails/blob/v8.1.3/actionpack/lib/action_controller/metal/live.rb

HTTP_CACHE_FOREVER_LAST_MODIFIED = "Sat, 01 Jan 2011 00:00:00 GMT"

Last-Modified of http_cache_forever, which Rails pins to 2011-01-01.

https://github.com/rails/rails/blob/v8.1.3/actionpack/lib/action_controller/metal/conditional_get.rb

HTTP_CACHE_FOREVER_MAX_AGE = 3155695200_i64

max-age of http_cache_forever, which is 100.years in seconds.

https://github.com/rails/rails/blob/v8.1.3/actionpack/lib/action_controller/metal/conditional_get.rb

Instance Method Summary

Instance Method Detail

def get #
Description copied from class Marten::Handlers::Base

Handles a GET HTTP request and returns a Marten::HTTP::Response object.

The default implementation will return a 405 (not allowed) response.