2019-12-04 01:50:23 +01:00
|
|
|
defmodule PhilomenaWeb.Api.Json.ImageController do
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
|
|
|
alias Philomena.Images.Image
|
2020-05-01 06:40:57 +02:00
|
|
|
alias Philomena.Images
|
2020-03-30 02:51:35 +02:00
|
|
|
alias Philomena.Interactions
|
2019-12-31 06:10:50 +01:00
|
|
|
alias Philomena.Repo
|
2020-05-01 06:40:57 +02:00
|
|
|
alias Philomena.Tags
|
|
|
|
alias Philomena.UserStatistics
|
2019-12-31 06:10:50 +01:00
|
|
|
import Ecto.Query
|
2019-12-04 01:50:23 +01:00
|
|
|
|
2020-05-01 06:40:57 +02:00
|
|
|
plug :set_scraper_cache
|
|
|
|
plug PhilomenaWeb.ApiRequireAuthorizationPlug when action in [:create]
|
|
|
|
plug PhilomenaWeb.UserAttributionPlug when action in [:create]
|
|
|
|
|
|
|
|
plug PhilomenaWeb.ScraperPlug,
|
|
|
|
[params_name: "image", params_key: "image"] when action in [:create]
|
|
|
|
|
2019-12-31 06:10:50 +01:00
|
|
|
def show(conn, %{"id" => id}) do
|
2020-03-30 02:51:35 +02:00
|
|
|
user = conn.assigns.current_user
|
|
|
|
|
2019-12-31 06:10:50 +01:00
|
|
|
image =
|
|
|
|
Image
|
|
|
|
|> where(id: ^id)
|
|
|
|
|> preload([:tags, :user, :intensity])
|
|
|
|
|> Repo.one()
|
2019-12-04 01:50:23 +01:00
|
|
|
|
2019-12-31 06:10:50 +01:00
|
|
|
case image do
|
|
|
|
nil ->
|
|
|
|
conn
|
|
|
|
|> put_status(:not_found)
|
|
|
|
|> text("")
|
|
|
|
|
|
|
|
_ ->
|
2020-03-30 02:51:35 +02:00
|
|
|
interactions = Interactions.user_interactions([image], user)
|
|
|
|
|
|
|
|
render(conn, "show.json", image: image, interactions: interactions)
|
2019-12-31 06:10:50 +01:00
|
|
|
end
|
2019-12-04 01:50:23 +01:00
|
|
|
end
|
2020-05-01 06:40:57 +02:00
|
|
|
|
|
|
|
def create(conn, %{"image" => image_params}) do
|
|
|
|
user = conn.assigns.current_user
|
|
|
|
attributes = conn.assigns.attributes
|
|
|
|
|
|
|
|
case Images.create_image(attributes, image_params) do
|
|
|
|
{:ok, %{image: image}} ->
|
|
|
|
spawn(fn ->
|
|
|
|
Images.repair_image(image)
|
|
|
|
end)
|
|
|
|
|
|
|
|
# ImageProcessor.cast(image.id)
|
|
|
|
Images.reindex_image(image)
|
|
|
|
Tags.reindex_tags(image.added_tags)
|
|
|
|
UserStatistics.inc_stat(user, :uploads)
|
|
|
|
|
|
|
|
render(conn, "show.json", image: image, interactions: [])
|
|
|
|
|
|
|
|
{:error, :image, changeset, _} ->
|
|
|
|
conn
|
|
|
|
|> put_status(:bad_request)
|
|
|
|
|> render("error.json", changeset: changeset)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp set_scraper_cache(conn, _opts) do
|
|
|
|
params =
|
|
|
|
conn.params
|
|
|
|
|> Map.put_new("image", %{})
|
|
|
|
|> Map.put("scraper_cache", conn.params["url"])
|
|
|
|
|
|
|
|
%{conn | params: params}
|
|
|
|
end
|
2019-12-30 15:18:38 +01:00
|
|
|
end
|