You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.3 KiB

  1. # frozen_string_literal: true
  2. class MediaController < ApplicationController
  3. include Authorization
  4. skip_before_action :store_current_location
  5. skip_before_action :require_functional!, unless: :whitelist_mode?
  6. before_action :authenticate_user!, if: :whitelist_mode?
  7. before_action :set_media_attachment
  8. before_action :verify_permitted_status!
  9. before_action :check_playable, only: :player
  10. before_action :allow_iframing, only: :player
  11. before_action :set_pack, only: :player
  12. content_security_policy only: :player do |policy|
  13. policy.frame_ancestors(false)
  14. end
  15. def show
  16. redirect_to @media_attachment.file.url(:original)
  17. end
  18. def player
  19. @body_classes = 'player'
  20. end
  21. private
  22. def set_media_attachment
  23. id = params[:id] || params[:medium_id]
  24. return if id.nil?
  25. scope = MediaAttachment.local.attached
  26. # If id is 19 characters long, it's a shortcode, otherwise it's an identifier
  27. @media_attachment = id.size == 19 ? scope.find_by!(shortcode: id) : scope.find(id)
  28. end
  29. def verify_permitted_status!
  30. authorize @media_attachment.status, :show?
  31. rescue Mastodon::NotPermittedError
  32. not_found
  33. end
  34. def check_playable
  35. not_found unless @media_attachment.larger_media_format?
  36. end
  37. def allow_iframing
  38. response.headers['X-Frame-Options'] = 'ALLOWALL'
  39. end
  40. def set_pack
  41. use_pack 'public'
  42. end
  43. end