- @spec add_file(String.t(), String.t(), Path.t(), Plug.Upload.t() | String.t()) ::
- {:ok, t()} | {:error, File.posix() | atom()}
- def add_file(name, shortcode, filename, file) do
- with :ok <- validate_not_empty([name, shortcode, filename]),
+ @spec add_file(String.t(), String.t(), Path.t(), Plug.Upload.t()) ::
+ {:ok, t()}
+ | {:error, File.posix() | atom()}
+ def add_file(%Pack{} = pack, _, _, %Plug.Upload{content_type: "application/zip"} = file) do
+ with {:ok, zip_items} <- :zip.table(to_charlist(file.path)) do
+ emojies =
+ for {_, path, s, _, _, _} <- zip_items, elem(s, 2) == :regular do
+ filename = Path.basename(path)
+ shortcode = Path.basename(filename, Path.extname(filename))
+
+ %{
+ path: path,
+ filename: path,
+ shortcode: shortcode,
+ exist: not is_nil(Pleroma.Emoji.get(shortcode))
+ }
+ end
+ |> Enum.group_by(& &1[:exist])
+
+ case Map.get(emojies, false, []) do
+ [_ | _] = new_emojies ->
+ {:ok, tmp_dir} = Pleroma.Utils.tmp_dir("emoji")
+
+ try do
+ {:ok, _emoji_files} =
+ :zip.unzip(
+ to_charlist(file.path),
+ [
+ {:file_list, Enum.map(new_emojies, & &1[:path])},
+ {:cwd, tmp_dir}
+ ]
+ )
+
+ {_, updated_pack} =
+ Enum.map_reduce(new_emojies, pack, fn item, emoji_pack ->
+ emoji_file = %Plug.Upload{
+ filename: item[:filename],
+ path: Path.join(tmp_dir, item[:path])
+ }
+
+ {:ok, updated_pack} =
+ do_add_file(
+ emoji_pack,
+ item[:shortcode],
+ to_string(item[:filename]),
+ emoji_file
+ )
+
+ {item, updated_pack}
+ end)
+
+ Emoji.reload()
+
+ {:ok, updated_pack}
+ after
+ File.rm_rf(tmp_dir)
+ end
+
+ _ ->
+ {:ok, pack}
+ end
+ end
+ end
+
+ def add_file(%Pack{} = pack, shortcode, filename, file) do
+ with :ok <- validate_not_empty([shortcode, filename]),