From b21f50d8ae36d9b50b673579e17bccbe55363b34 Mon Sep 17 00:00:00 2001 From: George Claghorn Date: Thu, 21 Jun 2018 11:06:32 -0400 Subject: Permit configuring the default service URL expiry --- activestorage/app/controllers/active_storage/blobs_controller.rb | 2 +- .../app/controllers/active_storage/representations_controller.rb | 2 +- activestorage/app/models/active_storage/blob.rb | 4 ++-- activestorage/app/models/active_storage/variant.rb | 2 +- activestorage/lib/active_storage.rb | 1 + activestorage/lib/active_storage/engine.rb | 1 + activestorage/lib/active_storage/service.rb | 2 -- activestorage/test/models/blob_test.rb | 6 +++--- guides/source/configuring.md | 7 +++++++ 9 files changed, 17 insertions(+), 10 deletions(-) diff --git a/activestorage/app/controllers/active_storage/blobs_controller.rb b/activestorage/app/controllers/active_storage/blobs_controller.rb index 92e54c386d..4fc3fbe824 100644 --- a/activestorage/app/controllers/active_storage/blobs_controller.rb +++ b/activestorage/app/controllers/active_storage/blobs_controller.rb @@ -8,7 +8,7 @@ class ActiveStorage::BlobsController < ActiveStorage::BaseController include ActiveStorage::SetBlob def show - expires_in ActiveStorage::Blob.service.url_expires_in + expires_in ActiveStorage.service_urls_expire_in redirect_to @blob.service_url(disposition: params[:disposition]) end end diff --git a/activestorage/app/controllers/active_storage/representations_controller.rb b/activestorage/app/controllers/active_storage/representations_controller.rb index ce9286db7d..98e11e5dbb 100644 --- a/activestorage/app/controllers/active_storage/representations_controller.rb +++ b/activestorage/app/controllers/active_storage/representations_controller.rb @@ -8,7 +8,7 @@ class ActiveStorage::RepresentationsController < ActiveStorage::BaseController include ActiveStorage::SetBlob def show - expires_in ActiveStorage::Blob.service.url_expires_in + expires_in ActiveStorage.service_urls_expire_in redirect_to @blob.representation(params[:variation_key]).processed.service_url(disposition: params[:disposition]) end end diff --git a/activestorage/app/models/active_storage/blob.rb b/activestorage/app/models/active_storage/blob.rb index 203e4118c5..73029f21a1 100644 --- a/activestorage/app/models/active_storage/blob.rb +++ b/activestorage/app/models/active_storage/blob.rb @@ -121,7 +121,7 @@ class ActiveStorage::Blob < ActiveRecord::Base # with users. Instead, the +service_url+ should only be exposed as a redirect from a stable, possibly authenticated URL. # Hiding the +service_url+ behind a redirect also gives you the power to change services without updating all URLs. And # it allows permanent URLs that redirect to the +service_url+ to be cached in the view. - def service_url(expires_in: service.url_expires_in, disposition: :inline, filename: nil, **options) + def service_url(expires_in: ActiveStorage.service_urls_expire_in, disposition: :inline, filename: nil, **options) filename = ActiveStorage::Filename.wrap(filename || self.filename) service.url key, expires_in: expires_in, filename: filename, content_type: content_type, @@ -130,7 +130,7 @@ class ActiveStorage::Blob < ActiveRecord::Base # Returns a URL that can be used to directly upload a file for this blob on the service. This URL is intended to be # short-lived for security and only generated on-demand by the client-side JavaScript responsible for doing the uploading. - def service_url_for_direct_upload(expires_in: service.url_expires_in) + def service_url_for_direct_upload(expires_in: ActiveStorage.service_urls_expire_in) service.url_for_direct_upload key, expires_in: expires_in, content_type: content_type, content_length: byte_size, checksum: checksum end diff --git a/activestorage/app/models/active_storage/variant.rb b/activestorage/app/models/active_storage/variant.rb index 1df36e37d9..056fd2be99 100644 --- a/activestorage/app/models/active_storage/variant.rb +++ b/activestorage/app/models/active_storage/variant.rb @@ -79,7 +79,7 @@ class ActiveStorage::Variant # Use url_for(variant) (or the implied form, like +link_to variant+ or +redirect_to variant+) to get the stable URL # for a variant that points to the ActiveStorage::RepresentationsController, which in turn will use this +service_call+ method # for its redirection. - def service_url(expires_in: service.url_expires_in, disposition: :inline) + def service_url(expires_in: ActiveStorage.service_urls_expire_in, disposition: :inline) service.url key, expires_in: expires_in, disposition: disposition, filename: filename, content_type: content_type end diff --git a/activestorage/lib/active_storage.rb b/activestorage/lib/active_storage.rb index e1deee1d82..928db32691 100644 --- a/activestorage/lib/active_storage.rb +++ b/activestorage/lib/active_storage.rb @@ -49,4 +49,5 @@ module ActiveStorage mattr_accessor :paths, default: {} mattr_accessor :variable_content_types, default: [] mattr_accessor :content_types_to_serve_as_binary, default: [] + mattr_accessor :service_urls_expire_in, default: 5.minutes end diff --git a/activestorage/lib/active_storage/engine.rb b/activestorage/lib/active_storage/engine.rb index 519b9ae283..93c5c55b95 100644 --- a/activestorage/lib/active_storage/engine.rb +++ b/activestorage/lib/active_storage/engine.rb @@ -54,6 +54,7 @@ module ActiveStorage ActiveStorage.variable_content_types = app.config.active_storage.variable_content_types || [] ActiveStorage.content_types_to_serve_as_binary = app.config.active_storage.content_types_to_serve_as_binary || [] + ActiveStorage.service_urls_expire_in = app.config.active_storage.service_urls_expire_in || 5.minutes end end diff --git a/activestorage/lib/active_storage/service.rb b/activestorage/lib/active_storage/service.rb index da1af4f745..fe3ef7503a 100644 --- a/activestorage/lib/active_storage/service.rb +++ b/activestorage/lib/active_storage/service.rb @@ -39,8 +39,6 @@ module ActiveStorage extend ActiveSupport::Autoload autoload :Configurator - class_attribute :url_expires_in, default: 5.minutes - class << self # Configure an Active Storage service by name from a set of configurations, # typically loaded from a YAML file. The Active Storage engine uses this diff --git a/activestorage/test/models/blob_test.rb b/activestorage/test/models/blob_test.rb index 88ce0f868a..40b30acd3e 100644 --- a/activestorage/test/models/blob_test.rb +++ b/activestorage/test/models/blob_test.rb @@ -151,16 +151,16 @@ class ActiveStorage::BlobTest < ActiveSupport::TestCase test "urls allow for custom options" do blob = create_blob(filename: "original.txt") - options = [ + arguments = [ blob.key, - expires_in: blob.service.url_expires_in, + expires_in: ActiveStorage.service_urls_expire_in, disposition: :inline, content_type: blob.content_type, filename: blob.filename, thumb_size: "300x300", thumb_mode: "crop" ] - assert_called_with(blob.service, :url, options) do + assert_called_with(blob.service, :url, arguments) do blob.service_url(thumb_size: "300x300", thumb_mode: "crop") end end diff --git a/guides/source/configuring.md b/guides/source/configuring.md index e89909b614..43c2ac6827 100644 --- a/guides/source/configuring.md +++ b/guides/source/configuring.md @@ -822,6 +822,13 @@ text/javascript image/svg+xml application/postscript application/x-shockwave-fla config.active_storage.logger = ActiveSupport::Logger.new(STDOUT) ``` +* `config.active_storage.service_urls_expire_in` determines the default expiry of URLs generated by: + * `ActiveStorage::Blob#service_url` + * `ActiveStorage::Blob#service_url_for_direct_upload` + * `ActiveStorage::Variant#service_url` + + The default is 5 minutes. + ### Configuring a Database Just about every Rails application will interact with a database. You can connect to the database by setting an environment variable `ENV['DATABASE_URL']` or by using a configuration file called `config/database.yml`. -- cgit v1.2.3