From e33c3cd8ccbecaca6c6af0438956431b02cb3fb2 Mon Sep 17 00:00:00 2001 From: George Claghorn Date: Thu, 16 Aug 2018 01:41:15 -0400 Subject: Extract ActiveStorage::SetCurrent Provide a handy concern for custom Active Storage controllers that can't inherit from ActiveStorage::BaseController. --- .../app/controllers/active_storage/base_controller.rb | 8 +++----- .../controllers/concerns/active_storage/set_current.rb | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 activestorage/app/controllers/concerns/active_storage/set_current.rb (limited to 'activestorage/app/controllers') diff --git a/activestorage/app/controllers/active_storage/base_controller.rb b/activestorage/app/controllers/active_storage/base_controller.rb index 59312ac8df..b27d2bd8aa 100644 --- a/activestorage/app/controllers/active_storage/base_controller.rb +++ b/activestorage/app/controllers/active_storage/base_controller.rb @@ -1,10 +1,8 @@ # frozen_string_literal: true -# The base controller for all ActiveStorage controllers. +# The base class for all Active Storage controllers. class ActiveStorage::BaseController < ActionController::Base - protect_from_forgery with: :exception + include ActiveStorage::SetCurrent - before_action do - ActiveStorage::Current.host = request.base_url - end + protect_from_forgery with: :exception end diff --git a/activestorage/app/controllers/concerns/active_storage/set_current.rb b/activestorage/app/controllers/concerns/active_storage/set_current.rb new file mode 100644 index 0000000000..597afe7064 --- /dev/null +++ b/activestorage/app/controllers/concerns/active_storage/set_current.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +# Sets the ActiveStorage::Current.host attribute, which the disk service uses to generate URLs. +# Include this concern in custom controllers that call ActiveStorage::Blob#service_url, +# ActiveStorage::Variant#service_url, or ActiveStorage::Preview#service_url so the disk service can +# generate URLs using the same host, protocol, and base path as the current request. +module ActiveStorage::SetCurrent + extend ActiveSupport::Concern + + included do + before_action do + ActiveStorage::Current.host = request.base_url + end + end +end -- cgit v1.2.3 From 22efb2ec49087827ca1cb28a8bad9f016800c591 Mon Sep 17 00:00:00 2001 From: Cameron Bothner Date: Sat, 18 Aug 2018 14:03:52 -0400 Subject: Respond with 404 in ActiveStorage::DiskController#show when file missing `ActiveStorage::DiskController#show` generates a 404 Not Found response when the requested file is missing from the disk service. It previously raised `Errno::ENOENT`. --- activestorage/app/controllers/active_storage/disk_controller.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'activestorage/app/controllers') diff --git a/activestorage/app/controllers/active_storage/disk_controller.rb b/activestorage/app/controllers/active_storage/disk_controller.rb index 75cc11d6ff..7bd641ab9a 100644 --- a/activestorage/app/controllers/active_storage/disk_controller.rb +++ b/activestorage/app/controllers/active_storage/disk_controller.rb @@ -13,6 +13,8 @@ class ActiveStorage::DiskController < ActiveStorage::BaseController else head :not_found end + rescue Errno::ENOENT + head :not_found end def update -- cgit v1.2.3