When accessing uncached media attachment, redownload it (#4955)
* When accessing uncached media attachment, redownload it * Prevent re-download of rejected media
This commit is contained in:
parent
6c81f9d6e5
commit
54edb4b853
40
app/controllers/media_proxy_controller.rb
Normal file
40
app/controllers/media_proxy_controller.rb
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class MediaProxyController < ApplicationController
|
||||||
|
include RoutingHelper
|
||||||
|
|
||||||
|
def show
|
||||||
|
RedisLock.acquire(lock_options) do |lock|
|
||||||
|
if lock.acquired?
|
||||||
|
@media_attachment = MediaAttachment.remote.find(params[:id])
|
||||||
|
redownload! if @media_attachment.needs_redownload? && !reject_media?
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
redirect_to full_asset_url(@media_attachment.file.url(version))
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def redownload!
|
||||||
|
@media_attachment.file_remote_url = @media_attachment.remote_url
|
||||||
|
@media_attachment.touch(:created_at)
|
||||||
|
@media_attachment.save!
|
||||||
|
end
|
||||||
|
|
||||||
|
def version
|
||||||
|
if request.path.ends_with?('/small')
|
||||||
|
:small
|
||||||
|
else
|
||||||
|
:original
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def lock_options
|
||||||
|
{ redis: Redis.current, key: "media_download:#{params[:id]}" }
|
||||||
|
end
|
||||||
|
|
||||||
|
def reject_media?
|
||||||
|
DomainBlock.find_by(domain: @media_attachment.account.domain)&.reject_media?
|
||||||
|
end
|
||||||
|
end
|
|
@ -56,15 +56,21 @@ class MediaAttachment < ApplicationRecord
|
||||||
|
|
||||||
validates :account, presence: true
|
validates :account, presence: true
|
||||||
|
|
||||||
scope :attached, -> { where.not(status_id: nil) }
|
scope :attached, -> { where.not(status_id: nil) }
|
||||||
scope :unattached, -> { where(status_id: nil) }
|
scope :unattached, -> { where(status_id: nil) }
|
||||||
scope :local, -> { where(remote_url: '') }
|
scope :local, -> { where(remote_url: '') }
|
||||||
|
scope :remote, -> { where.not(remote_url: '') }
|
||||||
|
|
||||||
default_scope { order(id: :asc) }
|
default_scope { order(id: :asc) }
|
||||||
|
|
||||||
def local?
|
def local?
|
||||||
remote_url.blank?
|
remote_url.blank?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def needs_redownload?
|
||||||
|
file.blank? && remote_url.present?
|
||||||
|
end
|
||||||
|
|
||||||
def to_param
|
def to_param
|
||||||
shortcode
|
shortcode
|
||||||
end
|
end
|
||||||
|
|
|
@ -7,11 +7,19 @@ class REST::MediaAttachmentSerializer < ActiveModel::Serializer
|
||||||
:remote_url, :text_url, :meta
|
:remote_url, :text_url, :meta
|
||||||
|
|
||||||
def url
|
def url
|
||||||
full_asset_url(object.file.url(:original))
|
if object.needs_redownload?
|
||||||
|
media_proxy_url(object.id, :original)
|
||||||
|
else
|
||||||
|
full_asset_url(object.file.url(:original))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def preview_url
|
def preview_url
|
||||||
full_asset_url(object.file.url(:small))
|
if object.needs_redownload?
|
||||||
|
media_proxy_url(object.id, :small)
|
||||||
|
else
|
||||||
|
full_asset_url(object.file.url(:small))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def text_url
|
def text_url
|
||||||
|
|
|
@ -98,6 +98,8 @@ Rails.application.routes.draw do
|
||||||
resources :media, only: [:show]
|
resources :media, only: [:show]
|
||||||
resources :tags, only: [:show]
|
resources :tags, only: [:show]
|
||||||
|
|
||||||
|
get '/media_proxy/:id/(*any)', to: 'media_proxy#show', as: :media_proxy
|
||||||
|
|
||||||
# Remote follow
|
# Remote follow
|
||||||
resource :authorize_follow, only: [:show, :create]
|
resource :authorize_follow, only: [:show, :create]
|
||||||
resource :share, only: [:show, :create]
|
resource :share, only: [:show, :create]
|
||||||
|
|
|
@ -83,7 +83,6 @@ namespace :mastodon do
|
||||||
|
|
||||||
MediaAttachment.where.not(remote_url: '').where('created_at < ?', time_ago).find_each do |media|
|
MediaAttachment.where.not(remote_url: '').where('created_at < ?', time_ago).find_each do |media|
|
||||||
media.file.destroy
|
media.file.destroy
|
||||||
media.type = :unknown
|
|
||||||
media.save
|
media.save
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Reference in a new issue