This commit is contained in:
Matt Jankowski 2025-09-03 20:07:43 +00:00 committed by GitHub
commit c6836bdd23
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -10,6 +10,7 @@ class MediaProxyController < ApplicationController
before_action :authenticate_user!, if: :limited_federation_mode? before_action :authenticate_user!, if: :limited_federation_mode?
before_action :set_media_attachment before_action :set_media_attachment
before_action :redownload_media, if: :redownload_required?
rescue_from ActiveRecord::RecordInvalid, with: :not_found rescue_from ActiveRecord::RecordInvalid, with: :not_found
rescue_from Mastodon::UnexpectedResponseError, 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) rescue_from(*Mastodon::HTTP_CONNECTION_ERRORS, with: :internal_server_error)
def show 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? if requires_file_streaming?
send_file(media_attachment_file.path, type: media_attachment_file.instance_read(:content_type), disposition: 'inline') send_file(media_attachment_file.path, type: media_attachment_file.instance_read(:content_type), disposition: 'inline')
else else
@ -33,6 +27,18 @@ class MediaProxyController < ApplicationController
private 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 def set_media_attachment
@media_attachment = MediaAttachment.attached.find(params[:id]) @media_attachment = MediaAttachment.attached.find(params[:id])
authorize @media_attachment, :download? authorize @media_attachment, :download?