Merge pull request #3612 from evazion/fix-iqdb-errors

Improve IQDB error handling
This commit is contained in:
Albert Yi
2018-04-09 10:53:40 -07:00
committed by GitHub
3 changed files with 43 additions and 69 deletions

View File

@@ -39,6 +39,11 @@ $(function() {
location.reload(); location.reload();
}); });
}); });
// triggered by rails when a `link_to ..., remote: true` call fails.
$(document).on("ajax:error", function (event, xhr, status, error) {
Danbooru.error("Error: " + xhr.status + " " + xhr.statusText);
});
}); });
var Danbooru = {}; var Danbooru = {};

View File

@@ -3,31 +3,16 @@ class IqdbQueriesController < ApplicationController
respond_to :html, :json, :xml respond_to :html, :json, :xml
def show def show
server_check @results = find_similar
if params[:url]
create_by_url respond_with(@results) do |fmt|
respond_with(@results) do |fmt| fmt.html { render :layout => false, :action => "create_by_url" }
fmt.html { render :layout => false, :action => "create_by_url" } fmt.js { render :layout => false, :action => "create_by_post" }
end
elsif params[:post_id]
create_by_post
respond_with(@results) do |fmt|
fmt.js { render :layout => false, :action => "create_by_post" }
end
else
render :nothing => true, :status => 422
end end
end end
def check def check
server_check @results = find_similar
if params[:url].present?
create_by_url
elsif params[:post_id].present?
create_by_post
else
@results = []
end
respond_with(@results) respond_with(@results)
end end
@@ -35,22 +20,10 @@ class IqdbQueriesController < ApplicationController
alias_method :create, :show alias_method :create, :show
protected protected
def server_check def find_similar
if !Danbooru.config.iqdbs_server return [] if params[:url].blank? && params[:post_id].blank?
raise NotImplementedError.new("the IQDBs service isn't configured. Similarity searches are not available.")
end
end
def create_by_url params[:url] = Post.find(params[:post_id]).preview_file_url if params[:post_id].present?
@download = Iqdb::Download.new(params[:url]) Iqdb::Download.find_similar(params[:url])
@download.find_similar
@results = @download.matches
end
def create_by_post
@post = Post.find(params[:post_id])
@download = Iqdb::Download.new(@post.preview_file_url)
@download.find_similar
@results = @download.matches
end end
end end

View File

@@ -1,12 +1,12 @@
module Iqdb module Iqdb
class Download class Download
attr_reader :source, :download, :matches class Error < StandardError; end
def initialize(source) def self.enabled?
@source = source Danbooru.config.iqdbs_server.present? && Danbooru.config.iqdbs_auth_key.present?
end end
def get_referer(url) def self.get_referer(url)
headers = {} headers = {}
datums = {} datums = {}
@@ -17,37 +17,33 @@ module Iqdb
[url, headers["Referer"]] [url, headers["Referer"]]
end end
def find_similar def self.find_similar(source)
if Danbooru.config.iqdbs_server raise NotImplementedError, "the IQDBs service isn't configured. Similarity searches are not available." unless enabled?
url, ref = get_referer(source)
params = {
"key" => Danbooru.config.iqdbs_auth_key,
"url" => url,
"ref" => ref
}
uri = URI.parse("#{Danbooru.config.iqdbs_server}/similar")
uri.query = URI.encode_www_form(params)
resp = HTTParty.get(uri, Danbooru.config.httparty_options) url, ref = get_referer(source)
if resp.success? params = {
json = JSON.parse(resp.body) "key" => Danbooru.config.iqdbs_auth_key,
if json.is_a?(Array) "url" => url,
post_ids = json.map { |match| match["post_id"] } "ref" => ref
posts = Post.find(post_ids) }
uri = URI.parse("#{Danbooru.config.iqdbs_server}/similar")
uri.query = URI.encode_www_form(params)
@matches = json.map do |match| resp = HTTParty.get(uri, Danbooru.config.httparty_options)
post = posts.find { |post| post.id == match["post_id"] } raise "HTTP error code: #{resp.code} #{resp.message}" unless resp.success?
match.with_indifferent_access.merge({ post: post })
end json = JSON.parse(resp.body)
else raise "IQDB error: #{json["error"]}" unless json.is_a?(Array)
@matches = []
end post_ids = json.map { |match| match["post_id"] }
else posts = Post.find(post_ids)
raise "HTTP error code: #{resp.code} #{resp.message}"
end json.map do |match|
else post = posts.find { |post| post.id == match["post_id"] }
raise NotImplementedError match.with_indifferent_access.merge({ post: post })
end end
rescue => e
raise Error, { message: e.message, iqdb_response: json }
end end
end end
end end