From 9e2c664a4152dc604692ef97db7e0074e816e1e6 Mon Sep 17 00:00:00 2001 From: r888888888 Date: Fri, 25 Apr 2014 17:15:09 -0700 Subject: [PATCH] fixes #2130 --- app/assets/javascripts/uploads.js | 2 +- app/controllers/iqdb_controller.rb | 8 ----- app/controllers/iqdb_queries_controller.rb | 30 +++++++++++++++++++ app/logical/iqdb/importer.rb | 1 - app/logical/iqdb/responses/collection.rb | 5 +--- app/logical/iqdb/server.rb | 13 ++++++++ .../iqdb_queries/create_by_post.html.erb | 9 ++++++ .../create_by_url.html.erb} | 0 .../posts/partials/show/_options.html.erb | 2 +- config/routes.rb | 2 +- 10 files changed, 56 insertions(+), 16 deletions(-) delete mode 100644 app/controllers/iqdb_controller.rb create mode 100644 app/controllers/iqdb_queries_controller.rb create mode 100644 app/views/iqdb_queries/create_by_post.html.erb rename app/views/{iqdb/similar_by_source.html.erb => iqdb_queries/create_by_url.html.erb} (100%) diff --git a/app/assets/javascripts/uploads.js b/app/assets/javascripts/uploads.js index 315191adb..2133f6bec 100644 --- a/app/assets/javascripts/uploads.js +++ b/app/assets/javascripts/uploads.js @@ -20,7 +20,7 @@ } Danbooru.Upload.initialize_iqdb_source = function() { - $.get("/iqdb/similar_by_source", {"source": $("#normalized_url").val()}).done(function(html) {$("#iqdb-similar").html(html)}); + $.post("/iqdb_queries", {"url": $("#normalized_url").val()}).done(function(html) {$("#iqdb-similar").html(html)}); } Danbooru.Upload.initialize_enter_on_tags = function() { diff --git a/app/controllers/iqdb_controller.rb b/app/controllers/iqdb_controller.rb deleted file mode 100644 index f1e63c5b3..000000000 --- a/app/controllers/iqdb_controller.rb +++ /dev/null @@ -1,8 +0,0 @@ -class IqdbController < ApplicationController - def similar_by_source - @download = Iqdb::Download.new(params[:source]) - @download.download_from_source - @download.find_similar - render :layout => false - end -end diff --git a/app/controllers/iqdb_queries_controller.rb b/app/controllers/iqdb_queries_controller.rb new file mode 100644 index 000000000..ac23a7ace --- /dev/null +++ b/app/controllers/iqdb_queries_controller.rb @@ -0,0 +1,30 @@ +class IqdbQueriesController < ApplicationController + before_filter :member_only + + def create + if !Danbooru.config.iqdb_hostname_and_port + render :nothing => true + return + end + + if params[:url] + create_by_url + elsif params[:post_id] + create_by_post + end + end + +protected + def create_by_url + @download = Iqdb::Download.new(params[:url]) + @download.download_from_source + @download.find_similar + render :layout => false, :action => "create_by_url" + end + + def create_by_post + @post = Post.find(params[:post_id]) + @results = Iqdb::Server.default.similar(@post.id, 3) + render :layout => false, :action => "create_by_post" + end +end \ No newline at end of file diff --git a/app/logical/iqdb/importer.rb b/app/logical/iqdb/importer.rb index 585d45710..8aad521b0 100644 --- a/app/logical/iqdb/importer.rb +++ b/app/logical/iqdb/importer.rb @@ -4,7 +4,6 @@ module Iqdb Post.find_each do |post| IO.popen("iqdb command #{Danbooru.config.iqdb_file}", "w+") do |io| if File.exists?(post.preview_file_path) - puts post.id hex = post.id.to_s(16) io.puts "add 0 #{hex}:#{post.preview_file_path}" end diff --git a/app/logical/iqdb/responses/collection.rb b/app/logical/iqdb/responses/collection.rb index a01ab2c8c..f8cd4915a 100644 --- a/app/logical/iqdb/responses/collection.rb +++ b/app/logical/iqdb/responses/collection.rb @@ -2,6 +2,7 @@ module Iqdb module Responses class Collection attr_reader :responses + delegate :each, :empty?, :any?, :to => :matches def initialize(response_string) @responses = response_string.split(/\n/).map do |string| @@ -13,10 +14,6 @@ module Iqdb @matches ||= responses.select {|x| x.is_a?(Iqdb::Responses::Response_200) && x.score >= 90} end - def empty? - matches.empty? - end - def errored? errors.any? end diff --git a/app/logical/iqdb/server.rb b/app/logical/iqdb/server.rb index efc2983e0..de39f04e7 100644 --- a/app/logical/iqdb/server.rb +++ b/app/logical/iqdb/server.rb @@ -7,6 +7,10 @@ module Iqdb attr_reader :hostname, :port, :socket + def self.default + new(*Danbooru.config.iqdb_hostname_and_port) + end + def initialize(hostname, port) @hostname = hostname @port = port @@ -45,6 +49,15 @@ module Iqdb end end + def similar(post_id, results, flags = FLAG_DISCARD_COMMON_COEFFS) + request do + hex_id = post_id.to_s(16) + socket.puts "sim 0 #{flags} #{results} #{hex_id}" + socket.puts "done" + responses = Responses::Collection.new(@socket.read) + end + end + def query(results, filename, flags = FLAG_DISCARD_COMMON_COEFFS) request do socket.puts "query 0 #{flags} #{results} #{filename}" diff --git a/app/views/iqdb_queries/create_by_post.html.erb b/app/views/iqdb_queries/create_by_post.html.erb new file mode 100644 index 000000000..f66fb5b58 --- /dev/null +++ b/app/views/iqdb_queries/create_by_post.html.erb @@ -0,0 +1,9 @@ +<% if @results.any? %> +

