2017-09-16 01:01:45 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class MediaProxyController < ApplicationController
|
|
|
|
include RoutingHelper
|
2020-07-07 13:26:51 +00:00
|
|
|
include Authorization
|
2022-04-28 15:47:34 +00:00
|
|
|
include Redisable
|
2022-05-12 22:02:35 +00:00
|
|
|
include Lockable
|
2017-09-16 01:01:45 +00:00
|
|
|
|
2019-09-27 23:33:27 +00:00
|
|
|
skip_before_action :require_functional!
|
2019-06-10 10:28:13 +00:00
|
|
|
|
2023-08-02 17:32:48 +00:00
|
|
|
before_action :authenticate_user!, if: :limited_federation_mode?
|
2019-07-30 09:10:46 +00:00
|
|
|
|
2019-08-18 16:04:18 +00:00
|
|
|
rescue_from ActiveRecord::RecordInvalid, with: :not_found
|
2019-09-11 23:51:12 +00:00
|
|
|
rescue_from Mastodon::UnexpectedResponseError, with: :not_found
|
2020-07-07 13:26:51 +00:00
|
|
|
rescue_from Mastodon::NotPermittedError, with: :not_found
|
2019-09-11 23:51:12 +00:00
|
|
|
rescue_from HTTP::TimeoutError, HTTP::ConnectionError, OpenSSL::SSL::SSLError, with: :internal_server_error
|
2019-08-18 16:04:18 +00:00
|
|
|
|
2017-09-16 01:01:45 +00:00
|
|
|
def show
|
2023-05-02 16:16:07 +00:00
|
|
|
with_redis_lock("media_download:#{params[:id]}") do
|
2022-05-12 22:02:35 +00:00
|
|
|
@media_attachment = MediaAttachment.remote.attached.find(params[:id])
|
|
|
|
authorize @media_attachment.status, :show?
|
|
|
|
redownload! if @media_attachment.needs_redownload? && !reject_media?
|
2017-09-16 01:01:45 +00:00
|
|
|
end
|
|
|
|
|
2023-03-25 23:38:32 +00:00
|
|
|
redirect_to full_asset_url(@media_attachment.file.url(version)), allow_other_host: true
|
2017-09-16 01:01:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def redownload!
|
2020-06-29 11:56:55 +00:00
|
|
|
@media_attachment.download_file!
|
|
|
|
@media_attachment.created_at = Time.now.utc
|
2017-09-16 01:01:45 +00:00
|
|
|
@media_attachment.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
def version
|
2021-02-19 08:56:14 +00:00
|
|
|
if request.path.end_with?('/small')
|
2017-09-16 01:01:45 +00:00
|
|
|
:small
|
|
|
|
else
|
|
|
|
:original
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def reject_media?
|
2019-06-21 22:13:10 +00:00
|
|
|
DomainBlock.reject_media?(@media_attachment.account.domain)
|
2017-09-16 01:01:45 +00:00
|
|
|
end
|
|
|
|
end
|