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
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

View file

@ -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)

View file

@ -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"