Add initial file upload
This commit is contained in:
parent
67b18d16f2
commit
06c5ccc7c1
|
@ -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))
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<h1>Edit Page</h1>
|
||||
|
||||
<%= 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) %>
|
||||
|
||||
<h2>Uploaded Files</h2>
|
||||
<table>
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
<%= form_for @changeset, @action, fn f -> %>
|
||||
<%= form_for @changeset, @action, [multipart: true], fn f -> %>
|
||||
<%= if @changeset.action do %>
|
||||
<div class="alert alert-danger">
|
||||
<p>Oops, something went wrong! Please check the errors below.</p>
|
||||
|
@ -13,6 +13,12 @@
|
|||
<%= textarea f, :content %>
|
||||
<%= error_tag f, :content %>
|
||||
|
||||
|
||||
<%= if @initial_file do %>
|
||||
<label for="initial_file">File</label>
|
||||
<input type="file" name="initial_file" id="initial_file">
|
||||
<% end %>
|
||||
|
||||
<div>
|
||||
<%= submit "Save" %>
|
||||
</div>
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
<h1>New Page</h1>
|
||||
|
||||
<%= 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) %>
|
||||
|
||||
<span><%= link "Back", to: Routes.page_path(@conn, :index) %></span>
|
||||
|
|
Loading…
Reference in New Issue