media_controller.rb 1.2 KB

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