media_proxy_controller.rb 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 :require_functional!
  8. before_action :authenticate_user!, if: :limited_federation_mode?
  9. rescue_from ActiveRecord::RecordInvalid, with: :not_found
  10. rescue_from Mastodon::UnexpectedResponseError, with: :not_found
  11. rescue_from Mastodon::NotPermittedError, with: :not_found
  12. rescue_from HTTP::TimeoutError, HTTP::ConnectionError, OpenSSL::SSL::SSLError, with: :internal_server_error
  13. def show
  14. with_redis_lock("media_download:#{params[:id]}") do
  15. @media_attachment = MediaAttachment.remote.attached.find(params[:id])
  16. authorize @media_attachment.status, :show?
  17. redownload! if @media_attachment.needs_redownload? && !reject_media?
  18. end
  19. redirect_to full_asset_url(@media_attachment.file.url(version)), allow_other_host: true
  20. end
  21. private
  22. def redownload!
  23. @media_attachment.download_file!
  24. @media_attachment.created_at = Time.now.utc
  25. @media_attachment.save!
  26. end
  27. def version
  28. if request.path.end_with?('/small')
  29. :small
  30. else
  31. :original
  32. end
  33. end
  34. def reject_media?
  35. DomainBlock.reject_media?(@media_attachment.account.domain)
  36. end
  37. end