diff --git a/app/controllers/iqdb_queries_controller.rb b/app/controllers/iqdb_queries_controller.rb index ab1952015..fb152fecb 100644 --- a/app/controllers/iqdb_queries_controller.rb +++ b/app/controllers/iqdb_queries_controller.rb @@ -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 diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index ebf8da34e..3a05e28d0 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -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) diff --git a/app/logical/iqdb_proxy.rb b/app/logical/iqdb_proxy.rb index 71fe5da3f..524bc7242 100644 --- a/app/logical/iqdb_proxy.rb +++ b/app/logical/iqdb_proxy.rb @@ -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) diff --git a/app/views/iqdb_queries/show.html.erb b/app/views/iqdb_queries/show.html.erb index a66dd34b3..f0aa0aaca 100644 --- a/app/views/iqdb_queries/show.html.erb +++ b/app/views/iqdb_queries/show.html.erb @@ -2,22 +2,13 @@

Similar Images Search

-

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.

+

Paste a URL or upload a file to perform a reverse image search on <%= Danbooru.config.app_name %>.

- <%= form_tag("#{Danbooru.config.iqdbs_server}/similar", method: :post, class: "simple_form", multipart: true ) do %> - <%= hidden_field_tag "callback", iqdb_queries_url %> - -
- <%= label_tag "url", "URL", class: "string optional" %> - <%= text_field_tag "url", params[:url] %> -
- -
- <%= label_tag "file", "File", class: "string optional" %> - <%= file_field_tag :file, :size => 50 %> -
- - <%= 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 %>
diff --git a/config/routes.rb b/config/routes.rb index f1770c0f9..cdc42ff56 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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}"}