media_proxy_controller.rb 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # frozen_string_literal: true
  2. class MediaProxyController < ApplicationController
  3. include RoutingHelper
  4. include Authorization
  5. include Redisable
  6. include Lockable
  7. skip_before_action :store_current_location
  8. skip_before_action :require_functional!
  9. before_action :authenticate_user!, if: :whitelist_mode?
  10. rescue_from ActiveRecord::RecordInvalid, with: :not_found
  11. rescue_from Mastodon::UnexpectedResponseError, with: :not_found
  12. rescue_from Mastodon::NotPermittedError, with: :not_found
  13. rescue_from HTTP::TimeoutError, HTTP::ConnectionError, OpenSSL::SSL::SSLError, with: :internal_server_error
  14. def show
  15. with_lock("media_download:#{params[:id]}") do
  16. @media_attachment = MediaAttachment.remote.attached.find(params[:id])
  17. authorize @media_attachment.status, :show?
  18. redownload! if @media_attachment.needs_redownload? && !reject_media?
  19. end
  20. redirect_to full_asset_url(@media_attachment.file.url(version))
  21. end
  22. private
  23. def redownload!
  24. @media_attachment.download_file!
  25. @media_attachment.created_at = Time.now.utc
  26. @media_attachment.save!
  27. end
  28. def version
  29. if request.path.end_with?('/small')
  30. :small
  31. else
  32. :original
  33. end
  34. end
  35. def reject_media?
  36. DomainBlock.reject_media?(@media_attachment.account.domain)
  37. end
  38. end