about summary refs log tree commit diff
path: root/app/controllers/media_proxy_controller.rb
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2017-09-16 03:01:45 +0200
committerGitHub <noreply@github.com>2017-09-16 03:01:45 +0200
commit54edb4b853522b322922b65989ddfc5fdf928014 (patch)
treebf169285ecb1e9073ca0c7beaf73ed71f2666521 /app/controllers/media_proxy_controller.rb
parent6c81f9d6e553d2e9346514d1b1c8c214c317fed4 (diff)
When accessing uncached media attachment, redownload it (#4955)
* When accessing uncached media attachment, redownload it

* Prevent re-download of rejected media
Diffstat (limited to 'app/controllers/media_proxy_controller.rb')
-rw-r--r--app/controllers/media_proxy_controller.rb40
1 files changed, 40 insertions, 0 deletions
diff --git a/app/controllers/media_proxy_controller.rb b/app/controllers/media_proxy_controller.rb
new file mode 100644
index 000000000..6a83cf9dc
--- /dev/null
+++ b/app/controllers/media_proxy_controller.rb
@@ -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