#2426 refactor to use predetermined referer
This commit is contained in:
@@ -34,24 +34,8 @@ class UploadsController < ApplicationController
|
||||
end
|
||||
|
||||
def image_proxy
|
||||
if params[:url].blank? || params[:ref].blank?
|
||||
raise "Must specify url and referer"
|
||||
end
|
||||
|
||||
url = URI.parse(params[:url])
|
||||
headers = {
|
||||
"Referer" => params[:ref],
|
||||
"User-Agent" => "#{Danbooru.config.safe_app_name}/#{Danbooru.config.version}"
|
||||
}
|
||||
|
||||
Net::HTTP.start(url.host, url.port) do |http|
|
||||
resp = http.request_get(url.request_uri, headers)
|
||||
if resp.is_a?(Net::HTTPSuccess)
|
||||
send_data resp.body, type: resp.content_type, disposition: "inline"
|
||||
else
|
||||
raise "HTTP error code: #{resp.code} #{resp.message}"
|
||||
end
|
||||
end
|
||||
resp = ImageProxy.get_image(params[:url])
|
||||
send_data resp.body, :type => resp.content_type, :disposition => "inline"
|
||||
end
|
||||
|
||||
def index
|
||||
|
||||
34
app/logical/image_proxy.rb
Normal file
34
app/logical/image_proxy.rb
Normal file
@@ -0,0 +1,34 @@
|
||||
class ImageProxy
|
||||
def self.needs_proxy?(url)
|
||||
fake_referer_for(url).present?
|
||||
end
|
||||
|
||||
def self.fake_referer_for(url)
|
||||
Sources::Site.new(url).strategy.fake_referer
|
||||
end
|
||||
|
||||
def self.get_image(url)
|
||||
if url.blank?
|
||||
raise "Must specify url"
|
||||
end
|
||||
|
||||
if !needs_proxy?(url)
|
||||
raise "Proxy not allowed for this site"
|
||||
end
|
||||
|
||||
uri = URI.parse(url)
|
||||
headers = {
|
||||
"Referer" => fake_referer_for(url),
|
||||
"User-Agent" => "#{Danbooru.config.safe_app_name}/#{Danbooru.config.version}"
|
||||
}
|
||||
|
||||
Net::HTTP.start(uri.host, uri.port) do |http|
|
||||
resp = http.request_get(uri.request_uri, headers)
|
||||
if resp.is_a?(Net::HTTPSuccess)
|
||||
return resp
|
||||
else
|
||||
raise "HTTP error code: #{resp.code} #{resp.message}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -74,6 +74,11 @@ module Sources
|
||||
[image_url]
|
||||
end
|
||||
|
||||
# Should be set to a url for sites that prevent hotlinking, or left nil for sites that don't.
|
||||
def fake_referer
|
||||
nil
|
||||
end
|
||||
|
||||
protected
|
||||
def agent
|
||||
raise NotImplementedError
|
||||
|
||||
@@ -6,7 +6,7 @@ module Sources
|
||||
module Strategies
|
||||
class Pixiv < Base
|
||||
attr_reader :zip_url, :ugoira_frame_data, :ugoira_content_type
|
||||
|
||||
|
||||
MONIKER = '(?:[a-zA-Z0-9_-]+)'
|
||||
TIMESTAMP = '(?:[0-9]{4}/[0-9]{2}/[0-9]{2}/[0-9]{2}/[0-9]{2}/[0-9]{2})'
|
||||
EXT = "(?:jpg|jpeg|png|gif)"
|
||||
@@ -35,6 +35,10 @@ module Sources
|
||||
@pixiv_moniker
|
||||
end
|
||||
|
||||
def fake_referer
|
||||
"http://www.pixiv.net"
|
||||
end
|
||||
|
||||
def has_artist_commentary?
|
||||
@artist_commentary_desc.present?
|
||||
end
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
<% if params[:url] %>
|
||||
<%= image_tag(image_proxy_uploads_path(:url => @normalized_url, :ref => params[:ref]), :title => "Preview", :id => "image") %>
|
||||
<% if ImageProxy.needs_proxy?(@normalized_url) %>
|
||||
<%= image_tag(image_proxy_uploads_path(:url => @normalized_url), :title => "Preview", :id => "image") %>
|
||||
<% else %>
|
||||
<%= image_tag(@normalized_url, :title => "Preview", :id => "image") %>
|
||||
<% end %>
|
||||
|
||||
<ul>
|
||||
<% if @remote_size %>
|
||||
|
||||
Reference in New Issue
Block a user