[IQDB] Expose the similarity cutoff in the UI

This commit is contained in:
Earlopain 2023-05-07 14:48:58 +02:00
parent b58865149f
commit 7ed3047621
No known key found for this signature in database
GPG Key ID: 6CFB948E15246897
6 changed files with 28 additions and 39 deletions

View File

@ -1,20 +1,23 @@
class IqdbQueriesController < ApplicationController
respond_to :html, :json
before_action :throttle
def show
if params[:file]
@matches = iqdb_proxy(:query_file, params[:file].tempfile)
elsif params[:url].present?
parsed_url = Addressable::URI.heuristic_parse(params[:url]) rescue nil
# Allow legacy ?post_id=123 parameters
search_params = params[:search].presence || params
throttle(search_params)
if search_params[:file].present?
@matches = IqdbProxy.query_file(search_params[:file].tempfile, search_params[:score_cutoff])
elsif search_params[:url].present?
parsed_url = Addressable::URI.heuristic_parse(search_params[:url]) rescue nil
raise User::PrivilegeError, "Invalid URL" unless parsed_url
whitelist_result = UploadWhitelist.is_whitelisted?(parsed_url)
raise User::PrivilegeError, "Not allowed to request content from this URL" unless whitelist_result[0]
@matches = iqdb_proxy(:query_url, params[:url])
elsif params[:post_id]
@matches = iqdb_proxy(:query_post, Post.find(params[:post_id]))
elsif params[:hash]
@matches = iqdb_proxy(:query_hash, params[:hash])
@matches = IqdbProxy.query_url(search_params[:url], search_params[:score_cutoff])
elsif search_params[:post_id].present?
@matches = IqdbProxy.query_post(Post.find_by(id: search_params[:post_id]), search_params[:score_cutoff])
elsif search_params[:hash].present?
@matches = IqdbProxy.query_hash(search_params[:hash], search_params[:score_cutoff])
end
respond_with(@matches) do |fmt|
@ -28,13 +31,11 @@ class IqdbQueriesController < ApplicationController
private
def iqdb_proxy(method, value)
IqdbProxy.send(method, value, params[:score_cutoff])
end
def throttle(search_params)
return if Danbooru.config.disable_throttles?
def throttle
if params[:file] || params[:url] || params[:post_id] || params[:hash]
if RateLimiter.check_limit("img:#{CurrentUser.ip_addr}", 1, 2.seconds) && !Danbooru.config.disable_throttles?
if %i[file url post_id hash].any? { |key| search_params[key].present? }
if RateLimiter.check_limit("img:#{CurrentUser.ip_addr}", 1, 2.seconds)
raise APIThrottled
else
RateLimiter.hit("img:#{CurrentUser.ip_addr}", 2.seconds)

View File

@ -1,10 +1,9 @@
module FormSearchHelper
def form_search(path:, always_display: false, &)
def form_search(path:, always_display: false, hideable: request.path.split("/")[2] != "search", method: :get, &)
# dedicated search routes like /comments/search should always show
hideable = request.path.split("/")[2] != "search"
show_on_load = filled_form_fields(&).any? || always_display || !hideable
form = simple_form_for(:search, {
method: :get,
method: method,
url: path,
builder: SearchFormBuilder,
search_params: params[:search],

View File

@ -36,7 +36,7 @@ module IqdbProxy
end
def query_post(post, score_cutoff)
return [] unless post.has_preview?
return [] unless post&.has_preview?
File.open(post.preview_file_path) do |f|
query_file(f, score_cutoff)
@ -63,7 +63,7 @@ module IqdbProxy
def process_iqdb_result(json, score_cutoff)
raise Error, "Server returned an error. Most likely the url is not found." unless json.is_a?(Array)
json.filter! { |entry| (entry["score"] || 0) >= (score_cutoff || 60).to_i }
json.filter! { |entry| (entry["score"] || 0) >= (score_cutoff.presence || 60).to_i }
json.map do |x|
x["post"] = Post.find(x["post_id"])
x

View File

@ -1,21 +1,12 @@
<div id="c-iqdb-queries">
<div id="a-show">
<h1>Similar Images Search</h1>
<section>
<%= form_tag(iqdb_queries_path, method: :post, class: "simple_form", multipart: true ) do %>
<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" %>
<% end %>
</section>
<%= form_search(path: iqdb_queries_path, hideable: false, method: :post) do |f| %>
<%= f.input :post_id, label: "Post ID" %>
<%= f.input :url, label: "URL" %>
<%= f.input :file, as: :file %>
<%= f.input :score_cutoff, label: "Similarity", default: 60 %>
<% end %>
<%= render "posts/partials/common/inline_blacklist" %>
<%= render "iqdb_queries/matches" %>

View File

@ -38,7 +38,7 @@
<ul>
<li><%= link_to "Sets with this post", post_sets_path(post_id: @post.id), rel: "nofollow" %></li>
<% if @post.has_preview? %>
<li><%= link_to "Visually similar on E6", iqdb_queries_path(post_id: @post.id), rel: "nofollow" %></li>
<li><%= link_to "Visually similar on E6", iqdb_queries_path(search: { post_id: @post.id }), rel: "nofollow" %></li>
<% end %>
<% if @post.is_image? %>
<li><a rel="nofollow" href="https://www.google.com/searchbyimage?image_url=<%= @post.reverse_image_url %>&client=e621">Reverse Google Search</a></li>

View File

@ -168,8 +168,6 @@ Rails.application.routes.draw do
resource :iqdb_queries, :only => [:show] do
collection do
post :show
get :preview
get :check, to: redirect {|path_params, req| "/iqdb_queries?#{req.query_string}"}
end
end
resources :mod_actions