From 56b40a025524c1093be3421f466db74d068d79c2 Mon Sep 17 00:00:00 2001 From: Albert Yi Date: Fri, 22 Jun 2018 15:56:15 -0700 Subject: [PATCH] add iqdb preview --- .../javascripts/{posts.js => posts.js.erb} | 2 +- app/assets/javascripts/uploads.js | 4 ++-- .../stylesheets/specific/z_responsive.scss | 20 ++++++++++++++----- app/controllers/iqdb_queries_controller.rb | 8 ++++++++ app/presenters/post_presenter.rb | 17 ++++++++-------- app/views/iqdb_queries/preview.html.erb | 9 +++++++++ config/routes.rb | 1 + 7 files changed, 44 insertions(+), 17 deletions(-) rename app/assets/javascripts/{posts.js => posts.js.erb} (99%) create mode 100644 app/views/iqdb_queries/preview.html.erb diff --git a/app/assets/javascripts/posts.js b/app/assets/javascripts/posts.js.erb similarity index 99% rename from app/assets/javascripts/posts.js rename to app/assets/javascripts/posts.js.erb index 46d7553d6..0adadf4d2 100644 --- a/app/assets/javascripts/posts.js +++ b/app/assets/javascripts/posts.js.erb @@ -154,7 +154,7 @@ Danbooru.Post.initialize_similar = function() { $("#similar-button").click(function(e) { - $.post("/iqdb_queries", {"url": $("#post_source").val()}).done(function(html) {$("#iqdb-similar").html(html).show()}); + $.get("/iqdb_queries/preview", {"url": $("#post_source").val()}).done(function(html) {$("#iqdb-similar").html(html).show()}); e.preventDefault(); }); } diff --git a/app/assets/javascripts/uploads.js b/app/assets/javascripts/uploads.js index 406739e4a..fa74257ba 100644 --- a/app/assets/javascripts/uploads.js +++ b/app/assets/javascripts/uploads.js @@ -61,7 +61,7 @@ Danbooru.Upload.initialize_iqdb_source = function() { if (/^https?:\/\//.test($("#normalized_url").val())) { - $.post("/iqdb_queries", {"url": $("#normalized_url").val()}).done(function(html) {$("#iqdb-similar").html(html)}); + $.get("/iqdb_queries/preview", {"url": $("#normalized_url").val()}).done(function(html) {$("#iqdb-similar").html(html)}); } } @@ -77,7 +77,7 @@ Danbooru.Upload.initialize_similar = function() { $("#similar-button").click(function(e) { - $.post("/iqdb_queries", {"url": $("#upload_source").val()}).done(function(html) {$("#iqdb-similar").html(html).show()}); + $.get("/iqdb_queries/preview", {"url": $("#upload_source").val()}).done(function(html) {$("#iqdb-similar").html(html).show()}); e.preventDefault(); }); } diff --git a/app/assets/stylesheets/specific/z_responsive.scss b/app/assets/stylesheets/specific/z_responsive.scss index 8dc06e279..93c79e034 100644 --- a/app/assets/stylesheets/specific/z_responsive.scss +++ b/app/assets/stylesheets/specific/z_responsive.scss @@ -138,24 +138,34 @@ article.post-preview { margin: 0.5vw; - width: 32vw; - height: 32vw; + width: 48vw; + height: 48vw; text-align: center; vertical-align: middle; display: inline-block; + &.cropped { + width: 32vw; + height: 32vw; + } + a { //width: 48.5vw; //display: block; margin: 0 auto; } - img.cropped { + + img { //object-fit: contain; - width: 32vw; - height: 32vw; margin: 0 auto; border: none !important; + + &.cropped { + width: 32vw; + height: 32vw; + } } + &[data-tags~=animated]:before, &[data-file-ext=swf]:before, &[data-file-ext=webm]:before, &[data-file-ext=mp4]:before, &[data-file-ext=zip]:before { @include animated-icon; } diff --git a/app/controllers/iqdb_queries_controller.rb b/app/controllers/iqdb_queries_controller.rb index 1485acf00..f5162330b 100644 --- a/app/controllers/iqdb_queries_controller.rb +++ b/app/controllers/iqdb_queries_controller.rb @@ -26,4 +26,12 @@ class IqdbQueriesController < ApplicationController respond_with(@matches) end + + def preview + url = URI.parse(Danbooru.config.iqdbs_server) + url.path = "/similar" + url.query = {url: params[:url]}.to_query + @results = HTTParty.get(url.to_s).parsed_response + render layout: false + end end diff --git a/app/presenters/post_presenter.rb b/app/presenters/post_presenter.rb index 2704f71b2..2b3417251 100644 --- a/app/presenters/post_presenter.rb +++ b/app/presenters/post_presenter.rb @@ -21,8 +21,15 @@ class PostPresenter < Presenter end path = options[:path_prefix] || "/posts" + if CurrentUser.id == 1 && options[:show_cropped] && post.has_cropped? && !CurrentUser.user.disable_cropped_thumbnails? + src = post.crop_file_url + imgClass = "cropped" + else + src = post.preview_file_url + imgClass = nil + end - html = %{
} + html = %{
} if options[:tags].present? && !CurrentUser.is_anonymous? tag_param = "?tags=#{CGI::escape(options[:tags])}" elsif options[:pool_id] || options[:pool] @@ -34,14 +41,6 @@ class PostPresenter < Presenter end html << %{} - if CurrentUser.id == 1 && options[:show_cropped] && post.has_cropped? && !CurrentUser.user.disable_cropped_thumbnails? - src = post.crop_file_url - imgClass = "cropped" - else - src = post.preview_file_url - imgClass = nil - end - tooltip = "#{post.tag_string} rating:#{post.rating} score:#{post.score}" html << %{#{h(post.tag_string)}} html << %{} diff --git a/app/views/iqdb_queries/preview.html.erb b/app/views/iqdb_queries/preview.html.erb new file mode 100644 index 000000000..9ab6b42a7 --- /dev/null +++ b/app/views/iqdb_queries/preview.html.erb @@ -0,0 +1,9 @@ +<% if @results.any? %> +

Similar

+ <% @results.each do |match| %> + <%= PostPresenter.preview(Post.find(match["post_id"]), :tags => "status:any", :similarity => match["score"]) %> + <% end %> +<% else %> +

Similar

+

No matches found

+<% end %> diff --git a/config/routes.rb b/config/routes.rb index f475fae05..073ad1883 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -157,6 +157,7 @@ Rails.application.routes.draw do resources :ip_bans resource :iqdb_queries, :only => [:show] do collection do + get :preview get :check, to: redirect {|path_params, req| "/iqdb_queries?#{req.query_string}"} end end