image proxy: replace HTTParty with Danbooru::Http.

This commit is contained in:
evazion
2020-06-21 14:01:38 -05:00
parent 7e471fe223
commit 29a5f7dfc8
3 changed files with 19 additions and 9 deletions

View File

@@ -21,7 +21,7 @@ class UploadsController < ApplicationController
def image_proxy
authorize Upload
resp = ImageProxy.get_image(params[:url])
send_data resp.body, :type => resp.content_type, :disposition => "inline"
send_data resp.body, type: resp.mime_type, disposition: "inline"
end
def index

View File

@@ -1,4 +1,6 @@
class ImageProxy
class Error < StandardError; end
def self.needs_proxy?(url)
fake_referer_for(url).present?
end
@@ -8,16 +10,13 @@ class ImageProxy
end
def self.get_image(url)
if url.blank?
raise "Must specify url"
end
raise Error, "URL not present" unless url.present?
raise Error, "Proxy not allowed for this url (url=#{url})" unless needs_proxy?(url)
if !needs_proxy?(url)
raise "Proxy not allowed for this site"
end
referer = fake_referer_for(url)
response = Danbooru::Http.headers(Referer: referer).get(url)
raise Error, "Couldn't proxy image (code=#{response.status}, url=#{url})" unless response.status.success?
response = HTTParty.get(url, Danbooru.config.httparty_options.deep_merge(headers: {"Referer" => fake_referer_for(url)}))
raise "HTTP error code: #{response.code} #{response.message}" unless response.success?
response
end
end

View File

@@ -18,6 +18,17 @@ class UploadsControllerTest < ActionDispatch::IntegrationTest
mock_iqdb_service!
end
context "image proxy action" do
should "work" do
url = "https://i.pximg.net/img-original/img/2017/11/21/17/06/44/65985331_p0.png"
get_auth image_proxy_uploads_path, @user, params: { url: url }
assert_response :success
assert_equal("image/png", response.media_type)
assert_equal(15_573, response.body.size)
end
end
context "batch action" do
context "for twitter galleries" do
should "render" do