wiki/lib/wiki_web/controllers/page_controller.ex

186 lines
4.9 KiB
Elixir
Raw Normal View History

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-08-15 19:51:52 +00:00
import Ecto.Query
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
2020-08-02 21:46:18 +00:00
pages = Content.list_pages(conn.assigns.current_user)
2020-07-30 00:49:51 +00:00
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-08-03 15:16:10 +00:00
render(conn, "new.html", changeset: changeset, page_title: "New Page")
2020-07-30 00:49:51 +00:00
end
2020-08-02 23:40:45 +00:00
def create(conn, %{"page" => page_params} = params) do
2020-07-30 00:49:51 +00:00
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} ->
2020-08-02 23:40:45 +00:00
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
2020-07-30 00:49:51 +00:00
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-03 15:16:10 +00:00
render(conn, "show.html", page: page, page_title: page.title)
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)
2020-08-03 15:16:10 +00:00
render(conn, "edit.html", page: page, changeset: changeset, page_title: "Edit #{page.title}")
2020-07-30 00:49:51 +00:00
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)
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)
2020-08-01 20:43:55 +00:00
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
2020-08-15 19:51:52 +00:00
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
2020-07-30 00:49:51 +00:00
end