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.

55 lines
1.5 KiB

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