diff options
author | Maksim Pechnikov <parallel588@gmail.com> | 2020-08-24 09:47:25 +0300 |
---|---|---|
committer | Maksim Pechnikov <parallel588@gmail.com> | 2020-08-24 15:01:45 +0300 |
commit | 14ec12ac956ffa9964254cb3be390c9903103da3 (patch) | |
tree | 1f92e47efcfc53217640525c5ad0ea93458eae91 /lib | |
parent | 0922791e4d2233d527dda23e66a952e3f359a3fe (diff) | |
download | pleroma-14ec12ac956ffa9964254cb3be390c9903103da3.tar.gz |
added tests
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pleroma/emoji.ex | 3 | ||||
-rw-r--r-- | lib/pleroma/emoji/pack.ex | 129 | ||||
-rw-r--r-- | lib/pleroma/utils.ex | 16 |
3 files changed, 77 insertions, 71 deletions
diff --git a/lib/pleroma/emoji.ex b/lib/pleroma/emoji.ex index f6016d73f..04936155b 100644 --- a/lib/pleroma/emoji.ex +++ b/lib/pleroma/emoji.ex @@ -56,6 +56,9 @@ defmodule Pleroma.Emoji do end end + @spec exist?(String.t()) :: boolean() + def exist?(name), do: not is_nil(get(name)) + @doc "Returns all the emojos!!" @spec get_all() :: list({String.t(), String.t(), String.t()}) def get_all do diff --git a/lib/pleroma/emoji/pack.ex b/lib/pleroma/emoji/pack.ex index 03aed33bb..dd79bdfab 100644 --- a/lib/pleroma/emoji/pack.ex +++ b/lib/pleroma/emoji/pack.ex @@ -65,71 +65,73 @@ defmodule Pleroma.Emoji.Pack do 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 @@ -139,12 +141,10 @@ defmodule Pleroma.Emoji.Pack 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 @@ -312,9 +312,10 @@ defmodule Pleroma.Emoji.Pack do 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 @@ -466,7 +467,7 @@ defmodule Pleroma.Emoji.Pack do 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 diff --git a/lib/pleroma/utils.ex b/lib/pleroma/utils.ex index fcb8c64c7..e95766223 100644 --- a/lib/pleroma/utils.ex +++ b/lib/pleroma/utils.ex @@ -28,14 +28,16 @@ defmodule Pleroma.Utils do @doc "creates the uniq temporary directory" @spec tmp_dir(String.t()) :: {:ok, String.t()} | {:error, :file.posix()} def tmp_dir(prefix \\ "") do - sub_dir = [ - prefix, - Timex.to_unix(Timex.now()), - :os.getpid(), - String.downcase(Integer.to_string(:rand.uniform(0x100000000), 36)) - ] + sub_dir = + [ + prefix, + Timex.to_unix(Timex.now()), + :os.getpid(), + String.downcase(Integer.to_string(:rand.uniform(0x100000000), 36)) + ] + |> Enum.join("-") - tmp_dir = Path.join(System.tmp_dir!(), Enum.join(sub_dir, "-")) + tmp_dir = Path.join(System.tmp_dir!(), sub_dir) case File.mkdir(tmp_dir) do :ok -> {:ok, tmp_dir} |