mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-24 12:37:58 +01:00
67 lines
1.7 KiB
Elixir
67 lines
1.7 KiB
Elixir
defmodule PhilomenaWeb.Api.Json.ImageController do
|
|
use PhilomenaWeb, :controller
|
|
|
|
alias Philomena.Images.Image
|
|
alias Philomena.Images
|
|
alias Philomena.Interactions
|
|
alias Philomena.Repo
|
|
import Ecto.Query
|
|
|
|
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]
|
|
|
|
def show(conn, %{"id" => id}) do
|
|
user = conn.assigns.current_user
|
|
|
|
image =
|
|
Image
|
|
|> where(id: ^id)
|
|
|> preload([:tags, :user, :intensity])
|
|
|> Repo.one()
|
|
|
|
case image do
|
|
nil ->
|
|
conn
|
|
|> put_status(:not_found)
|
|
|> text("")
|
|
|
|
_ ->
|
|
interactions = Interactions.user_interactions([image], user)
|
|
|
|
render(conn, "show.json", image: image, interactions: interactions)
|
|
end
|
|
end
|
|
|
|
def create(conn, %{"image" => image_params}) do
|
|
attributes = conn.assigns.attributes
|
|
|
|
case Images.create_image(attributes, image_params) do
|
|
{:ok, %{image: image}} ->
|
|
PhilomenaWeb.Endpoint.broadcast!(
|
|
"firehose",
|
|
"image:create",
|
|
PhilomenaWeb.Api.Json.ImageView.render("show.json", %{image: image, interactions: []})
|
|
)
|
|
|
|
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
|
|
end
|