philomena/lib/philomena_web/controllers/search/reverse_controller.ex

42 lines
1 KiB
Elixir
Raw Normal View History

2019-11-29 01:11:05 +01:00
defmodule PhilomenaWeb.Search.ReverseController do
use PhilomenaWeb, :controller
2019-12-04 01:50:23 +01:00
alias PhilomenaWeb.ImageReverse
2019-11-29 01:11:05 +01:00
2020-01-11 19:50:53 +01:00
plug :set_scraper_cache
plug PhilomenaWeb.ScraperPlug, params_key: "image", params_name: "image"
2019-11-29 01:11:05 +01:00
2020-01-11 19:50:53 +01:00
def index(conn, params) do
create(conn, params)
2019-11-29 01:11:05 +01:00
end
2020-01-11 19:50:53 +01:00
def create(conn, %{"image" => image_params}) when is_map(image_params) do
2019-12-04 01:50:23 +01:00
images = ImageReverse.images(image_params)
2019-11-29 01:11:05 +01:00
2020-01-11 19:50:53 +01:00
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
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 | params: params}
2019-11-29 01:11:05 +01:00
end
2020-01-11 19:50:53 +01:00
defp normalize_dist(%{"distance" => distance}) do
("0" <> distance)
|> Float.parse()
|> elem(0)
|> Float.to_string()
end
defp normalize_dist(_dist), do: "0.25"
2019-12-16 20:24:38 +01:00
end