2019-12-17 19:53:41 +01:00
|
|
|
defmodule PhilomenaWeb.Image.UploaderController do
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
|
|
|
alias Philomena.Images.Image
|
|
|
|
alias Philomena.Images
|
|
|
|
alias Philomena.Repo
|
|
|
|
|
|
|
|
plug :verify_authorized
|
|
|
|
plug :load_resource, model: Image, id_name: "image_id", persisted: true
|
|
|
|
|
|
|
|
def update(conn, %{"image" => image_params}) do
|
|
|
|
{:ok, image} = Images.update_uploader(conn.assigns.image, image_params)
|
|
|
|
|
|
|
|
Images.reindex_image(image)
|
|
|
|
|
|
|
|
image = Repo.preload(image, user: [awards: :badge])
|
|
|
|
changeset = Images.change_image(image)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_view(PhilomenaWeb.ImageView)
|
|
|
|
|> render("_uploader.html", layout: false, image: image, changeset: changeset)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp verify_authorized(conn, _opts) do
|
|
|
|
case Canada.Can.can?(conn.assigns.current_user, :show, :ip_address) do
|
2020-01-11 05:20:19 +01:00
|
|
|
true -> conn
|
2019-12-17 19:53:41 +01:00
|
|
|
_false -> PhilomenaWeb.NotAuthorizedPlug.call(conn)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|