From 44fb04c914f90e0b35d8664fe3af2bac96fccce7 Mon Sep 17 00:00:00 2001 From: Justin Ko Date: Thu, 2 May 2024 16:57:04 -0600 Subject: [PATCH] Allow explicit loading of ActiveStorage routes Fixes #31228 --- activestorage/config/routes.rb | 83 +------------------- activestorage/lib/active_storage.rb | 1 + activestorage/lib/active_storage/routes.rb | 90 ++++++++++++++++++++++ 3 files changed, 92 insertions(+), 82 deletions(-) create mode 100644 activestorage/lib/active_storage/routes.rb diff --git a/activestorage/config/routes.rb b/activestorage/config/routes.rb index d890a92efc6b6..d78216338369c 100644 --- a/activestorage/config/routes.rb +++ b/activestorage/config/routes.rb @@ -1,84 +1,3 @@ # frozen_string_literal: true -Rails.application.routes.draw do - scope ActiveStorage.routes_prefix do - get "/blobs/redirect/:signed_id/*filename" => "active_storage/blobs/redirect#show", as: :rails_service_blob - get "/blobs/proxy/:signed_id/*filename" => "active_storage/blobs/proxy#show", as: :rails_service_blob_proxy - get "/blobs/:signed_id/*filename" => "active_storage/blobs/redirect#show" - - get "/representations/redirect/:signed_blob_id/:variation_key/*filename" => "active_storage/representations/redirect#show", as: :rails_blob_representation - get "/representations/proxy/:signed_blob_id/:variation_key/*filename" => "active_storage/representations/proxy#show", as: :rails_blob_representation_proxy - get "/representations/:signed_blob_id/:variation_key/*filename" => "active_storage/representations/redirect#show" - - get "/disk/:encoded_key/*filename" => "active_storage/disk#show", as: :rails_disk_service - put "/disk/:encoded_token" => "active_storage/disk#update", as: :update_rails_disk_service - post "/direct_uploads" => "active_storage/direct_uploads#create", as: :rails_direct_uploads - end - - direct :rails_representation do |representation, options| - route_for(ActiveStorage.resolve_model_to_route, representation, options) - end - - resolve("ActiveStorage::Variant") { |variant, options| route_for(ActiveStorage.resolve_model_to_route, variant, options) } - resolve("ActiveStorage::VariantWithRecord") { |variant, options| route_for(ActiveStorage.resolve_model_to_route, variant, options) } - resolve("ActiveStorage::Preview") { |preview, options| route_for(ActiveStorage.resolve_model_to_route, preview, options) } - - direct :rails_blob do |blob, options| - route_for(ActiveStorage.resolve_model_to_route, blob, options) - end - - resolve("ActiveStorage::Blob") { |blob, options| route_for(ActiveStorage.resolve_model_to_route, blob, options) } - resolve("ActiveStorage::Attachment") { |attachment, options| route_for(ActiveStorage.resolve_model_to_route, attachment.blob, options) } - - direct :rails_storage_proxy do |model, options| - expires_in = options.delete(:expires_in) { ActiveStorage.urls_expire_in } - expires_at = options.delete(:expires_at) - - if model.respond_to?(:signed_id) - route_for( - :rails_service_blob_proxy, - model.signed_id(expires_in: expires_in, expires_at: expires_at), - model.filename, - options - ) - else - signed_blob_id = model.blob.signed_id(expires_in: expires_in, expires_at: expires_at) - variation_key = model.variation.key - filename = model.blob.filename - - route_for( - :rails_blob_representation_proxy, - signed_blob_id, - variation_key, - filename, - options - ) - end - end - - direct :rails_storage_redirect do |model, options| - expires_in = options.delete(:expires_in) { ActiveStorage.urls_expire_in } - expires_at = options.delete(:expires_at) - - if model.respond_to?(:signed_id) - route_for( - :rails_service_blob, - model.signed_id(expires_in: expires_in, expires_at: expires_at), - model.filename, - options - ) - else - signed_blob_id = model.blob.signed_id(expires_in: expires_in, expires_at: expires_at) - variation_key = model.variation.key - filename = model.blob.filename - - route_for( - :rails_blob_representation, - signed_blob_id, - variation_key, - filename, - options - ) - end - end -end if ActiveStorage.draw_routes +Rails.application.routes.draw { ActiveStorage::Routes.draw(self) } if ActiveStorage.draw_routes diff --git a/activestorage/lib/active_storage.rb b/activestorage/lib/active_storage.rb index 86782e4f1d421..cf819bf96c8a3 100644 --- a/activestorage/lib/active_storage.rb +++ b/activestorage/lib/active_storage.rb @@ -44,6 +44,7 @@ module ActiveStorage autoload :Service autoload :Previewer autoload :Analyzer + autoload :Routes mattr_accessor :logger mattr_accessor :verifier diff --git a/activestorage/lib/active_storage/routes.rb b/activestorage/lib/active_storage/routes.rb new file mode 100644 index 0000000000000..b0a7310c09c9c --- /dev/null +++ b/activestorage/lib/active_storage/routes.rb @@ -0,0 +1,90 @@ +# frozen_string_literal: true + +module ActiveStorage + module Routes + def self.draw(route_set) + route_set.instance_eval do + scope ActiveStorage.routes_prefix do + get "/blobs/redirect/:signed_id/*filename" => "active_storage/blobs/redirect#show", as: :rails_service_blob + get "/blobs/proxy/:signed_id/*filename" => "active_storage/blobs/proxy#show", as: :rails_service_blob_proxy + get "/blobs/:signed_id/*filename" => "active_storage/blobs/redirect#show" + + get "/representations/redirect/:signed_blob_id/:variation_key/*filename" => "active_storage/representations/redirect#show", as: :rails_blob_representation + get "/representations/proxy/:signed_blob_id/:variation_key/*filename" => "active_storage/representations/proxy#show", as: :rails_blob_representation_proxy + get "/representations/:signed_blob_id/:variation_key/*filename" => "active_storage/representations/redirect#show" + + get "/disk/:encoded_key/*filename" => "active_storage/disk#show", as: :rails_disk_service + put "/disk/:encoded_token" => "active_storage/disk#update", as: :update_rails_disk_service + post "/direct_uploads" => "active_storage/direct_uploads#create", as: :rails_direct_uploads + end + + direct :rails_representation do |representation, options| + route_for(ActiveStorage.resolve_model_to_route, representation, options) + end + + resolve("ActiveStorage::Variant") { |variant, options| route_for(ActiveStorage.resolve_model_to_route, variant, options) } + resolve("ActiveStorage::VariantWithRecord") { |variant, options| route_for(ActiveStorage.resolve_model_to_route, variant, options) } + resolve("ActiveStorage::Preview") { |preview, options| route_for(ActiveStorage.resolve_model_to_route, preview, options) } + + direct :rails_blob do |blob, options| + route_for(ActiveStorage.resolve_model_to_route, blob, options) + end + + resolve("ActiveStorage::Blob") { |blob, options| route_for(ActiveStorage.resolve_model_to_route, blob, options) } + resolve("ActiveStorage::Attachment") { |attachment, options| route_for(ActiveStorage.resolve_model_to_route, attachment.blob, options) } + + direct :rails_storage_proxy do |model, options| + expires_in = options.delete(:expires_in) { ActiveStorage.urls_expire_in } + expires_at = options.delete(:expires_at) + + if model.respond_to?(:signed_id) + route_for( + :rails_service_blob_proxy, + model.signed_id(expires_in: expires_in, expires_at: expires_at), + model.filename, + options + ) + else + signed_blob_id = model.blob.signed_id(expires_in: expires_in, expires_at: expires_at) + variation_key = model.variation.key + filename = model.blob.filename + + route_for( + :rails_blob_representation_proxy, + signed_blob_id, + variation_key, + filename, + options + ) + end + end + + direct :rails_storage_redirect do |model, options| + expires_in = options.delete(:expires_in) { ActiveStorage.urls_expire_in } + expires_at = options.delete(:expires_at) + + if model.respond_to?(:signed_id) + route_for( + :rails_service_blob, + model.signed_id(expires_in: expires_in, expires_at: expires_at), + model.filename, + options + ) + else + signed_blob_id = model.blob.signed_id(expires_in: expires_in, expires_at: expires_at) + variation_key = model.variation.key + filename = model.blob.filename + + route_for( + :rails_blob_representation, + signed_blob_id, + variation_key, + filename, + options + ) + end + end + end + end + end +end