Skip to content

Commit

Permalink
Address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
keianhzo committed Nov 1, 2023
1 parent e4ef4db commit 0b23c97
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 14 deletions.
15 changes: 15 additions & 0 deletions lib/ret/owned_file.ex
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ defmodule Ret.OwnedFile do
end
end

def set_inactive(owned_file_uuid) do
case get_by_uuid(owned_file_uuid) do
nil ->
{:error, :file_not_found}

owned_file ->
set_state(owned_file, :inactive)
end
end

@spec set_inactive(Ret.OwnedFile.t()) :: any()
def set_inactive(%OwnedFile{} = owned_file) do
set_state(owned_file, :inactive)
end
Expand All @@ -69,6 +80,10 @@ defmodule Ret.OwnedFile do
Repo.one(from OwnedFile, where: [owned_file_uuid: ^owned_file_uuid, account_id: ^account_id])
end

def get_by_uuid(owned_file_uuid) do
Repo.one(from OwnedFile, where: [owned_file_uuid: ^owned_file_uuid])
end

defp set_state(nil, _state), do: nil

defp set_state(%OwnedFile{} = owned_file, state) do
Expand Down
21 changes: 12 additions & 9 deletions lib/ret/storage.ex
Original file line number Diff line number Diff line change
Expand Up @@ -233,24 +233,26 @@ defmodule Ret.Storage do
end
end

def promote(_id, nil, nil, _account) do
def promote(id, key, promotion_token, account, require_token \\ true)

def promote(_id, nil, nil, _account, _require_token) do
{:error, :not_allowed}
end

def promote(nil, _key, _promotion_token, _account) do
def promote(nil, _key, _promotion_token, _account, _require_token) do
{:error, :not_found}
end

def promote(_id, nil, _promotion_token, _account) do
def promote(_id, nil, _promotion_token, _account, _require_token) do
{:error, :not_allowed}
end

# Promotes an expiring stored file to a permanently stored file in the specified Account.
def promote(id, key, promotion_token, %Account{} = account) do
def promote(id, key, promotion_token, %Account{} = account, require_token) do
# Check if this file has already been promoted
OwnedFile
|> Repo.get_by(owned_file_uuid: id)
|> promote_or_return_owned_file(id, key, promotion_token, account)
|> promote_or_return_owned_file(id, key, promotion_token, account, require_token)
end

# Promotes multiple files into the given account.
Expand Down Expand Up @@ -283,17 +285,18 @@ defmodule Ret.Storage do
_id,
_key,
_promotion_token,
_account
_account,
_require_token
) do
{:ok, owned_file}
{:ok, owned_file}
end

# Promoting a stored file to being owned has two side effects: the file is moved
# into the owned files directory (which prevents it from being vacuumed) and an
# OwnedFile record is inserted into the database which includes the decryption key.
# If the stored file has an associated promotion token, the given promotion token is verified against it.
# If the given promotion token fails verification, the file is not promoted.
defp promote_or_return_owned_file(nil, id, key, promotion_token, account) do
defp promote_or_return_owned_file(nil, id, key, promotion_token, account, require_token) do
with(
storage_path when is_binary(storage_path) <- module_config(:storage_path),
{:ok, uuid} <- Ecto.UUID.cast(id),
Expand All @@ -307,7 +310,7 @@ defmodule Ret.Storage do
"promotion_token" => actual_promotion_token
} <-
File.read!(meta_file_path) |> Poison.decode!(),
{:ok} <- check_promotion_token(actual_promotion_token, promotion_token),
{:ok} <- (if require_token, do: check_promotion_token(actual_promotion_token, promotion_token), else: {:ok}),
{:ok} <- check_blob_file_key(blob_file_path, key)
) do
owned_file_params = %{
Expand Down
41 changes: 36 additions & 5 deletions lib/ret_web/channels/hub_channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,9 @@ defmodule RetWeb.HubChannel do
def handle_in("save_entity_state", params, socket) do
params = parse(params)

with {:ok, hub, _account} <- authorize(socket, :write_entity_state),
{:ok, %{entity: entity}} <- Ret.create_entity(hub, params) do
with {:ok, hub, account} <- authorize(socket, :write_entity_state),
{:ok, %{entity: entity}} <- Ret.create_entity(hub, params),
:ok <- maybe_promote_file(params, account, socket) do
entity = Repo.preload(entity, [:sub_entities])

broadcast!(
Expand Down Expand Up @@ -497,9 +498,10 @@ defmodule RetWeb.HubChannel do
end
end

def handle_in("delete_entity_state", %{"nid" => nid} = _payload, socket) do
with {:ok, hub, _account} <- authorize(socket, :write_entity_state),
{:ok, _} <- Ret.delete_entity(hub.hub_id, nid) do
def handle_in("delete_entity_state", %{"nid" => nid} = payload, socket) do
with {:ok, hub, account} <- authorize(socket, :write_entity_state),
{:ok, _} <- Ret.delete_entity(hub.hub_id, nid),
{:ok, _} <- maybe_set_owned_file_inactive(payload, account) do
RoomObject.perform_unpin(hub, nid)

broadcast!(socket, "entity_state_deleted", %{
Expand Down Expand Up @@ -1470,6 +1472,35 @@ defmodule RetWeb.HubChannel do
}
end

defp maybe_set_owned_file_inactive(%{"file_id" => file_id}, _account) do
OwnedFile.set_inactive(file_id)
end

defp maybe_set_owned_file_inactive(_payload, _account) do
{:ok, :no_file}
end

defp maybe_promote_file(%{file_id: nil} = _params, _account, _socket) do
:ok
end

defp maybe_promote_file(params, account, _socket) do
with {:ok, _owned_file} <-
Storage.promote(
params.file_id,
params.file_access_token,
nil,
account,
false
) do
OwnedFile.set_active(params.file_id, account.account_id)
:ok
else
{:error, reason} ->
{:error, reason}
end
end

defp reply_error(socket, reason) do
{:reply, {:error, %{reason: reason}}, socket}
end
Expand Down

0 comments on commit 0b23c97

Please sign in to comment.