1
0

media_proxy_controller.rb 980 B

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