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 @@

Edit Page

-<%= render "form.html", Map.put(assigns, :action, Routes.page_path(@conn, :update, @page)) %> +<%= render "form.html", assigns |> Map.put(:action, Routes.page_path(@conn, :update, @page)) |> Map.put(:initial_file, false) %>

Uploaded Files

diff --git a/lib/wiki_web/templates/page/form.html.eex b/lib/wiki_web/templates/page/form.html.eex index 2f0e756..0347433 100644 --- a/lib/wiki_web/templates/page/form.html.eex +++ b/lib/wiki_web/templates/page/form.html.eex @@ -1,4 +1,4 @@ -<%= form_for @changeset, @action, fn f -> %> +<%= form_for @changeset, @action, [multipart: true], fn f -> %> <%= if @changeset.action do %>

Oops, something went wrong! Please check the errors below.

@@ -13,6 +13,12 @@ <%= textarea f, :content %> <%= error_tag f, :content %> + + <%= if @initial_file do %> + + + <% end %> +
<%= submit "Save" %>
diff --git a/lib/wiki_web/templates/page/new.html.eex b/lib/wiki_web/templates/page/new.html.eex index ade8c6d..e9e87f8 100644 --- a/lib/wiki_web/templates/page/new.html.eex +++ b/lib/wiki_web/templates/page/new.html.eex @@ -1,5 +1,5 @@

New Page

-<%= render "form.html", Map.put(assigns, :action, Routes.page_path(@conn, :create)) %> +<%= render "form.html", assigns |> Map.put(:action, Routes.page_path(@conn, :create)) |> Map.put(:initial_file, true) %> <%= link "Back", to: Routes.page_path(@conn, :index) %>