Use FileSystem.Utils.id/3 instead

This commit is contained in:
Alexandre de Souza 2025-09-02 10:21:58 -03:00
parent 1c582d69be
commit c37f8b97e3
No known key found for this signature in database
GPG key ID: E39228FFBA346545
5 changed files with 31 additions and 21 deletions

View file

@ -2,6 +2,8 @@ defmodule Livebook.FileSystem.S3 do
use Ecto.Schema
import Ecto.Changeset
alias Livebook.FileSystem
# File system backed by an S3 bucket.
@type t :: %__MODULE__{
@ -81,27 +83,10 @@ defmodule Livebook.FileSystem.S3 do
if get_field(changeset, :id) do
changeset
else
put_change(changeset, :id, id(hub_id, bucket_url))
put_change(changeset, :id, FileSystem.Utils.id("s3", hub_id, bucket_url))
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 """
Retrieves credentials for the given file system.

View file

@ -104,6 +104,26 @@ defmodule Livebook.FileSystem.Utils do
|> Enum.join("/")
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([head], _elem), do: [head]
defp remove_in_middle([head | tail], elem), do: remove_in_middle(tail, elem, [head])

View file

@ -272,7 +272,7 @@ defmodule LivebookWeb.Integration.SessionLiveTest do
team_file_system =
build(:fs_s3,
id: FileSystem.S3.id(team_id, bucket_url),
id: Livebook.FileSystemHelpers.s3_id(team_id, bucket_url),
bucket_url: bucket_url,
hub_id: team_id
)
@ -312,7 +312,7 @@ defmodule LivebookWeb.Integration.SessionLiveTest do
file_system =
build(:fs_s3,
id: FileSystem.S3.id(hub_id, bucket_url),
id: Livebook.FileSystemHelpers.s3_id(hub_id, bucket_url),
bucket_url: bucket_url,
hub_id: hub_id,
external_id: "123"

View 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

View file

@ -154,7 +154,7 @@ defmodule Livebook.HubHelpers do
file_system =
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,
region: "auto",
hub_id: hub_id