]> cat aescling's git repositories - mastodon.git/blob - app/controllers/media_proxy_controller.rb
Fix redirecting non-functional accounts on public pages (#11978)
[mastodon.git] / app / controllers / media_proxy_controller.rb
1 # frozen_string_literal: true
2
3 class MediaProxyController < ApplicationController
4 include RoutingHelper
5
6 skip_before_action :store_current_location
7 skip_before_action :require_functional!
8
9 before_action :authenticate_user!, if: :whitelist_mode?
10
11 rescue_from ActiveRecord::RecordInvalid, with: :not_found
12 rescue_from Mastodon::UnexpectedResponseError, with: :not_found
13 rescue_from HTTP::TimeoutError, HTTP::ConnectionError, OpenSSL::SSL::SSLError, with: :internal_server_error
14
15 def show
16 RedisLock.acquire(lock_options) do |lock|
17 if lock.acquired?
18 @media_attachment = MediaAttachment.remote.find(params[:id])
19 redownload! if @media_attachment.needs_redownload? && !reject_media?
20 else
21 raise Mastodon::RaceConditionError
22 end
23 end
24
25 redirect_to full_asset_url(@media_attachment.file.url(version))
26 end
27
28 private
29
30 def redownload!
31 @media_attachment.file_remote_url = @media_attachment.remote_url
32 @media_attachment.created_at = Time.now.utc
33 @media_attachment.save!
34 end
35
36 def version
37 if request.path.ends_with?('/small')
38 :small
39 else
40 :original
41 end
42 end
43
44 def lock_options
45 { redis: Redis.current, key: "media_download:#{params[:id]}" }
46 end
47
48 def reject_media?
49 DomainBlock.reject_media?(@media_attachment.account.domain)
50 end
51 end
This page took 0.09747 seconds and 4 git commands to generate.