iqdb: proxy iqdb searches through danbooru.
Previously the search form on the /iqdb_queries page submitted directly to the iqdb service (karasuma.donmai.us), which redirected back to Danbooru with the search results. This was different than API requests, which submitted to /iqdb_queries.json which proxied the call to iqdb through Danbooru. Because of this, searches on the /iqdb_queries page had different behavior than API requests. Things like filesize limits and referrer spoofing were handled differently. Now searches on the /iqdb_queries page submit directly to Danbooru. This is simpler and it means that API requests and HTML requests have the same behavior.
This commit is contained in:
@@ -2,16 +2,12 @@ class IqdbQueriesController < ApplicationController
|
||||
respond_to :html, :json, :xml, :js
|
||||
|
||||
def show
|
||||
if params[:url]
|
||||
url = Sources::Strategies.find(params[:url]).image_url
|
||||
@matches = IqdbProxy.query(url, params[:limit], params[:similarity])
|
||||
elsif params[:post_id]
|
||||
url = Post.find(params[:post_id]).preview_file_url
|
||||
@matches = IqdbProxy.query(url, params[:limit], params[:similarity])
|
||||
elsif params[:matches]
|
||||
@matches = IqdbProxy.decorate_posts(JSON.parse(params[:matches]))
|
||||
end
|
||||
# XXX allow bare search params for backwards compatibility.
|
||||
search_params.merge!(params.slice(:url, :post_id, :limit, :similarity).permit!)
|
||||
|
||||
respond_with(@matches)
|
||||
@matches = IqdbProxy.search(search_params)
|
||||
respond_with(@matches, template: "iqdb_queries/show")
|
||||
end
|
||||
|
||||
alias create show
|
||||
end
|
||||
|
||||
@@ -164,11 +164,11 @@ module ApplicationHelper
|
||||
end
|
||||
end
|
||||
|
||||
def search_form_for(url, classes: "inline-form", &block)
|
||||
def search_form_for(url, classes: "inline-form", method: :get, &block)
|
||||
defaults = { required: false }
|
||||
html_options = { autocomplete: "off", class: "search-form #{classes}" }
|
||||
|
||||
simple_form_for(:search, method: :get, url: url, defaults: defaults, html: html_options, &block)
|
||||
simple_form_for(:search, method: method, url: url, defaults: defaults, html: html_options, &block)
|
||||
end
|
||||
|
||||
def body_attributes(user = CurrentUser.user)
|
||||
|
||||
@@ -1,15 +1,37 @@
|
||||
class IqdbProxy
|
||||
def self.query(url, limit, similarity)
|
||||
def self.search(params)
|
||||
raise NotImplementedError unless Danbooru.config.iqdbs_server.present?
|
||||
|
||||
limit ||= 20
|
||||
similarity ||= 0.0
|
||||
limit = params[:limit].presence || 20
|
||||
limit = limit.to_i.clamp(1, 100)
|
||||
similarity = params[:similarity].to_f.clamp(0.0, 100.0)
|
||||
|
||||
if params[:file].present?
|
||||
results = query_file(params[:file], limit)
|
||||
elsif params[:url].present?
|
||||
url = Sources::Strategies.find(params[:url]).image_url
|
||||
results = query_url(url, limit)
|
||||
elsif params[:post_id].present?
|
||||
url = Post.find(params[:post_id]).preview_file_url
|
||||
results = query_url(url, limit)
|
||||
else
|
||||
results = []
|
||||
end
|
||||
|
||||
results = results.select { |result| result["score"] >= similarity }.take(limit)
|
||||
decorate_posts(results)
|
||||
end
|
||||
|
||||
def self.query_url(url, limit)
|
||||
query = { url: url, limit: limit }
|
||||
response = HTTParty.get("#{Danbooru.config.iqdbs_server}/similar", query: query, **Danbooru.config.httparty_options)
|
||||
response.parsed_response
|
||||
end
|
||||
|
||||
json = decorate_posts(response.parsed_response)
|
||||
json = json.select { |result| result["score"] >= similarity.to_f }.take(limit.to_i)
|
||||
json
|
||||
def self.query_file(file, limit)
|
||||
body = { file: file, limit: limit }
|
||||
response = HTTParty.post("#{Danbooru.config.iqdbs_server}/similar", body: body, **Danbooru.config.httparty_options)
|
||||
response.parsed_response
|
||||
end
|
||||
|
||||
def self.decorate_posts(json)
|
||||
|
||||
@@ -2,22 +2,13 @@
|
||||
<div id="a-check">
|
||||
<h1>Similar Images Search</h1>
|
||||
<section>
|
||||
<p>You can upload a file or paste a URL to perform an image similarity search with every upload on <%= Danbooru.config.app_name %>. Note that this page will redirect you to <%= link_to "IQDBS", Danbooru.config.iqdbs_server %>. You will be redirected back here once the search is executed.</p>
|
||||
<p>Paste a URL or upload a file to perform a reverse image search on <%= Danbooru.config.app_name %>.</p>
|
||||
|
||||
<%= form_tag("#{Danbooru.config.iqdbs_server}/similar", method: :post, class: "simple_form", multipart: true ) do %>
|
||||
<%= hidden_field_tag "callback", iqdb_queries_url %>
|
||||
|
||||
<div class="input string optional">
|
||||
<%= label_tag "url", "URL", class: "string optional" %>
|
||||
<%= text_field_tag "url", params[:url] %>
|
||||
</div>
|
||||
|
||||
<div class="input string optional fallback">
|
||||
<%= label_tag "file", "File", class: "string optional" %>
|
||||
<%= file_field_tag :file, :size => 50 %>
|
||||
</div>
|
||||
|
||||
<%= submit_tag "Search" %>
|
||||
<%= search_form_for(iqdb_queries_path, method: :post) do |f| %>
|
||||
<%= f.input :post_id, label: "Post ID", input_html: { value: params[:search][:post_id] } %>
|
||||
<%= f.input :url, input_html: { value: params[:search][:url] } %>
|
||||
<%= f.input :file, as: :file %>
|
||||
<%= f.submit "Search" %>
|
||||
<% end %>
|
||||
</section>
|
||||
|
||||
|
||||
@@ -153,7 +153,7 @@ Rails.application.routes.draw do
|
||||
resource :visit, :controller => "forum_topic_visits"
|
||||
end
|
||||
resources :ip_bans
|
||||
resource :iqdb_queries, :only => [:show] do
|
||||
resource :iqdb_queries, :only => [:show, :create] do
|
||||
collection do
|
||||
get :preview
|
||||
get :check, to: redirect {|path_params, req| "/iqdb_queries?#{req.query_string}"}
|
||||
|
||||
Reference in New Issue
Block a user