Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: feat: [#2498] Allow drag-n-drop in a notebook to upload to a particular storage #2621

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/livebook/file_system.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down Expand Up @@ -62,6 +69,13 @@ defmodule LivebookWeb.SessionLive.AddFileEntryUploadComponent do
autocomplete="off"
phx-debounce="200"
/>
<.radio_field
field={f[:storage_type]}
options={[
{"local", "Store in the notebook files as an attachment"},
{"s3", "Upload to your S3 storage and store link"}
]}
/>
</div>
<div class="mt-6 flex space-x-3">
<.button type="submit" disabled={not @changeset.valid? or upload_disabled?(@uploads.file)}>
Expand Down Expand Up @@ -108,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()
Expand All @@ -117,10 +132,39 @@ 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}
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} ->
{:noreply, assign(socket, changeset: changeset)}
Expand Down