download_file.rb 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class ApplicationController::HasDownload::DownloadFile < SimpleDelegator
  3. attr_reader :requested_disposition
  4. def initialize(id, disposition: 'inline')
  5. @requested_disposition = disposition
  6. super(Store.find(id))
  7. end
  8. def disposition
  9. return 'attachment' if forcibly_download_as_binary? || !allowed_inline?
  10. requested_disposition
  11. end
  12. def content_type
  13. return ActiveStorage.binary_content_type if forcibly_download_as_binary?
  14. file_content_type
  15. end
  16. def content(view_type)
  17. return __getobj__.content if view_type.blank? || !preferences[:resizable]
  18. return content_inline if content_inline? && view_type == 'inline'
  19. return content_preview if content_preview? && view_type == 'preview'
  20. __getobj__.content
  21. end
  22. private
  23. def allowed_inline?
  24. ActiveStorage.content_types_allowed_inline.include?(content_type)
  25. end
  26. def forcibly_download_as_binary?
  27. ActiveStorage.content_types_to_serve_as_binary.include?(file_content_type)
  28. end
  29. def file_content_type
  30. @file_content_type ||= preferences['Content-Type'] || preferences['Mime-Type'] || ActiveStorage.binary_content_type
  31. end
  32. def content_inline?
  33. preferences[:content_inline] == true
  34. end
  35. def content_preview?
  36. preferences[:content_preview] == true
  37. end
  38. end