defmodule WikiWeb.PageController do use WikiWeb, :controller alias Wiki.Repo alias Wiki.Content alias Wiki.Content.{Page, Upload} import Ecto.Query 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(conn.assigns.current_user) 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, page_title: "New Page") end def create(conn, %{"page" => page_params} = 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} -> file = Map.get(params, "initial_file") unless is_nil(file) do decoded_key = Base.decode16!(key, case: :lower) changeset = Upload.create_from_plug(file, decoded_key) |> Upload.changeset(%{page_id: page.id}) {:ok, _upload} = Repo.insert(changeset) end 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, :pages_linked_from]) render(conn, "show.html", page: page, page_title: page.title) 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, page_title: "Edit #{page.title}") 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) if Map.get(params, "oneshot") do data = Upload.decrypt_content(upload, key) conn |> put_resp_header("content-type", upload.content_type) |> send_resp(200, data) else conn = conn |> put_resp_header("content-type", upload.content_type) |> send_chunked(200) upload |> Upload.decrypt_stream(key) |> Enum.reduce_while(conn, fn decrypted_chunk, conn -> case chunk(conn, decrypted_chunk) do {:ok, conn} -> {:cont, conn} {:error, :closed} -> {:halt, {:halt, conn}} end end) end 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 def random(conn, _params) do user = conn.assigns.current_user page_ids = Repo.all(from p in Page, where: p.user_id == ^user.id, select: p.id) random_page = Enum.random(page_ids) redirect(conn, to: Routes.page_path(conn, :show, random_page)) end end