has_download.rb 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module ApplicationController::HasDownload
  3. extend ActiveSupport::Concern
  4. included do
  5. around_action do |_controller, block|
  6. subscriber = proc do
  7. policy = ActionDispatch::ContentSecurityPolicy.new
  8. policy.default_src :none
  9. request.content_security_policy = policy
  10. end
  11. ActiveSupport::Notifications.subscribed(subscriber, 'send_file.action_controller') do
  12. ActiveSupport::Notifications.subscribed(subscriber, 'send_data.action_controller') do
  13. block.call
  14. end
  15. end
  16. end
  17. end
  18. private
  19. def file_id
  20. @file_id ||= params[:id]
  21. end
  22. def download_file
  23. @download_file ||= ::ApplicationController::HasDownload::DownloadFile.new(file_id, disposition: sanitized_disposition)
  24. end
  25. def sanitized_disposition
  26. disposition = params.fetch(:disposition, 'inline')
  27. valid_disposition = %w[inline attachment]
  28. return disposition if valid_disposition.include?(disposition)
  29. raise Exceptions::Forbidden, "Invalid disposition #{disposition} requested. Only #{valid_disposition.join(', ')} are valid."
  30. end
  31. end