From 3ac8687c18391481a3428cfa56567b65c77032ea Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Sat, 11 Jan 2020 13:50:53 -0500 Subject: [PATCH] allow get-based reverse searching --- .../controllers/search/reverse_controller.ex | 36 +++++++++++++++---- lib/philomena_web/plugs/scraper_plug.ex | 2 +- .../templates/search/reverse/index.html.slime | 14 ++++---- 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/lib/philomena_web/controllers/search/reverse_controller.ex b/lib/philomena_web/controllers/search/reverse_controller.ex index 88c1df44..44bf896b 100644 --- a/lib/philomena_web/controllers/search/reverse_controller.ex +++ b/lib/philomena_web/controllers/search/reverse_controller.ex @@ -3,17 +3,39 @@ defmodule PhilomenaWeb.Search.ReverseController do alias PhilomenaWeb.ImageReverse - plug PhilomenaWeb.ScraperPlug, - [params_key: "image", params_name: "image"] when action in [:create] + plug :set_scraper_cache + plug PhilomenaWeb.ScraperPlug, params_key: "image", params_name: "image" - def index(conn, _params) do + def index(conn, params) do + create(conn, params) + end + + def create(conn, %{"image" => image_params}) when is_map(image_params) do + images = ImageReverse.images(image_params) + + render(conn, "index.html", title: "Reverse Search", images: images) + end + + def create(conn, _params) do render(conn, "index.html", title: "Reverse Search", images: nil) end - def create(conn, %{"image" => image_params}) do - images = ImageReverse.images(image_params) + defp set_scraper_cache(conn, _opts) do + params = + conn.params + |> Map.put_new("image", %{}) + |> Map.put_new("scraper_cache", conn.params["url"]) + |> Map.put("distance", normalize_dist(conn.params)) - conn - |> render("index.html", title: "Reverse Search", images: images) + %{conn | params: params} end + + defp normalize_dist(%{"distance" => distance}) do + ("0" <> distance) + |> Float.parse() + |> elem(0) + |> Float.to_string() + end + + defp normalize_dist(_dist), do: "0.25" end diff --git a/lib/philomena_web/plugs/scraper_plug.ex b/lib/philomena_web/plugs/scraper_plug.ex index 56ac0cc1..f9454e39 100644 --- a/lib/philomena_web/plugs/scraper_plug.ex +++ b/lib/philomena_web/plugs/scraper_plug.ex @@ -9,7 +9,7 @@ defmodule PhilomenaWeb.ScraperPlug do %{^params_name => %{^params_key => %Plug.Upload{}}} -> conn - %{"scraper_cache" => url} -> + %{"scraper_cache" => url} when not is_nil(url) -> Philomena.Http.get!(url, [], max_body_length: 30_000_000) |> maybe_fixup_params(opts, conn) diff --git a/lib/philomena_web/templates/search/reverse/index.html.slime b/lib/philomena_web/templates/search/reverse/index.html.slime index 17efef77..7a3d8066 100644 --- a/lib/philomena_web/templates/search/reverse/index.html.slime +++ b/lib/philomena_web/templates/search/reverse/index.html.slime @@ -3,10 +3,10 @@ h1 Reverse Search = form_for :image, Routes.search_reverse_path(@conn, :create), [multipart: true], fn f -> p ' Basic image similarity search. Finds uploaded images similar to the one - ' provided based on simple intensities and uses the median frame of GIFs; - ' very low contrast images (such as sketches) will produce poor results - ' and, regardless of contrast, results may include seemingly random images - ' that look very different. + ' provided based on simple intensities and uses the median frame of + ' animations; very low contrast images (such as sketches) will produce + ' poor results and, regardless of contrast, results may include seemingly + ' random images that look very different. .image-other #js-image-upload-previews @@ -15,7 +15,7 @@ h1 Reverse Search = file_input f, :image, class: "input js-scraper" .field.field--inline - = url_input f, :scraper_url, class: "input input--wide js-scraper", placeholder: "Link a deviantART page, a Tumblr post, or the image directly" + = url_input f, :url, name: "url", class: "input input--wide js-scraper", placeholder: "Link a deviantART page, a Tumblr post, or the image directly" button.button.button--separate-left#js-scraper-preview type="button" title="Fetch the image at the specified URL" data-disable-with="Fetch" ' Fetch @@ -26,7 +26,7 @@ h1 Reverse Search .field = label f, :distance, "Match distance (suggested values: between 0.2 and 0.5)" br - = number_input f, :distance, value: 0.25, min: 0, max: 1, step: 0.01, class: "input" + = number_input f, :distance, name: "distance", value: 0.25, min: 0, max: 1, step: 0.01, class: "input" .field = submit "Reverse Search", class: "button" @@ -71,4 +71,4 @@ h1 Reverse Search - true -> h2 Results p - ' We couldn't find any images matching this in our image database. \ No newline at end of file + ' We couldn't find any images matching this in our image database.