Expand directory path when opened via CLI (#976)

This commit is contained in:
Jonatan Kłosko 2022-02-03 23:43:47 +01:00 committed by GitHub
parent 12e2e7902f
commit 4681f98972
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 6 deletions

View file

@ -158,6 +158,7 @@ defmodule LivebookCLI.Server do
defp open_from_args(base_url, [url_or_file_or_dir]) do
url = URI.parse(url_or_file_or_dir)
path = Path.expand(url_or_file_or_dir)
cond do
url.scheme in ~w(http https file) ->
@ -165,15 +166,15 @@ defmodule LivebookCLI.Server do
|> Livebook.Utils.notebook_import_url(url_or_file_or_dir)
|> Livebook.Utils.browser_open()
File.regular?(url_or_file_or_dir) ->
File.regular?(path) ->
base_url
|> append_path("open")
|> update_query(%{"path" => url_or_file_or_dir})
|> update_query(%{"path" => path})
|> Livebook.Utils.browser_open()
File.dir?(url_or_file_or_dir) ->
File.dir?(path) ->
base_url
|> update_query(%{"path" => url_or_file_or_dir})
|> update_query(%{"path" => path})
|> Livebook.Utils.browser_open()
true ->

View file

@ -61,19 +61,22 @@ defmodule LivebookWeb.AuthPlug do
end
defp authenticate(conn, :token) do
token = Map.get(conn.query_params, "token")
{token, query_params} = Map.pop(conn.query_params, "token")
if is_binary(token) and Plug.Crypto.secure_compare(hash(token), expected(:token)) do
# Redirect to the same path without query params
conn
|> store(:token, token)
|> redirect(to: conn.request_path)
|> redirect(to: path_with_query(conn.request_path, query_params))
|> halt()
else
raise LivebookWeb.InvalidTokenError
end
end
defp path_with_query(path, params) when params == %{}, do: path
defp path_with_query(path, params), do: path <> "?" <> URI.encode_query(params)
defp key(port, mode), do: "#{port}:#{mode}"
defp expected(mode), do: hash(Application.fetch_env!(:livebook, mode))
defp hash(value), do: :crypto.hash(:sha256, value)