2020-07-30 00:49:51 +00:00
|
|
|
defmodule WikiWeb.PageController do
|
|
|
|
use WikiWeb, :controller
|
|
|
|
|
2020-08-01 20:43:55 +00:00
|
|
|
alias Wiki.Repo
|
2020-07-30 00:49:51 +00:00
|
|
|
alias Wiki.Content
|
2020-08-01 20:43:55 +00:00
|
|
|
alias Wiki.Content.{Page, Upload}
|
2020-07-30 00:49:51 +00:00
|
|
|
|
2020-08-01 20:43:55 +00:00
|
|
|
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]
|
2020-07-30 00:49:51 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2020-08-01 20:43:55 +00:00
|
|
|
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
|
|
|
|
|
2020-07-30 00:49:51 +00:00
|
|
|
def index(conn, _params) do
|
|
|
|
pages = Content.list_pages()
|
|
|
|
render(conn, "index.html", pages: pages)
|
|
|
|
end
|
|
|
|
|
2020-07-30 02:21:25 +00:00
|
|
|
def new(conn, params) do
|
|
|
|
changeset = Content.change_page(%Page{}, params, encrypt: false)
|
2020-07-30 00:49:51 +00:00
|
|
|
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
|
2020-08-02 20:12:38 +00:00
|
|
|
page = Repo.preload(conn.assigns.page, [:uploads, :pages_linked_from])
|
2020-08-01 23:37:16 +00:00
|
|
|
render(conn, "show.html", page: page)
|
2020-07-30 00:49:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def edit(conn, _params) do
|
2020-08-01 20:43:55 +00:00
|
|
|
page = conn.assigns.page |> Repo.preload(:uploads)
|
2020-07-30 00:49:51 +00:00
|
|
|
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
|
|
|
|
|
2020-08-01 20:43:55 +00:00
|
|
|
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
|
|
|
|
|
2020-07-30 00:49:51 +00:00
|
|
|
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
|