2016-11-15 10:56:29 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-09-17 11:47:26 -04:00
|
|
|
class MediaController < ApplicationController
|
2017-05-29 12:22:22 -04:00
|
|
|
include Authorization
|
|
|
|
|
2023-08-02 13:32:48 -04:00
|
|
|
skip_before_action :require_functional!, unless: :limited_federation_mode?
|
2019-06-10 06:28:13 -04:00
|
|
|
|
2023-08-02 13:32:48 -04:00
|
|
|
before_action :authenticate_user!, if: :limited_federation_mode?
|
2018-02-16 01:22:20 -05:00
|
|
|
before_action :set_media_attachment
|
|
|
|
before_action :verify_permitted_status!
|
2019-06-19 17:42:38 -04:00
|
|
|
before_action :check_playable, only: :player
|
|
|
|
before_action :allow_iframing, only: :player
|
2016-09-17 11:47:26 -04:00
|
|
|
|
2022-12-15 11:11:58 -05:00
|
|
|
content_security_policy only: :player do |policy|
|
|
|
|
policy.frame_ancestors(false)
|
2018-12-18 10:40:30 -05:00
|
|
|
end
|
|
|
|
|
2016-09-17 11:47:26 -04:00
|
|
|
def show
|
2018-02-16 01:22:20 -05:00
|
|
|
redirect_to @media_attachment.file.url(:original)
|
|
|
|
end
|
|
|
|
|
|
|
|
def player
|
|
|
|
@body_classes = 'player'
|
2016-09-17 11:47:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-02-16 01:22:20 -05:00
|
|
|
def set_media_attachment
|
2021-10-13 09:27:19 -04:00
|
|
|
id = params[:id] || params[:medium_id]
|
|
|
|
return if id.nil?
|
|
|
|
|
|
|
|
scope = MediaAttachment.local.attached
|
|
|
|
# If id is 19 characters long, it's a shortcode, otherwise it's an identifier
|
2023-02-20 20:21:48 -05:00
|
|
|
@media_attachment = id.size == 19 ? scope.find_by!(shortcode: id) : scope.find(id)
|
2017-04-17 14:02:00 -04:00
|
|
|
end
|
|
|
|
|
2018-02-16 01:22:20 -05:00
|
|
|
def verify_permitted_status!
|
|
|
|
authorize @media_attachment.status, :show?
|
2017-05-29 12:22:22 -04:00
|
|
|
rescue Mastodon::NotPermittedError
|
2020-05-03 10:30:36 -04:00
|
|
|
not_found
|
2016-09-17 11:47:26 -04:00
|
|
|
end
|
2019-06-19 17:42:38 -04:00
|
|
|
|
|
|
|
def check_playable
|
|
|
|
not_found unless @media_attachment.larger_media_format?
|
|
|
|
end
|
|
|
|
|
|
|
|
def allow_iframing
|
2023-05-23 08:27:17 -04:00
|
|
|
response.headers.delete('X-Frame-Options')
|
2019-06-19 17:42:38 -04:00
|
|
|
end
|
2016-09-17 11:47:26 -04:00
|
|
|
end
|