end
end
+ @spec unpack_zip_emojies(list(tuple())) :: list(map())
+ defp unpack_zip_emojies(zip_files) do
+ Enum.reduce(zip_files, [], fn
+ {_, path, s, _, _, _}, acc when elem(s, 2) == :regular ->
+ with(
+ filename <- Path.basename(path),
+ shortcode <- Path.basename(filename, Path.extname(filename)),
+ false <- Emoji.exist?(shortcode)
+ ) do
+ acc ++ [%{path: path, filename: path, shortcode: shortcode}]
+ else
+ _ -> acc
+ end
+
+ _, acc ->
+ acc
+ end)
+ end
+
@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}
- ]
+ with {:ok, zip_files} <- :zip.table(to_charlist(file.path)),
+ [_ | _] = emojies <- unpack_zip_emojies(zip_files),
+ {:ok, tmp_dir} <- Pleroma.Utils.tmp_dir("emoji") do
+ try do
+ {:ok, _emoji_files} =
+ :zip.unzip(
+ to_charlist(file.path),
+ [{:file_list, Enum.map(emojies, & &1[:path])}, {:cwd, tmp_dir}]
+ )
+
+ {_, updated_pack} =
+ Enum.map_reduce(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
)
- {_, 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
+ {item, updated_pack}
+ end)
+
+ Emoji.reload()
- _ ->
- {:ok, pack}
+ {:ok, updated_pack}
+ after
+ File.rm_rf(tmp_dir)
end
+ else
+ {:error, _} = error ->
+ error
+
+ _ ->
+ {:ok, pack}
end
end
- def add_file(%Pack{} = pack, shortcode, filename, file) do
+ def add_file(%Pack{} = pack, shortcode, filename, %Plug.Upload{} = file) do
with :ok <- validate_not_empty([shortcode, filename]),
:ok <- validate_emoji_not_exists(shortcode),
{:ok, updated_pack} <- do_add_file(pack, shortcode, filename, file) do
end
defp do_add_file(pack, shortcode, filename, file) do
- with :ok <- save_file(file, pack, filename),
- {:ok, updated_pack} <-
- pack
- |> put_emoji(shortcode, filename)
- |> save_pack() do
- {:ok, updated_pack}
+ with :ok <- save_file(file, pack, filename) do
+ pack
+ |> put_emoji(shortcode, filename)
+ |> save_pack()
end
end
defp validate_emoji_not_exists(_shortcode, true), do: :ok
defp validate_emoji_not_exists(shortcode, _) do
- case Emoji.get(shortcode) do
- nil -> :ok
- _ -> {:error, :already_exists}
+ if Emoji.exist?(shortcode) do
+ {:error, :already_exists}
+ else
+ :ok
end
end
defp put_emoji(pack, shortcode, filename) do
files = Map.put(pack.files, shortcode, filename)
- %{pack | files: files}
+ %{pack | files: files, files_count: length(Map.keys(files))}
end
defp delete_emoji(pack, shortcode) do
--- /dev/null
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Emoji.PackTest do
+ use ExUnit.Case, async: true
+ alias Pleroma.Emoji.Pack
+
+ @emoji_path Path.join(
+ Pleroma.Config.get!([:instance, :static_dir]),
+ "emoji"
+ )
+
+ setup do
+ pack_path = Path.join(@emoji_path, "dump_pack")
+ File.mkdir(pack_path)
+
+ File.write!(Path.join(pack_path, "pack.json"), """
+ {
+ "files": { },
+ "pack": {
+ "description": "Dump pack", "homepage": "https://pleroma.social",
+ "license": "Test license", "share-files": true
+ }}
+ """)
+
+ {:ok, pack} = Pleroma.Emoji.Pack.load_pack("dump_pack")
+
+ on_exit(fn ->
+ File.rm_rf!(pack_path)
+ end)
+
+ {:ok, pack: pack}
+ end
+
+ describe "add_file/4" do
+ test "add emojies from zip file", %{pack: pack} do
+ file = %Plug.Upload{
+ content_type: "application/zip",
+ filename: "finland-emojis.zip",
+ path: Path.absname("test/fixtures/finland-emojis.zip")
+ }
+
+ {:ok, updated_pack} = Pack.add_file(pack, nil, nil, file)
+
+ assert updated_pack.files == %{
+ "a_trusted_friend-128" => "128px/a_trusted_friend-128.png",
+ "auroraborealis" => "auroraborealis.png",
+ "baby_in_a_box" => "1000px/baby_in_a_box.png",
+ "bear" => "1000px/bear.png",
+ "bear-128" => "128px/bear-128.png"
+ }
+
+ assert updated_pack.files_count == 5
+ end
+ end
+
+ test "returns error when zip file is bad", %{pack: pack} do
+ file = %Plug.Upload{
+ content_type: "application/zip",
+ filename: "finland-emojis.zip",
+ path: Path.absname("test/instance_static/emoji/test_pack/blank.png")
+ }
+
+ assert Pack.add_file(pack, nil, nil, file) == {:error, :einval}
+ end
+
+ test "returns pack when zip file is empty", %{pack: pack} do
+ file = %Plug.Upload{
+ content_type: "application/zip",
+ filename: "finland-emojis.zip",
+ path: Path.absname("test/fixtures/empty.zip")
+ }
+
+ {:ok, updated_pack} = Pack.add_file(pack, nil, nil, file)
+ assert updated_pack == pack
+ end
+
+ test "add emoji file", %{pack: pack} do
+ file = %Plug.Upload{
+ filename: "blank.png",
+ path: "#{@emoji_path}/test_pack/blank.png"
+ }
+
+ {:ok, updated_pack} = Pack.add_file(pack, "test_blank", "test_blank.png", file)
+
+ assert updated_pack.files == %{
+ "test_blank" => "test_blank.png"
+ }
+
+ assert updated_pack.files_count == 1
+ end
+end