diff --git a/lib/wiki_web/controllers/page_controller.ex b/lib/wiki_web/controllers/page_controller.ex index 6dc9707..262e2ea 100644 --- a/lib/wiki_web/controllers/page_controller.ex +++ b/lib/wiki_web/controllers/page_controller.ex @@ -65,7 +65,7 @@ defmodule WikiWeb.PageController do render(conn, "new.html", changeset: changeset) end - def create(conn, %{"page" => page_params}) do + def create(conn, %{"page" => page_params} = params) do key = get_session(conn, :content_encryption_key) page_params = @@ -75,6 +75,18 @@ defmodule WikiWeb.PageController do 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)) diff --git a/lib/wiki_web/templates/page/edit.html.eex b/lib/wiki_web/templates/page/edit.html.eex index e25db71..b3d38bb 100644 --- a/lib/wiki_web/templates/page/edit.html.eex +++ b/lib/wiki_web/templates/page/edit.html.eex @@ -1,6 +1,6 @@