defmodule WikiWeb.PageController do use WikiWeb, :controller alias Wiki.Repo alias Wiki.Content alias Wiki.Content.{Page, Upload} plug :get_page when action in [ :show, :edit, :update, :delete, :create_upload, :get_upload, :delete_upload ] plug :get_upload_plug when action in [:get_upload, :delete_upload] defp get_page(%Plug.Conn{path_params: %{"id" => id}} = conn, _opts) do case Content.get_page(id) do nil -> conn |> send_resp(404, "Not found") |> halt() page -> key = get_session(conn, :content_encryption_key) page = %Page{page | content_encryption_key: key} page = if action_name(conn) in [:show, :edit] do Page.decrypt_content(page) else page end assign(conn, :page, page) end end defp get_upload_plug(%Plug.Conn{path_params: %{"upload_id" => id}} = conn, _opts) do page = conn.assigns.page case Content.get_upload(page, id) do nil -> conn |> send_resp(404, "Not found") |> halt() upload -> assign(conn, :upload, upload) end end def index(conn, _params) do pages = Content.list_pages() render(conn, "index.html", pages: pages) end def new(conn, params) do changeset = Content.change_page(%Page{}, params, encrypt: false) render(conn, "new.html", changeset: changeset) end def create(conn, %{"page" => page_params}) do key = get_session(conn, :content_encryption_key) page_params = page_params |> Map.put("user_id", conn.assigns.current_user.id) |> Map.put("content_encryption_key", key) case Content.create_page(page_params) do {:ok, page} -> conn |> put_flash(:info, "Page created successfully.") |> redirect(to: Routes.page_path(conn, :show, page)) {:error, %Ecto.Changeset{} = changeset} -> render(conn, "new.html", changeset: changeset) end end def show(conn, _params) do page = Repo.preload(conn.assigns.page, :uploads) render(conn, "show.html", page: page) end def edit(conn, _params) do page = conn.assigns.page |> Repo.preload(:uploads) changeset = Content.change_page(page) render(conn, "edit.html", page: page, changeset: changeset) end def update(conn, %{"page" => page_params}) do page = conn.assigns.page case Content.update_page(page, page_params) do {:ok, page} -> conn |> put_flash(:info, "Page updated successfully.") |> redirect(to: Routes.page_path(conn, :show, page)) {:error, %Ecto.Changeset{} = changeset} -> render(conn, "edit.html", page: page, changeset: changeset) end end def create_upload(conn, %{"file" => %Plug.Upload{} = file}) do page = conn.assigns.page key = get_session(conn, :content_encryption_key) key = Base.decode16!(key, case: :lower) changeset = Upload.create_from_plug(file, key) |> Upload.changeset(%{page_id: page.id}) {:ok, _upload} = Repo.insert(changeset) redirect(conn, to: Routes.page_path(conn, :edit, page.id)) end def get_upload(conn, _params) do upload = conn.assigns.upload key = get_session(conn, :content_encryption_key) key = Base.decode16!(key, case: :lower) data = Upload.decrypt_content(upload, key) conn |> put_resp_header("content-type", upload.content_type) |> send_resp(200, data) end def delete_upload(conn, _params) do page = conn.assigns.page upload = conn.assigns.upload Upload.delete_file(upload) Repo.delete(upload) redirect(conn, to: Routes.page_path(conn, :edit, page.id)) end def delete(conn, _params) do page = conn.assigns.page {:ok, _page} = Content.delete_page(page) conn |> put_flash(:info, "Page deleted successfully.") |> redirect(to: Routes.page_path(conn, :index)) end end