From 4159821f5e4fc845b62c40276e7ebe2bd38a9e99 Mon Sep 17 00:00:00 2001 From: Pavel Liferenko Date: Sat, 25 May 2024 13:12:25 +0200 Subject: [PATCH 1/6] feat: add radio button + update changeset --- .../add_file_entry_upload_component.ex | 37 +++++++++++++++---- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/lib/livebook_web/live/session_live/add_file_entry_upload_component.ex b/lib/livebook_web/live/session_live/add_file_entry_upload_component.ex index 226fb73f506..f4468c11296 100644 --- a/lib/livebook_web/live/session_live/add_file_entry_upload_component.ex +++ b/lib/livebook_web/live/session_live/add_file_entry_upload_component.ex @@ -1,4 +1,11 @@ defmodule LivebookWeb.SessionLive.AddFileEntryUploadComponent do + +# TODO REMOVE BEFORE FLIGHT!!!!!! +""" +TODO: +- add uploaded file in a sidebar "Files/references" +- validate radio (when user forgot to choose one of options) +""" use LivebookWeb, :live_component import Ecto.Changeset @@ -23,11 +30,11 @@ defmodule LivebookWeb.SessionLive.AddFileEntryUploadComponent do end defp changeset(attrs \\ %{}) do - data = %{name: nil} - types = %{name: :string} + data = %{name: nil, store_local?: true} + types = %{name: :string, store_local?: :boolean} - cast({data, types}, attrs, [:name]) - |> validate_required([:name]) + cast({data, types}, attrs, [:name, :store_local?]) + |> validate_required([:name, :store_local?]) |> Livebook.Notebook.validate_file_entry_name(:name) end @@ -62,6 +69,14 @@ defmodule LivebookWeb.SessionLive.AddFileEntryUploadComponent do autocomplete="off" phx-debounce="200" /> + <.radio_field + field={f[:storage_place]} + options={[ + {"true", "Store in the notebook files as an attachment"}, + {"false", "Upload to storage and store link"} + + ]} + />
<.button type="submit" disabled={not @changeset.valid? or upload_disabled?(@uploads.file)}> @@ -117,10 +132,16 @@ defmodule LivebookWeb.SessionLive.AddFileEntryUploadComponent do [:ok] = consume_uploaded_entries(socket, :file, fn %{}, _entry -> {:ok, :ok} end) - file_entry = %{name: data.name, type: :attachment} - Livebook.Session.add_file_entries(socket.assigns.session.pid, [file_entry]) - send(self(), {:file_entry_uploaded, file_entry}) - {:noreply, socket} + if data.store_local? do + file_entry = %{name: data.name, type: :attachment} + + Livebook.Session.add_file_entries(socket.assigns.session.pid, [file_entry]) + send(self(), {:file_entry_uploaded, file_entry}) + + {:noreply, socket} + else + IO.puts("Imma be stored in a global storage") + end {:error, changeset} -> {:noreply, assign(socket, changeset: changeset)} From 286d8e1308713b32014eabfd59f3a55f0081fc21 Mon Sep 17 00:00:00 2001 From: Pavel Liferenko Date: Sun, 26 May 2024 15:16:12 +0200 Subject: [PATCH 2/6] chore: clean-up + todo for further work --- .../add_file_entry_upload_component.ex | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/livebook_web/live/session_live/add_file_entry_upload_component.ex b/lib/livebook_web/live/session_live/add_file_entry_upload_component.ex index f4468c11296..45ace479436 100644 --- a/lib/livebook_web/live/session_live/add_file_entry_upload_component.ex +++ b/lib/livebook_web/live/session_live/add_file_entry_upload_component.ex @@ -1,11 +1,11 @@ defmodule LivebookWeb.SessionLive.AddFileEntryUploadComponent do + # TODO REMOVE BEFORE FLIGHT!!!!!! + """ + TODO: + - add uploaded file in a sidebar "Files/references" + - validate radio (when user forgot to choose one of options) + """ -# TODO REMOVE BEFORE FLIGHT!!!!!! -""" -TODO: -- add uploaded file in a sidebar "Files/references" -- validate radio (when user forgot to choose one of options) -""" use LivebookWeb, :live_component import Ecto.Changeset @@ -74,7 +74,6 @@ TODO: options={[ {"true", "Store in the notebook files as an attachment"}, {"false", "Upload to storage and store link"} - ]} />
@@ -140,6 +139,7 @@ TODO: {:noreply, socket} else + # TODO IO.puts("Imma be stored in a global storage") end From 224f15f4c0415c248645255f041b559ad078d2e9 Mon Sep 17 00:00:00 2001 From: Pavel Liferenko Date: Mon, 27 May 2024 07:43:55 +0200 Subject: [PATCH 3/6] fix: resolved issue with store_local? being ignored during the uploading --- .../live/session_live/add_file_entry_upload_component.ex | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/livebook_web/live/session_live/add_file_entry_upload_component.ex b/lib/livebook_web/live/session_live/add_file_entry_upload_component.ex index 45ace479436..e30e3ff7d99 100644 --- a/lib/livebook_web/live/session_live/add_file_entry_upload_component.ex +++ b/lib/livebook_web/live/session_live/add_file_entry_upload_component.ex @@ -70,7 +70,7 @@ defmodule LivebookWeb.SessionLive.AddFileEntryUploadComponent do phx-debounce="200" /> <.radio_field - field={f[:storage_place]} + field={f[:store_local?]} options={[ {"true", "Store in the notebook files as an attachment"}, {"false", "Upload to storage and store link"} @@ -131,6 +131,9 @@ defmodule LivebookWeb.SessionLive.AddFileEntryUploadComponent do [:ok] = consume_uploaded_entries(socket, :file, fn %{}, _entry -> {:ok, :ok} end) + dbg(data.store_local?) + IO.inspect(data.store_local? |> is_boolean(), label: "is bool?") + if data.store_local? do file_entry = %{name: data.name, type: :attachment} From a6fe3e55f9a0cac8850dbf87adbd313656d3d5f4 Mon Sep 17 00:00:00 2001 From: Pavel Liferenko Date: Mon, 27 May 2024 14:20:37 +0200 Subject: [PATCH 4/6] fix: typo in @doc --- lib/livebook/file_system.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/livebook/file_system.ex b/lib/livebook/file_system.ex index a65443a4f68..a72b3b74b9c 100644 --- a/lib/livebook/file_system.ex +++ b/lib/livebook/file_system.ex @@ -23,7 +23,7 @@ defprotocol Livebook.FileSystem do Path has most of the semantics of regular file paths, with the following exceptions: - * path must be be absolute for consistency + * path must be absolute for consistency * directory path must have a trailing slash, whereas regular file path must not have a trailing slash. Rationale: certain file From 1a4183ab2b541293a7c80258ab729d05d5680509 Mon Sep 17 00:00:00 2001 From: Pavel Liferenko Date: Sun, 9 Jun 2024 17:20:25 +0200 Subject: [PATCH 5/6] feat[unstable]: there are 2 options: impl for S3 or pure S3 --- .../live/session_live/add_file_entry_upload_component.ex | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/livebook_web/live/session_live/add_file_entry_upload_component.ex b/lib/livebook_web/live/session_live/add_file_entry_upload_component.ex index e30e3ff7d99..3b366813556 100644 --- a/lib/livebook_web/live/session_live/add_file_entry_upload_component.ex +++ b/lib/livebook_web/live/session_live/add_file_entry_upload_component.ex @@ -142,7 +142,9 @@ defmodule LivebookWeb.SessionLive.AddFileEntryUploadComponent do {:noreply, socket} else - # TODO + # -- TODO + # Option 1: Livebook.FileSystem.write(%Livebook.FileSystem.S3{}, "", false) + # Option 2: Livebook.FileSystem.S3.write(%Livebook.FileSystem.S3{}, path, content) IO.puts("Imma be stored in a global storage") end From 81134fd401b948d7193c695bfc767e7e2760867c Mon Sep 17 00:00:00 2001 From: Pavel Liferenko Date: Thu, 20 Jun 2024 10:49:45 +0200 Subject: [PATCH 6/6] feat [unstable]: trying to change filesystem to upload a file in S3 --- .../add_file_entry_upload_component.ex | 54 ++++++++++++------- 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/lib/livebook_web/live/session_live/add_file_entry_upload_component.ex b/lib/livebook_web/live/session_live/add_file_entry_upload_component.ex index 3b366813556..59e0bffccf6 100644 --- a/lib/livebook_web/live/session_live/add_file_entry_upload_component.ex +++ b/lib/livebook_web/live/session_live/add_file_entry_upload_component.ex @@ -70,10 +70,10 @@ defmodule LivebookWeb.SessionLive.AddFileEntryUploadComponent do phx-debounce="200" /> <.radio_field - field={f[:store_local?]} + field={f[:storage_type]} options={[ - {"true", "Store in the notebook files as an attachment"}, - {"false", "Upload to storage and store link"} + {"local", "Store in the notebook files as an attachment"}, + {"s3", "Upload to your S3 storage and store link"} ]} /> @@ -122,6 +122,7 @@ defmodule LivebookWeb.SessionLive.AddFileEntryUploadComponent do {:noreply, cancel_upload(socket, :file, ref)} end + # OPTIMIZE add guard data.storage_type def handle_event("add", %{"data" => data}, socket) do data |> changeset() @@ -131,21 +132,38 @@ defmodule LivebookWeb.SessionLive.AddFileEntryUploadComponent do [:ok] = consume_uploaded_entries(socket, :file, fn %{}, _entry -> {:ok, :ok} end) - dbg(data.store_local?) - IO.inspect(data.store_local? |> is_boolean(), label: "is bool?") - - if data.store_local? do - file_entry = %{name: data.name, type: :attachment} - - Livebook.Session.add_file_entries(socket.assigns.session.pid, [file_entry]) - send(self(), {:file_entry_uploaded, file_entry}) - - {:noreply, socket} - else - # -- TODO - # Option 1: Livebook.FileSystem.write(%Livebook.FileSystem.S3{}, "", false) - # Option 2: Livebook.FileSystem.S3.write(%Livebook.FileSystem.S3{}, path, content) - IO.puts("Imma be stored in a global storage") + dbg(data.storage_type) + IO.inspect(data.storage_type, label: "storage type") + + case data.storage_type do + "s3" -> + # -- TODO + # Option 1: Livebook.FileSystem.write(%Livebook.FileSystem.S3{}, "", false) + # Option 2: + path = "/test_hardcoded_path" + content = data + + # FileSystems.load("local", "s3") + case Livebook.FileSystem.write(%Livebook.FileSystem.S3{}, path, content) do + :ok -> + IO.puts("Imma be stored in a global storage") + # -- TODO + :ok + + {:error, error} -> + IO.inspect(error, label: "Something goes ne tak !!!") + # -- TODO + :error + end + + # "local" -> + _ -> + file_entry = %{name: data.name, type: :attachment} + + Livebook.Session.add_file_entries(socket.assigns.session.pid, [file_entry]) + send(self(), {:file_entry_uploaded, file_entry}) + + {:noreply, socket} end {:error, changeset} ->