1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/app/controllers/active_storage/disk_controller.rb
David Heinemeier Hansson 9e81741b34 Disk controller must rely on key alone
Otherwise it can't be used to display variants. It's better anyway since all other services won't know about blobs either. Better simulation. Closes #71
2017-07-23 10:56:53 -05:00

33 lines
1.1 KiB
Ruby

# This controller is a wrapper around local file downloading. It allows you to
# make abstraction of the URL generation logic and to serve files with expiry
# if you are using the +Disk+ service.
#
# By default, mounting the Active Storage engine inside your application will
# define a +/rails/blobs/:encoded_key/*filename+ route that will reference this
# controller's +show+ action and will be used to serve local files.
#
# A URL for an attachment can be generated through its +#url+ method, that
# will use the aforementioned route.
class ActiveStorage::DiskController < ActionController::Base
def show
if key = decode_verified_key
# FIXME: Find a way to set the correct content type
send_data disk_service.download(key), filename: params[:filename], disposition: disposition_param
else
head :not_found
end
end
private
def disk_service
ActiveStorage::Blob.service
end
def decode_verified_key
ActiveStorage::VerifiedKeyWithExpiration.decode(params[:encoded_key])
end
def disposition_param
params[:disposition].presence_in(%w( inline attachment )) || "inline"
end
end