From 4712e2361199c0becc216d392d3d84666bad076b Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 4 Jul 2017 18:11:06 +0200 Subject: Fix up DiskController and add basic testing --- lib/active_file/disk_controller.rb | 13 +++++++++++-- lib/active_file/site/disk_site.rb | 2 +- 2 files changed, 12 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/active_file/disk_controller.rb b/lib/active_file/disk_controller.rb index 5140c91f0a..d778cf066f 100644 --- a/lib/active_file/disk_controller.rb +++ b/lib/active_file/disk_controller.rb @@ -1,8 +1,17 @@ +require "action_controller" +require "active_file/blob" +require "active_file/verified_key_with_expiration" + +require "active_support/core_ext/object/inclusion" + class ActiveFile::DiskController < ActionController::Base def show if key = decode_verified_key blob = ActiveFile::Blob.find_by!(key: key) - send_data blob.download, filename: blob.filename, type: blob.content_type, disposition: disposition_param + + 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 @@ -10,7 +19,7 @@ class ActiveFile::DiskController < ActionController::Base private def decode_verified_key - ActiveFile::Site::DiskSite::VerifiedKeyWithExpiration.decode(params[:encoded_key]) + ActiveFile::VerifiedKeyWithExpiration.decode(params[:encoded_key]) end def disposition_param diff --git a/lib/active_file/site/disk_site.rb b/lib/active_file/site/disk_site.rb index ec60175bbf..7916a642c0 100644 --- a/lib/active_file/site/disk_site.rb +++ b/lib/active_file/site/disk_site.rb @@ -40,7 +40,7 @@ class ActiveFile::Site::DiskSite < ActiveFile::Site def url(key, expires_in:, disposition:, filename:) verified_key_with_expiration = ActiveFile::VerifiedKeyWithExpiration.encode(key, expires_in: expires_in) - if defined?(Rails) + if defined?(Rails) && defined?(Rails.application) Rails.application.routes.url_helpers.rails_disk_blob_path(verified_key_with_expiration, disposition: disposition) else "/rails/blobs/#{verified_key_with_expiration}?disposition=#{disposition}" -- cgit v1.2.3