media_proxy_controller.rb 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. def show
  7. RedisLock.acquire(lock_options) do |lock|
  8. if lock.acquired?
  9. @media_attachment = MediaAttachment.remote.find(params[:id])
  10. redownload! if @media_attachment.needs_redownload? && !reject_media?
  11. else
  12. raise Mastodon::RaceConditionError
  13. end
  14. end
  15. redirect_to full_asset_url(@media_attachment.file.url(version))
  16. end
  17. private
  18. def redownload!
  19. @media_attachment.file_remote_url = @media_attachment.remote_url
  20. @media_attachment.created_at = Time.now.utc
  21. @media_attachment.save!
  22. end
  23. def version
  24. if request.path.ends_with?('/small')
  25. :small
  26. else
  27. :original
  28. end
  29. end
  30. def lock_options
  31. { redis: Redis.current, key: "media_download:#{params[:id]}" }
  32. end
  33. def reject_media?
  34. DomainBlock.reject_media?(@media_attachment.account.domain)
  35. end
  36. end