From 3bfd4b4a92be1e34f8439a5c42bb329b41c7c5da Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Thu, 14 Aug 2025 17:47:34 -0400 Subject: [PATCH] Move media proxy "redownload" check to before_action --- app/controllers/media_proxy_controller.rb | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/app/controllers/media_proxy_controller.rb b/app/controllers/media_proxy_controller.rb index d55b90ad887..f07d77cb9ec 100644 --- a/app/controllers/media_proxy_controller.rb +++ b/app/controllers/media_proxy_controller.rb @@ -10,6 +10,7 @@ class MediaProxyController < ApplicationController before_action :authenticate_user!, if: :limited_federation_mode? before_action :set_media_attachment + before_action :redownload_media, if: :redownload_required? rescue_from ActiveRecord::RecordInvalid, with: :not_found rescue_from Mastodon::UnexpectedResponseError, with: :not_found @@ -17,13 +18,6 @@ class MediaProxyController < ApplicationController rescue_from(*Mastodon::HTTP_CONNECTION_ERRORS, with: :internal_server_error) def show - if @media_attachment.needs_redownload? && !reject_media? - with_redis_lock("media_download:#{params[:id]}") do - @media_attachment.reload # Reload once we have acquired a lock, in case the file was downloaded in the meantime - redownload! if @media_attachment.needs_redownload? - end - end - if requires_file_streaming? send_file(media_attachment_file.path, type: media_attachment_file.instance_read(:content_type), disposition: 'inline') else @@ -33,6 +27,18 @@ class MediaProxyController < ApplicationController private + def redownload_required? + @media_attachment.needs_redownload? && !reject_media? + end + + def redownload_media + with_redis_lock("media_download:#{params[:id]}") do + # Reload within lock in case the file was downloaded in the meantime + @media_attachment.reload + redownload! if @media_attachment.needs_redownload? + end + end + def set_media_attachment @media_attachment = MediaAttachment.attached.find(params[:id]) authorize @media_attachment, :download?