From 4c0a073d3d2be85f17bba8de4a2626404bf44fa7 Mon Sep 17 00:00:00 2001 From: Inhji Date: Fri, 7 Apr 2023 11:37:55 +0200 Subject: [PATCH] file import --- lib/chiya/notes/note_import.ex | 13 ++++++ lib/chiya_web/controllers/note_controller.ex | 43 ++++++++++++++++++- .../controllers/note_html/import.html.heex | 15 +++++++ .../controllers/note_html/index.html.heex | 3 ++ lib/chiya_web/live/note_show_live.ex | 10 +++++ lib/chiya_web/router.ex | 3 ++ 6 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 lib/chiya/notes/note_import.ex create mode 100644 lib/chiya_web/controllers/note_html/import.html.heex diff --git a/lib/chiya/notes/note_import.ex b/lib/chiya/notes/note_import.ex new file mode 100644 index 0000000..66627b0 --- /dev/null +++ b/lib/chiya/notes/note_import.ex @@ -0,0 +1,13 @@ +defmodule Chiya.Notes.NoteImport do + import Ecto.Changeset + + defstruct [:file] + + @types %{file: :string} + + def change_note_import(params) do + {%Chiya.Notes.NoteImport{}, @types} + |> cast(params, Map.keys(@types)) + |> validate_required(:file) + end +end \ No newline at end of file diff --git a/lib/chiya_web/controllers/note_controller.ex b/lib/chiya_web/controllers/note_controller.ex index f4059ce..19b4216 100644 --- a/lib/chiya_web/controllers/note_controller.ex +++ b/lib/chiya_web/controllers/note_controller.ex @@ -2,7 +2,7 @@ defmodule ChiyaWeb.NoteController do use ChiyaWeb, :controller alias Chiya.Notes - alias Chiya.Notes.Note + alias Chiya.Notes.{Note, NoteImport} def index(conn, _params) do notes = Notes.list_notes() @@ -95,6 +95,47 @@ defmodule ChiyaWeb.NoteController do end end + def import_prepare(conn, _params) do + render(conn, :import, changeset: NoteImport.change_note_import(%{})) + end + + def import_run(conn, %{ + "note_import" => %{ + "file" => %{ + path: path, + content_type: "text/markdown", + filename: filename + } + } + }) do + case File.read(path) do + {:ok, content} -> + note_params = %{ + name: filename, + content: content + } + + case Notes.create_note(note_params) do + {:ok, note} -> + conn + |> put_flash(:info, "Note created successfully.") + |> redirect(to: ~p"/admin/notes/#{note}") + + {:error, %Ecto.Changeset{} = changeset} -> + render(conn, :new, changeset: changeset, channels: to_channel_options()) + end + + _ -> + render(conn, :import, changeset: NoteImport.change_note_import(%{})) + end + end + + def import_run(conn, _params) do + conn + |> put_flash(:error, "Error while importing.") + |> redirect(to: ~p"/admin/notes") + end + defp from_channel_ids(note_params) do selected_ids = Enum.map(note_params["channels"] || [], &String.to_integer/1) diff --git a/lib/chiya_web/controllers/note_html/import.html.heex b/lib/chiya_web/controllers/note_html/import.html.heex new file mode 100644 index 0000000..67bcee0 --- /dev/null +++ b/lib/chiya_web/controllers/note_html/import.html.heex @@ -0,0 +1,15 @@ +<.header> + Import + + +<.simple_form :let={f} for={@changeset} action={~p"/admin/notes/import"} multipart={true}> + <.error :if={@changeset.action}> + Oops, something went wrong! Please check the errors below. + + + <.input field={f[:file]} type="file" label="Markdown file" accept="text/markdown, text/plain" /> + + <:actions> + <.button>Import Note + + diff --git a/lib/chiya_web/controllers/note_html/index.html.heex b/lib/chiya_web/controllers/note_html/index.html.heex index 814b11f..23551ad 100644 --- a/lib/chiya_web/controllers/note_html/index.html.heex +++ b/lib/chiya_web/controllers/note_html/index.html.heex @@ -4,6 +4,9 @@ <.link href={~p"/admin/notes/new"}> <.button>New Note + <.link href={~p"/admin/notes/import"}> + <.button>Import Note + diff --git a/lib/chiya_web/live/note_show_live.ex b/lib/chiya_web/live/note_show_live.ex index 7ec0e0e..7db7bc6 100644 --- a/lib/chiya_web/live/note_show_live.ex +++ b/lib/chiya_web/live/note_show_live.ex @@ -36,6 +36,16 @@ defmodule ChiyaWeb.NoteShowLive do <:item title="Channels"><%= @channels %> + <.line /> + +
+ File Content +
+ <%= raw ChiyaWeb.Markdown.render(@note.content) %> +
+
+ + <.line /> <%= if !Enum.empty?(@note.images) do %> diff --git a/lib/chiya_web/router.ex b/lib/chiya_web/router.ex index cf0d09b..33a9cba 100644 --- a/lib/chiya_web/router.ex +++ b/lib/chiya_web/router.ex @@ -61,6 +61,9 @@ defmodule ChiyaWeb.Router do resources "/settings", SettingController, singleton: true resources "/identities", IdentityController + get "/notes/import", NoteController, :import_prepare + post "/notes/import", NoteController, :import_run + live "/notes/:id", NoteShowLive, :show get "/notes/:id/raw", NoteController, :raw