You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.3 KiB

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