aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEkaterina Vaartis <vaartis@cock.li>2019-09-11 18:59:31 +0300
committerEkaterina Vaartis <vaartis@cock.li>2019-09-19 00:16:33 +0300
commit8f509e6d1ee8955fc430d1f4ed7929ba0d91177c (patch)
tree6be6842a68304ccba8a559a1c2abd29279a3c40a
parent8790365fef9d5f76b7ac1c94933e2ee218e76285 (diff)
downloadpleroma-8f509e6d1ee8955fc430d1f4ed7929ba0d91177c.tar.gz
Use with w/ pack_info_res
-rw-r--r--lib/pleroma/web/emoji_api/emoji_api_controller.ex52
1 files changed, 23 insertions, 29 deletions
diff --git a/lib/pleroma/web/emoji_api/emoji_api_controller.ex b/lib/pleroma/web/emoji_api/emoji_api_controller.ex
index 8ef6ae71f..9e0ff0b28 100644
--- a/lib/pleroma/web/emoji_api/emoji_api_controller.ex
+++ b/lib/pleroma/web/emoji_api/emoji_api_controller.ex
@@ -183,42 +183,36 @@ keeping it in cache for #{div(cache_ms, 1000)}s")
{:error, "The pack was not set as shared and there is no fallback src to download from"}
end
- case pack_info_res do
- {:ok, %{sha: sha, uri: uri} = pinfo} ->
- sha = Base.decode16!(sha)
- emoji_archive = Tesla.get!(uri).body
-
- got_sha = :crypto.hash(:sha256, emoji_archive)
-
- if got_sha == sha do
- local_name = data["as"] || name
- pack_dir = Path.join(@emoji_dir_path, local_name)
- File.mkdir_p!(pack_dir)
-
- # Fallback cannot contain a pack.json file
- files = Enum.map(full_pack["files"], fn {_, path} -> to_charlist(path) end)
- # Fallback cannot contain a pack.json file
- files = if pinfo[:fallback], do: files, else: ['pack.json'] ++ files
+ with {:ok, %{sha: sha, uri: uri} = pinfo} <- pack_info_res,
+ %{body: emoji_archive} <- Tesla.get!(uri),
+ {_, true} <- {:sha, Base.decode16!(sha) == :crypto.hash(:sha256, emoji_archive)} do
+ local_name = data["as"] || name
+ pack_dir = Path.join(@emoji_dir_path, local_name)
+ File.mkdir_p!(pack_dir)
- {:ok, _} = :zip.unzip(emoji_archive, cwd: to_charlist(pack_dir), file_list: files)
+ files = Enum.map(full_pack["files"], fn {_, path} -> to_charlist(path) end)
+ # Fallback cannot contain a pack.json file
+ files = if pinfo[:fallback], do: files, else: ['pack.json'] ++ files
- # Fallback can't contain a pack.json file, since that would cause the fallback-src-sha256
- # in it to depend on itself
- if pinfo[:fallback] do
- pack_file_path = Path.join(pack_dir, "pack.json")
+ {:ok, _} = :zip.unzip(emoji_archive, cwd: to_charlist(pack_dir), file_list: files)
- File.write!(pack_file_path, Jason.encode!(full_pack, pretty: true))
- end
+ # Fallback can't contain a pack.json file, since that would cause the fallback-src-sha256
+ # in it to depend on itself
+ if pinfo[:fallback] do
+ pack_file_path = Path.join(pack_dir, "pack.json")
- conn |> text("ok")
- else
- conn
- |> put_status(:internal_server_error)
- |> text("SHA256 for the pack doesn't match the one sent by the server")
- end
+ File.write!(pack_file_path, Jason.encode!(full_pack, pretty: true))
+ end
+ text(conn, "ok")
+ else
{:error, e} ->
conn |> put_status(:internal_server_error) |> text(e)
+
+ {:sha, _} ->
+ conn
+ |> put_status(:internal_server_error)
+ |> text("SHA256 for the pack doesn't match the one sent by the server")
end
end