Similar

+ <% @results.each do |match| %> + <%= PostPresenter.preview(Post.find(match.post_id)) %> + <% end %> +<% else %> +

Similar

+

No matches found

+<% end %> diff --git a/app/views/iqdb/similar_by_source.html.erb b/app/views/iqdb_queries/create_by_url.html.erb similarity index 100% rename from app/views/iqdb/similar_by_source.html.erb rename to app/views/iqdb_queries/create_by_url.html.erb diff --git a/app/views/posts/partials/show/_options.html.erb b/app/views/posts/partials/show/_options.html.erb index b8ffaf9cc..18996f2ba 100644 --- a/app/views/posts/partials/show/_options.html.erb +++ b/app/views/posts/partials/show/_options.html.erb @@ -14,7 +14,7 @@ <% if CurrentUser.is_builder? && post.has_notes? %>
  • <%= link_to "Copy all notes", "#", :id => "copy-notes" %>
  • <% end %> -
  • <%= link_to "Find similar", "http://danbooru.iqdb.org/db-search.php?url=http://#{Danbooru.config.hostname}#{post.preview_file_url}" %>
  • +
  • <%= link_to "Find similar", iqdb_queries_path(:post_id => post.id), :method => :post, :remote => true %>
  • <% if post.is_status_locked? %>
  • Status locked
  • diff --git a/config/routes.rb b/config/routes.rb index 8391c0b85..0c28cfc3e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -257,6 +257,7 @@ Rails.application.routes.draw do get :diff end end + resources :iqdb_queries, :only => [:create] # aliases resources :wpages, :controller => "wiki_pages" @@ -264,7 +265,6 @@ Rails.application.routes.draw do resources :fposts, :controller => "forum_posts" get "/m/posts", :controller => "mobile/posts", :action => "index" get "/m/posts/:id", :controller => "mobile/posts", :action => "show" - get "/iqdb/similar_by_source", :controller => "iqdb", :action => "similar_by_source" # legacy aliases get "/artist" => redirect {|params, req| "/artists?page=#{req.params[:page]}&search[name]=#{CGI::escape(req.params[:name].to_s)}"}