allow get-based reverse searching

This commit is contained in:
byte[] 2020-01-11 13:50:53 -05:00
parent 48baae21ed
commit 3ac8687c18
3 changed files with 37 additions and 15 deletions

View file

@ -3,17 +3,39 @@ defmodule PhilomenaWeb.Search.ReverseController do
alias PhilomenaWeb.ImageReverse alias PhilomenaWeb.ImageReverse
plug PhilomenaWeb.ScraperPlug, plug :set_scraper_cache
[params_key: "image", params_name: "image"] when action in [:create] 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) render(conn, "index.html", title: "Reverse Search", images: nil)
end end
def create(conn, %{"image" => image_params}) do defp set_scraper_cache(conn, _opts) do
images = ImageReverse.images(image_params) params =
conn.params
|> Map.put_new("image", %{})
|> Map.put_new("scraper_cache", conn.params["url"])
|> Map.put("distance", normalize_dist(conn.params))
conn %{conn | params: params}
|> render("index.html", title: "Reverse Search", images: images)
end 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 end

View file

@ -9,7 +9,7 @@ defmodule PhilomenaWeb.ScraperPlug do
%{^params_name => %{^params_key => %Plug.Upload{}}} -> %{^params_name => %{^params_key => %Plug.Upload{}}} ->
conn conn
%{"scraper_cache" => url} -> %{"scraper_cache" => url} when not is_nil(url) ->
Philomena.Http.get!(url, [], max_body_length: 30_000_000) Philomena.Http.get!(url, [], max_body_length: 30_000_000)
|> maybe_fixup_params(opts, conn) |> maybe_fixup_params(opts, conn)

View file

@ -3,10 +3,10 @@ h1 Reverse Search
= form_for :image, Routes.search_reverse_path(@conn, :create), [multipart: true], fn f -> = form_for :image, Routes.search_reverse_path(@conn, :create), [multipart: true], fn f ->
p p
' Basic image similarity search. Finds uploaded images similar to the one ' Basic image similarity search. Finds uploaded images similar to the one
' provided based on simple intensities and uses the median frame of GIFs; ' provided based on simple intensities and uses the median frame of
' very low contrast images (such as sketches) will produce poor results ' animations; very low contrast images (such as sketches) will produce
' and, regardless of contrast, results may include seemingly random images ' poor results and, regardless of contrast, results may include seemingly
' that look very different. ' random images that look very different.
.image-other .image-other
#js-image-upload-previews #js-image-upload-previews
@ -15,7 +15,7 @@ h1 Reverse Search
= file_input f, :image, class: "input js-scraper" = file_input f, :image, class: "input js-scraper"
.field.field--inline .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" button.button.button--separate-left#js-scraper-preview type="button" title="Fetch the image at the specified URL" data-disable-with="Fetch"
' Fetch ' Fetch
@ -26,7 +26,7 @@ h1 Reverse Search
.field .field
= label f, :distance, "Match distance (suggested values: between 0.2 and 0.5)" = label f, :distance, "Match distance (suggested values: between 0.2 and 0.5)"
br 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 .field
= submit "Reverse Search", class: "button" = submit "Reverse Search", class: "button"
@ -71,4 +71,4 @@ h1 Reverse Search
- true -> - true ->
h2 Results h2 Results
p p
' We couldn't find any images matching this in our image database. ' We couldn't find any images matching this in our image database.