forked from e621ng/e621ng
[IQDB] Expose the similarity cutoff in the UI
This commit is contained in:
parent
b58865149f
commit
7ed3047621
@ -1,20 +1,23 @@
|
|||||||
class IqdbQueriesController < ApplicationController
|
class IqdbQueriesController < ApplicationController
|
||||||
respond_to :html, :json
|
respond_to :html, :json
|
||||||
before_action :throttle
|
|
||||||
|
|
||||||
def show
|
def show
|
||||||
if params[:file]
|
# Allow legacy ?post_id=123 parameters
|
||||||
@matches = iqdb_proxy(:query_file, params[:file].tempfile)
|
search_params = params[:search].presence || params
|
||||||
elsif params[:url].present?
|
throttle(search_params)
|
||||||
parsed_url = Addressable::URI.heuristic_parse(params[:url]) rescue nil
|
|
||||||
|
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
|
raise User::PrivilegeError, "Invalid URL" unless parsed_url
|
||||||
whitelist_result = UploadWhitelist.is_whitelisted?(parsed_url)
|
whitelist_result = UploadWhitelist.is_whitelisted?(parsed_url)
|
||||||
raise User::PrivilegeError, "Not allowed to request content from this URL" unless whitelist_result[0]
|
raise User::PrivilegeError, "Not allowed to request content from this URL" unless whitelist_result[0]
|
||||||
@matches = iqdb_proxy(:query_url, params[:url])
|
@matches = IqdbProxy.query_url(search_params[:url], search_params[:score_cutoff])
|
||||||
elsif params[:post_id]
|
elsif search_params[:post_id].present?
|
||||||
@matches = iqdb_proxy(:query_post, Post.find(params[:post_id]))
|
@matches = IqdbProxy.query_post(Post.find_by(id: search_params[:post_id]), search_params[:score_cutoff])
|
||||||
elsif params[:hash]
|
elsif search_params[:hash].present?
|
||||||
@matches = iqdb_proxy(:query_hash, params[:hash])
|
@matches = IqdbProxy.query_hash(search_params[:hash], search_params[:score_cutoff])
|
||||||
end
|
end
|
||||||
|
|
||||||
respond_with(@matches) do |fmt|
|
respond_with(@matches) do |fmt|
|
||||||
@ -28,13 +31,11 @@ class IqdbQueriesController < ApplicationController
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def iqdb_proxy(method, value)
|
def throttle(search_params)
|
||||||
IqdbProxy.send(method, value, params[:score_cutoff])
|
return if Danbooru.config.disable_throttles?
|
||||||
end
|
|
||||||
|
|
||||||
def throttle
|
if %i[file url post_id hash].any? { |key| search_params[key].present? }
|
||||||
if params[:file] || params[:url] || params[:post_id] || params[:hash]
|
if RateLimiter.check_limit("img:#{CurrentUser.ip_addr}", 1, 2.seconds)
|
||||||
if RateLimiter.check_limit("img:#{CurrentUser.ip_addr}", 1, 2.seconds) && !Danbooru.config.disable_throttles?
|
|
||||||
raise APIThrottled
|
raise APIThrottled
|
||||||
else
|
else
|
||||||
RateLimiter.hit("img:#{CurrentUser.ip_addr}", 2.seconds)
|
RateLimiter.hit("img:#{CurrentUser.ip_addr}", 2.seconds)
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
module FormSearchHelper
|
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
|
# 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
|
show_on_load = filled_form_fields(&).any? || always_display || !hideable
|
||||||
form = simple_form_for(:search, {
|
form = simple_form_for(:search, {
|
||||||
method: :get,
|
method: method,
|
||||||
url: path,
|
url: path,
|
||||||
builder: SearchFormBuilder,
|
builder: SearchFormBuilder,
|
||||||
search_params: params[:search],
|
search_params: params[:search],
|
||||||
|
@ -36,7 +36,7 @@ module IqdbProxy
|
|||||||
end
|
end
|
||||||
|
|
||||||
def query_post(post, score_cutoff)
|
def query_post(post, score_cutoff)
|
||||||
return [] unless post.has_preview?
|
return [] unless post&.has_preview?
|
||||||
|
|
||||||
File.open(post.preview_file_path) do |f|
|
File.open(post.preview_file_path) do |f|
|
||||||
query_file(f, score_cutoff)
|
query_file(f, score_cutoff)
|
||||||
@ -63,7 +63,7 @@ module IqdbProxy
|
|||||||
def process_iqdb_result(json, score_cutoff)
|
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)
|
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|
|
json.map do |x|
|
||||||
x["post"] = Post.find(x["post_id"])
|
x["post"] = Post.find(x["post_id"])
|
||||||
x
|
x
|
||||||
|
@ -1,21 +1,12 @@
|
|||||||
<div id="c-iqdb-queries">
|
<div id="c-iqdb-queries">
|
||||||
<div id="a-show">
|
<div id="a-show">
|
||||||
<h1>Similar Images Search</h1>
|
<h1>Similar Images Search</h1>
|
||||||
<section>
|
<%= form_search(path: iqdb_queries_path, hideable: false, method: :post) do |f| %>
|
||||||
<%= form_tag(iqdb_queries_path, method: :post, class: "simple_form", multipart: true ) do %>
|
<%= f.input :post_id, label: "Post ID" %>
|
||||||
<div class="input string optional">
|
<%= f.input :url, label: "URL" %>
|
||||||
<%= label_tag "url", "URL", class: "string optional" %>
|
<%= f.input :file, as: :file %>
|
||||||
<%= text_field_tag "url", params[:url] %>
|
<%= f.input :score_cutoff, label: "Similarity", default: 60 %>
|
||||||
</div>
|
<% end %>
|
||||||
|
|
||||||
<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>
|
|
||||||
|
|
||||||
<%= render "posts/partials/common/inline_blacklist" %>
|
<%= render "posts/partials/common/inline_blacklist" %>
|
||||||
<%= render "iqdb_queries/matches" %>
|
<%= render "iqdb_queries/matches" %>
|
||||||
|
@ -38,7 +38,7 @@
|
|||||||
<ul>
|
<ul>
|
||||||
<li><%= link_to "Sets with this post", post_sets_path(post_id: @post.id), rel: "nofollow" %></li>
|
<li><%= link_to "Sets with this post", post_sets_path(post_id: @post.id), rel: "nofollow" %></li>
|
||||||
<% if @post.has_preview? %>
|
<% 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 %>
|
<% end %>
|
||||||
<% if @post.is_image? %>
|
<% 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>
|
<li><a rel="nofollow" href="https://www.google.com/searchbyimage?image_url=<%= @post.reverse_image_url %>&client=e621">Reverse Google Search</a></li>
|
||||||
|
@ -168,8 +168,6 @@ Rails.application.routes.draw do
|
|||||||
resource :iqdb_queries, :only => [:show] do
|
resource :iqdb_queries, :only => [:show] do
|
||||||
collection do
|
collection do
|
||||||
post :show
|
post :show
|
||||||
get :preview
|
|
||||||
get :check, to: redirect {|path_params, req| "/iqdb_queries?#{req.query_string}"}
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
resources :mod_actions
|
resources :mod_actions
|
||||||
|
Loading…
Reference in New Issue
Block a user