Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicit loading of ActiveStorage routes #51641

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 28 additions & 20 deletions activestorage/config/routes.rb
@@ -1,19 +1,27 @@
# 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"
ActiveStorage::Engine.routes.draw do
get "/blobs/redirect/:signed_id/*filename" => "blobs/redirect#show", as: :service_blob
get "/blobs/proxy/:signed_id/*filename" => "blobs/proxy#show", as: :service_blob_proxy
get "/blobs/:signed_id/*filename" => "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 "/representations/redirect/:signed_blob_id/:variation_key/*filename" => "representations/redirect#show", as: :blob_representation
get "/representations/proxy/:signed_blob_id/:variation_key/*filename" => "representations/proxy#show", as: :blob_representation_proxy
get "/representations/:signed_blob_id/:variation_key/*filename" => "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
get "/disk/:encoded_key/*filename" => "disk#show", as: :disk_service
put "/disk/:encoded_token" => "disk#update", as: :update_disk_service
post "/direct_uploads" => "direct_uploads#create", as: :direct_uploads
end

Rails.application.routes.draw do
direct(:rails_service_blob) {|*args| active_storage.route_for(:service_blob, *args) }
direct(:rails_service_blob_proxy) {|*args| active_storage.route_for(:service_blob_proxy, *args) }
direct(:rails_blob_representation) {|*args| active_storage.route_for(:blob_representation, *args) }
direct(:rails_blob_representation_proxy) {|*args| active_storage.route_for(:blob_representation_proxy, *args) }
direct(:rails_disk_service) {|*args| active_storage.route_for(:disk_service, *args) }
direct(:update_rails_disk_service) {|*args| active_storage.route_for(:update_disk_service, *args) }
direct(:rails_direct_uploads) {|*args| active_storage.route_for(:direct_uploads, *args) }

direct :rails_representation do |representation, options|
route_for(ActiveStorage.resolve_model_to_route, representation, options)
Expand All @@ -35,8 +43,8 @@
expires_at = options.delete(:expires_at)

if model.respond_to?(:signed_id)
route_for(
:rails_service_blob_proxy,
active_storage.route_for(
:service_blob_proxy,
model.signed_id(expires_in: expires_in, expires_at: expires_at),
model.filename,
options
Expand All @@ -46,8 +54,8 @@
variation_key = model.variation.key
filename = model.blob.filename

route_for(
:rails_blob_representation_proxy,
active_storage.route_for(
:blob_representation_proxy,
signed_blob_id,
variation_key,
filename,
Expand All @@ -61,8 +69,8 @@
expires_at = options.delete(:expires_at)

if model.respond_to?(:signed_id)
route_for(
:rails_service_blob,
active_storage.route_for(
:service_blob,
model.signed_id(expires_in: expires_in, expires_at: expires_at),
model.filename,
options
Expand All @@ -72,8 +80,8 @@
variation_key = model.variation.key
filename = model.blob.filename

route_for(
:rails_blob_representation,
active_storage.route_for(
:blob_representation,
signed_blob_id,
variation_key,
filename,
Expand Down
10 changes: 10 additions & 0 deletions activestorage/lib/active_storage/engine.rb
Expand Up @@ -197,6 +197,16 @@ class Engine < Rails::Engine # :nodoc:
end
end

initializer "active_storage.routes" do
config.after_initialize do |app|
if ActiveStorage.draw_routes
app.routes.prepend do
mount ActiveStorage::Engine => ActiveStorage.routes_prefix
end
end
end
end

initializer "active_storage.fixture_set" do
ActiveSupport.on_load(:active_record_fixture_set) do
ActiveStorage::FixtureSet.file_fixture_path ||= Rails.root.join(*[
Expand Down
6 changes: 3 additions & 3 deletions activestorage/lib/active_storage/service/disk_service.rb
Expand Up @@ -88,7 +88,7 @@ def url_for_direct_upload(key, expires_in:, content_type:, content_length:, chec
purpose: :blob_token
)

url_helpers.update_rails_disk_service_url(verified_token_with_expiration, url_options).tap do |generated_url|
url_helpers.update_disk_service_url(verified_token_with_expiration, url_options).tap do |generated_url|
payload[:url] = generated_url
end
end
Expand Down Expand Up @@ -138,7 +138,7 @@ def generate_url(key, expires_in:, filename:, content_type:, disposition:)
raise ArgumentError, "Cannot generate URL for #{filename} using Disk service, please set ActiveStorage::Current.url_options."
end

url_helpers.rails_disk_service_url(verified_key_with_expiration, filename: filename, **url_options)
url_helpers.disk_service_url(verified_key_with_expiration, filename: filename, **url_options)
end


Expand Down Expand Up @@ -168,7 +168,7 @@ def ensure_integrity_of(key, checksum)
end

def url_helpers
@url_helpers ||= Rails.application.routes.url_helpers
@url_helpers ||= ActiveStorage::Engine.routes.url_helpers
end

def url_options
Expand Down
6 changes: 3 additions & 3 deletions activestorage/test/service/disk_service_test.rb
Expand Up @@ -52,13 +52,13 @@ class ActiveStorage::Service::DiskServiceTest < ActiveSupport::TestCase
test "URL generation keeps working with ActiveStorage::Current.host set" do
ActiveStorage::Current.url_options = { host: "https://example.com" }

original_url_options = Rails.application.routes.default_url_options.dup
Rails.application.routes.default_url_options.merge!(protocol: "http", host: "test.example.com", port: 3001)
original_url_options = ActiveStorage::Engine.routes.default_url_options.dup
ActiveStorage::Engine.routes.default_url_options.merge!(protocol: "http", host: "test.example.com", port: 3001)
begin
assert_match(/^http:\/\/example.com:3001\/rails\/active_storage\/disk\/.*\/avatar\.png$/,
@service.url(@key, expires_in: 5.minutes, disposition: :inline, filename: ActiveStorage::Filename.new("avatar.png"), content_type: "image/png"))
ensure
Rails.application.routes.default_url_options = original_url_options
ActiveStorage::Engine.routes.default_url_options = original_url_options
end
end

Expand Down
1 change: 1 addition & 0 deletions activestorage/test/urls/rails_storage_proxy_url_test.rb
Expand Up @@ -5,6 +5,7 @@

class RailsStorageProxyUrlTest < ActiveSupport::TestCase
include Rails.application.routes.url_helpers
include Rails.application.routes.mounted_helpers

setup do
@blob = create_file_blob filename: "racecar.jpg"
Expand Down
1 change: 1 addition & 0 deletions activestorage/test/urls/rails_storage_redirect_url_test.rb
Expand Up @@ -5,6 +5,7 @@

class RailsStorageRedirectUrlTest < ActiveSupport::TestCase
include Rails.application.routes.url_helpers
include Rails.application.routes.mounted_helpers

setup do
@blob = create_file_blob filename: "racecar.jpg"
Expand Down
25 changes: 14 additions & 11 deletions railties/test/application/configuration_test.rb
Expand Up @@ -3750,17 +3750,20 @@ class Post < ActiveRecord::Base

output = rails("routes", "-g", "active_storage")
assert_equal <<~MESSAGE, output
Prefix Verb URI Pattern Controller#Action
/:controller(/:action(/:id))(.:format) :controller#:action
rails_service_blob GET /files/blobs/redirect/:signed_id/*filename(.:format) active_storage/blobs/redirect#show
rails_service_blob_proxy GET /files/blobs/proxy/:signed_id/*filename(.:format) active_storage/blobs/proxy#show
GET /files/blobs/:signed_id/*filename(.:format) active_storage/blobs/redirect#show
rails_blob_representation GET /files/representations/redirect/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations/redirect#show
rails_blob_representation_proxy GET /files/representations/proxy/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations/proxy#show
GET /files/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations/redirect#show
rails_disk_service GET /files/disk/:encoded_key/*filename(.:format) active_storage/disk#show
update_rails_disk_service PUT /files/disk/:encoded_token(.:format) active_storage/disk#update
rails_direct_uploads POST /files/direct_uploads(.:format) active_storage/direct_uploads#create
Prefix Verb URI Pattern Controller#Action
active_storage /files ActiveStorage::Engine
/:controller(/:action(/:id))(.:format) :controller#:action

Routes for ActiveStorage::Engine:
service_blob GET /blobs/redirect/:signed_id/*filename(.:format) active_storage/blobs/redirect#show
service_blob_proxy GET /blobs/proxy/:signed_id/*filename(.:format) active_storage/blobs/proxy#show
GET /blobs/:signed_id/*filename(.:format) active_storage/blobs/redirect#show
blob_representation GET /representations/redirect/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations/redirect#show
blob_representation_proxy GET /representations/proxy/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations/proxy#show
GET /representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations/redirect#show
disk_service GET /disk/:encoded_key/*filename(.:format) active_storage/disk#show
update_disk_service PUT /disk/:encoded_token(.:format) active_storage/disk#update
direct_uploads POST /direct_uploads(.:format) active_storage/direct_uploads#create
MESSAGE
end

Expand Down