gab-social/app/controllers/media_proxy_controller.rb

47 lines
1.1 KiB
Ruby
Raw Normal View History

2019-07-02 08:10:25 +01:00
# frozen_string_literal: true
class MediaProxyController < ApplicationController
include RoutingHelper
skip_before_action :store_current_location
2019-07-02 08:10:25 +01:00
def show
RedisLock.acquire(lock_options) do |lock|
if lock.acquired?
2020-12-22 17:49:40 +00:00
@media_attachment = MediaAttachment.find(params[:id])
2019-07-02 08:10:25 +01:00
redownload! if @media_attachment.needs_redownload? && !reject_media?
else
raise GabSocial::RaceConditionError
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.created_at = Time.now.utc
@media_attachment.save!
end
def version
if request.path.ends_with?('/small')
:small
2020-12-22 17:49:40 +00:00
elsif request.path.ends_with?('/playable')
:playable
2019-07-02 08:10:25 +01:00
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