Emoji.Pack: Refactor and use safe_unzip.

This commit is contained in:
Lain Soykaf 2025-08-07 13:51:19 +04:00
commit b249340fce

View file

@ -226,44 +226,73 @@ defmodule Pleroma.Emoji.Pack do
end end
def download_zip(name, opts \\ %{}) do def download_zip(name, opts \\ %{}) do
pack_path = with :ok <- validate_not_empty([name]),
path_join_name_safe( :ok <- validate_new_pack(name),
Path.join(Pleroma.Config.get!([:instance, :static_dir]), "emoji"), {:ok, archive_data} <- fetch_archive_data(opts),
name pack_path <- path_join_name_safe(emoji_path(), name),
) :ok <- File.mkdir_p(pack_path),
:ok <- safe_unzip(archive_data, pack_path) do
with {_, false} <- ensure_pack_json(pack_path, archive_data, opts)
{"Pack already exists, refusing to import #{name}", File.exists?(pack_path)}, else
{_, :ok} <- {"Could not create the pack directory", File.mkdir_p(pack_path)}, {:error, reason} when is_binary(reason) -> {:error, reason}
{_, {:ok, %{body: binary_archive}}} <- _ -> {:error, "Could not process pack"}
(case opts do end
%{url: url} ->
{"Could not download pack", Pleroma.HTTP.get(url)}
%{file: file} ->
case File.read(file.path) do
{:ok, data} -> {nil, {:ok, %{body: data}}}
{:error, _e} -> {"Could not read the uploaded pack file", :error}
end end
_ -> defp safe_unzip(archive_data, pack_path) do
{"Neither file nor URL was present in the request", :error} case SafeZip.unzip_data(archive_data, pack_path) do
end), {:ok, _} -> :ok
{_, {:ok, _}} <- {:error, reason} when is_binary(reason) -> {:error, reason}
{"Could not unzip pack", _ -> {:error, "Could not unzip pack"}
:zip.unzip(binary_archive, cwd: String.to_charlist(pack_path))} do end
pack_json_path = Path.join([pack_path, "pack.json"]) end
# Make a json if it does not exist
defp validate_new_pack(name) do
pack_path = path_join_name_safe(emoji_path(), name)
if File.exists?(pack_path) do
{:error, "Pack already exists, refusing to import #{name}"}
else
:ok
end
end
defp fetch_archive_data(%{url: url}) do
case Pleroma.HTTP.get(url) do
{:ok, %{status: 200, body: data}} -> {:ok, data}
_ -> {:error, "Could not download pack"}
end
end
defp fetch_archive_data(%{file: %Plug.Upload{path: path}}) do
case File.read(path) do
{:ok, data} -> {:ok, data}
_ -> {:error, "Could not read the uploaded pack file"}
end
end
defp fetch_archive_data(_) do
{:error, "Neither file nor URL was present in the request"}
end
defp ensure_pack_json(pack_path, archive_data, opts) do
pack_json_path = Path.join(pack_path, "pack.json")
if not File.exists?(pack_json_path) do if not File.exists?(pack_json_path) do
# Make a list of the emojis create_pack_json(pack_path, pack_json_path, archive_data, opts)
end
:ok
end
defp create_pack_json(pack_path, pack_json_path, archive_data, opts) do
emoji_map = emoji_map =
Pleroma.Emoji.Loader.make_shortcode_to_file_map( Pleroma.Emoji.Loader.make_shortcode_to_file_map(
pack_path, pack_path,
Map.get(opts, :exts, [".png", ".gif", ".jpg"]) Map.get(opts, :exts, [".png", ".gif", ".jpg"])
) )
# Calculate the pack SHA. Only needed when there's no pack.json, as it would already include a hash archive_sha = :crypto.hash(:sha256, archive_data) |> Base.encode16()
archive_sha = :crypto.hash(:sha256, binary_archive) |> Base.encode16()
pack_json = %{ pack_json = %{
pack: %{ pack: %{
@ -279,12 +308,6 @@ defmodule Pleroma.Emoji.Pack do
File.write!(pack_json_path, Jason.encode!(pack_json, pretty: true)) File.write!(pack_json_path, Jason.encode!(pack_json, pretty: true))
end end
:ok
else
{err, _} -> {:error, err}
end
end
@spec download(String.t(), String.t(), String.t()) :: {:ok, t()} | {:error, atom()} @spec download(String.t(), String.t(), String.t()) :: {:ok, t()} | {:error, atom()}
def download(name, url, as) do def download(name, url, as) do
uri = url |> String.trim() |> URI.parse() uri = url |> String.trim() |> URI.parse()