2017-09-16 03:01:45 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class MediaProxyController < ApplicationController
|
|
|
|
include RoutingHelper
|
|
|
|
|
2019-06-10 12:28:13 +02:00
|
|
|
skip_before_action :store_current_location
|
|
|
|
|
2019-07-30 11:10:46 +02:00
|
|
|
before_action :authenticate_user!, if: :whitelist_mode?
|
|
|
|
|
2019-08-18 18:04:18 +02:00
|
|
|
rescue_from ActiveRecord::RecordInvalid, with: :not_found
|
|
|
|
|
2017-09-16 03:01:45 +02:00
|
|
|
def show
|
|
|
|
RedisLock.acquire(lock_options) do |lock|
|
|
|
|
if lock.acquired?
|
|
|
|
@media_attachment = MediaAttachment.remote.find(params[:id])
|
|
|
|
redownload! if @media_attachment.needs_redownload? && !reject_media?
|
2018-05-16 12:29:45 +02:00
|
|
|
else
|
|
|
|
raise Mastodon::RaceConditionError
|
2017-09-16 03:01:45 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
redirect_to full_asset_url(@media_attachment.file.url(version))
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def redownload!
|
|
|
|
@media_attachment.file_remote_url = @media_attachment.remote_url
|
2017-09-19 06:53:16 +02:00
|
|
|
@media_attachment.created_at = Time.now.utc
|
2017-09-16 03:01:45 +02:00
|
|
|
@media_attachment.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
def version
|
|
|
|
if request.path.ends_with?('/small')
|
|
|
|
:small
|
|
|
|
else
|
|
|
|
:original
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def lock_options
|
|
|
|
{ redis: Redis.current, key: "media_download:#{params[:id]}" }
|
|
|
|
end
|
|
|
|
|
|
|
|
def reject_media?
|
2019-06-22 00:13:10 +02:00
|
|
|
DomainBlock.reject_media?(@media_attachment.account.domain)
|
2017-09-16 03:01:45 +02:00
|
|
|
end
|
|
|
|
end
|