Use with w/ pack_info_res

This commit is contained in:
Ekaterina Vaartis 2019-09-11 18:59:31 +03:00
parent 8790365fef
commit 8f509e6d1e
1 changed files with 23 additions and 29 deletions

View File

@ -183,19 +183,13 @@ def download_from(conn, %{"instance_address" => address, "pack_name" => name} =
{:error, "The pack was not set as shared and there is no fallback src to download from"} {:error, "The pack was not set as shared and there is no fallback src to download from"}
end end
case pack_info_res do with {:ok, %{sha: sha, uri: uri} = pinfo} <- pack_info_res,
{:ok, %{sha: sha, uri: uri} = pinfo} -> %{body: emoji_archive} <- Tesla.get!(uri),
sha = Base.decode16!(sha) {_, true} <- {:sha, Base.decode16!(sha) == :crypto.hash(:sha256, emoji_archive)} do
emoji_archive = Tesla.get!(uri).body
got_sha = :crypto.hash(:sha256, emoji_archive)
if got_sha == sha do
local_name = data["as"] || name local_name = data["as"] || name
pack_dir = Path.join(@emoji_dir_path, local_name) pack_dir = Path.join(@emoji_dir_path, local_name)
File.mkdir_p!(pack_dir) File.mkdir_p!(pack_dir)
# Fallback cannot contain a pack.json file
files = Enum.map(full_pack["files"], fn {_, path} -> to_charlist(path) end) files = Enum.map(full_pack["files"], fn {_, path} -> to_charlist(path) end)
# Fallback cannot contain a pack.json file # Fallback cannot contain a pack.json file
files = if pinfo[:fallback], do: files, else: ['pack.json'] ++ files files = if pinfo[:fallback], do: files, else: ['pack.json'] ++ files
@ -210,16 +204,16 @@ def download_from(conn, %{"instance_address" => address, "pack_name" => name} =
File.write!(pack_file_path, Jason.encode!(full_pack, pretty: true)) File.write!(pack_file_path, Jason.encode!(full_pack, pretty: true))
end end
conn |> text("ok") text(conn, "ok")
else else
{:error, e} ->
conn |> put_status(:internal_server_error) |> text(e)
{:sha, _} ->
conn conn
|> put_status(:internal_server_error) |> put_status(:internal_server_error)
|> text("SHA256 for the pack doesn't match the one sent by the server") |> text("SHA256 for the pack doesn't match the one sent by the server")
end end
{:error, e} ->
conn |> put_status(:internal_server_error) |> text(e)
end
end end
@doc """ @doc """