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.

44 lines
1009 B

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