Fix importing notebook via livebook.dev/run to download attachments

This commit is contained in:
Jonatan Kłosko 2024-11-04 13:23:28 +08:00
parent 4380a41192
commit d853eefafa
2 changed files with 20 additions and 3 deletions

View file

@ -159,12 +159,13 @@ defmodule LivebookWeb.OpenLive do
def handle_params(%{"url" => url}, _url, socket)
when socket.assigns.live_action == :public_import do
origin = Notebook.ContentLoader.url_to_location(url)
files_url = Livebook.Utils.expand_url(url, "files/")
origin
|> Notebook.ContentLoader.fetch_content_from_location()
|> case do
{:ok, content} ->
socket = import_source(socket, content, origin: origin)
socket = import_source(socket, content, origin: origin, files_source: {:url, files_url})
{:noreply, socket}
{:error, _message} ->

View file

@ -232,13 +232,23 @@ defmodule LivebookWeb.OpenLiveTest do
end
describe "public import endpoint" do
test "imports notebook from the given url and redirects to the new session", %{conn: conn} do
test "imports notebook from the given url and downloads its files", %{conn: conn} do
bypass = Bypass.open()
Bypass.expect_once(bypass, "GET", "/notebook", fn conn ->
conn
|> Plug.Conn.put_resp_content_type("text/plain")
|> Plug.Conn.resp(200, "# My notebook")
|> Plug.Conn.resp(200, """
<!-- livebook:{"file_entries":[{"name":"image.jpg","type":"attachment"}]} -->
# My notebook
""")
end)
Bypass.expect_once(bypass, "GET", "/files/image.jpg", fn conn ->
conn
|> Plug.Conn.put_resp_content_type("text/plain")
|> Plug.Conn.resp(200, "content")
end)
notebook_url = "http://localhost:#{bypass.port}/notebook"
@ -248,6 +258,12 @@ defmodule LivebookWeb.OpenLiveTest do
{:ok, view, _} = live(conn, to)
assert render(view) =~ "My notebook"
"/sessions/" <> session_id = to
{:ok, session} = Sessions.fetch_session(session_id)
assert FileSystem.File.resolve(session.files_dir, "image.jpg") |> FileSystem.File.read() ==
{:ok, "content"}
close_session_by_path(to)
end