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.

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