mirror of
https://github.com/livebook-dev/livebook.git
synced 2025-11-13 15:49:33 +08:00
Use FileSystem.Utils.id/3 instead
This commit is contained in:
parent
1c582d69be
commit
c37f8b97e3
5 changed files with 31 additions and 21 deletions
|
|
@ -2,6 +2,8 @@ defmodule Livebook.FileSystem.S3 do
|
||||||
use Ecto.Schema
|
use Ecto.Schema
|
||||||
import Ecto.Changeset
|
import Ecto.Changeset
|
||||||
|
|
||||||
|
alias Livebook.FileSystem
|
||||||
|
|
||||||
# File system backed by an S3 bucket.
|
# File system backed by an S3 bucket.
|
||||||
|
|
||||||
@type t :: %__MODULE__{
|
@type t :: %__MODULE__{
|
||||||
|
|
@ -81,27 +83,10 @@ defmodule Livebook.FileSystem.S3 do
|
||||||
if get_field(changeset, :id) do
|
if get_field(changeset, :id) do
|
||||||
changeset
|
changeset
|
||||||
else
|
else
|
||||||
put_change(changeset, :id, id(hub_id, bucket_url))
|
put_change(changeset, :id, FileSystem.Utils.id("s3", hub_id, bucket_url))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def id(_, nil), do: nil
|
|
||||||
|
|
||||||
def id(hub_id, bucket_url) do
|
|
||||||
if hub_id == nil or hub_id == Livebook.Hubs.Personal.id() do
|
|
||||||
hashed_id(bucket_url)
|
|
||||||
else
|
|
||||||
"#{hub_id}-#{hashed_id(bucket_url)}"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
defp hashed_id(bucket_url) do
|
|
||||||
hash = :crypto.hash(:sha256, bucket_url)
|
|
||||||
encrypted_hash = Base.url_encode64(hash, padding: false)
|
|
||||||
|
|
||||||
"s3-#{encrypted_hash}"
|
|
||||||
end
|
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Retrieves credentials for the given file system.
|
Retrieves credentials for the given file system.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -104,6 +104,26 @@ defmodule Livebook.FileSystem.Utils do
|
||||||
|> Enum.join("/")
|
|> Enum.join("/")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Returns the id based on given hub id and data with given prefix.
|
||||||
|
"""
|
||||||
|
def id(prefix, hub_id, data)
|
||||||
|
|
||||||
|
def id(_, _, nil), do: nil
|
||||||
|
|
||||||
|
def id(prefix, hub_id, data) do
|
||||||
|
if hub_id == nil or hub_id == Livebook.Hubs.Personal.id() do
|
||||||
|
hashed_id(prefix, data)
|
||||||
|
else
|
||||||
|
"#{hub_id}-#{hashed_id(prefix, data)}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp hashed_id(prefix, data) do
|
||||||
|
hash = :crypto.hash(:sha256, data)
|
||||||
|
"#{prefix}-#{Base.url_encode64(hash, padding: false)}"
|
||||||
|
end
|
||||||
|
|
||||||
defp remove_in_middle([], _elem), do: []
|
defp remove_in_middle([], _elem), do: []
|
||||||
defp remove_in_middle([head], _elem), do: [head]
|
defp remove_in_middle([head], _elem), do: [head]
|
||||||
defp remove_in_middle([head | tail], elem), do: remove_in_middle(tail, elem, [head])
|
defp remove_in_middle([head | tail], elem), do: remove_in_middle(tail, elem, [head])
|
||||||
|
|
|
||||||
|
|
@ -272,7 +272,7 @@ defmodule LivebookWeb.Integration.SessionLiveTest do
|
||||||
|
|
||||||
team_file_system =
|
team_file_system =
|
||||||
build(:fs_s3,
|
build(:fs_s3,
|
||||||
id: FileSystem.S3.id(team_id, bucket_url),
|
id: Livebook.FileSystemHelpers.s3_id(team_id, bucket_url),
|
||||||
bucket_url: bucket_url,
|
bucket_url: bucket_url,
|
||||||
hub_id: team_id
|
hub_id: team_id
|
||||||
)
|
)
|
||||||
|
|
@ -312,7 +312,7 @@ defmodule LivebookWeb.Integration.SessionLiveTest do
|
||||||
|
|
||||||
file_system =
|
file_system =
|
||||||
build(:fs_s3,
|
build(:fs_s3,
|
||||||
id: FileSystem.S3.id(hub_id, bucket_url),
|
id: Livebook.FileSystemHelpers.s3_id(hub_id, bucket_url),
|
||||||
bucket_url: bucket_url,
|
bucket_url: bucket_url,
|
||||||
hub_id: hub_id,
|
hub_id: hub_id,
|
||||||
external_id: "123"
|
external_id: "123"
|
||||||
|
|
|
||||||
5
test/support/file_system_helpers.ex
Normal file
5
test/support/file_system_helpers.ex
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
defmodule Livebook.FileSystemHelpers do
|
||||||
|
def s3_id(hub_id, bucket_url) do
|
||||||
|
Livebook.FileSystem.Utils.id("s3", hub_id, bucket_url)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@ -154,7 +154,7 @@ defmodule Livebook.HubHelpers do
|
||||||
|
|
||||||
file_system =
|
file_system =
|
||||||
build(:fs_s3,
|
build(:fs_s3,
|
||||||
id: Livebook.FileSystem.S3.id(hub_id, bucket_url),
|
id: Livebook.FileSystemHelpers.s3_id(hub_id, bucket_url),
|
||||||
bucket_url: bucket_url,
|
bucket_url: bucket_url,
|
||||||
region: "auto",
|
region: "auto",
|
||||||
hub_id: hub_id
|
hub_id: hub_id
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue