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