From c624df326a4ef36919a5195a3c5509fab97dcba3 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Thu, 6 Jul 2017 11:33:29 +0200 Subject: ActiveVault -> ActiveStorage MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Yaroslav agreed to hand over the gem name ❤️ --- lib/active_storage/disk_controller.rb | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 lib/active_storage/disk_controller.rb (limited to 'lib/active_storage/disk_controller.rb') diff --git a/lib/active_storage/disk_controller.rb b/lib/active_storage/disk_controller.rb new file mode 100644 index 0000000000..3eba86c213 --- /dev/null +++ b/lib/active_storage/disk_controller.rb @@ -0,0 +1,28 @@ +require "action_controller" +require "active_storage/blob" +require "active_storage/verified_key_with_expiration" + +require "active_support/core_ext/object/inclusion" + +class ActiveStorage::DiskController < ActionController::Base + def show + if key = decode_verified_key + blob = ActiveStorage::Blob.find_by!(key: key) + + if stale?(etag: blob.checksum) + send_data blob.download, filename: blob.filename, type: blob.content_type, disposition: disposition_param + end + else + head :not_found + end + end + + private + def decode_verified_key + ActiveStorage::VerifiedKeyWithExpiration.decode(params[:encoded_key]) + end + + def disposition_param + params[:disposition].presence_in(%w( inline attachment )) || 'inline' + end +end -- cgit v1.2.3