From aafd7b44ceaf50788f12061ee88071a288155b95 Mon Sep 17 00:00:00 2001 From: Alexander Strizhakov Date: Thu, 24 Dec 2020 20:27:28 +0300 Subject: [PATCH] check dir existence in policy --- lib/pleroma/application.ex | 19 ----------- .../activity_pub/mrf/steal_emoji_policy.ex | 34 ++++++++++++------- 2 files changed, 22 insertions(+), 31 deletions(-) diff --git a/lib/pleroma/application.ex b/lib/pleroma/application.ex index e3edd05ca..bd568d858 100644 --- a/lib/pleroma/application.ex +++ b/lib/pleroma/application.ex @@ -114,9 +114,6 @@ def start(_type, _args) do set_postgres_server_version() - # Requires Config.TransferTask so ConfigDB values are loaded - steal_emoji_policy_setup() - result end @@ -303,20 +300,4 @@ def limiters_setup do [Pleroma.Web.RichMedia.Helpers, Pleroma.Web.MediaProxy] |> Enum.each(&ConcurrentLimiter.new(&1, 1, 0)) end - - @spec steal_emoji_policy_setup() :: :ok - defp steal_emoji_policy_setup() do - with true <- - Pleroma.Web.ActivityPub.MRF.StealEmojiPolicy in Config.get!([:mrf, :policies]) do - path = - [:instance, :static_dir] - |> Config.get!() - |> Path.join("emoji/steal") - - if !File.exists?(path), do: File.mkdir_p!(path) - else - _ -> - :ok - end - end end diff --git a/lib/pleroma/web/activity_pub/mrf/steal_emoji_policy.ex b/lib/pleroma/web/activity_pub/mrf/steal_emoji_policy.ex index 2858af9eb..eabee6542 100644 --- a/lib/pleroma/web/activity_pub/mrf/steal_emoji_policy.ex +++ b/lib/pleroma/web/activity_pub/mrf/steal_emoji_policy.ex @@ -14,18 +14,12 @@ defp remote_host?(host), do: host != Config.get([Pleroma.Web.Endpoint, :url, :ho defp accept_host?(host), do: host in Config.get([:mrf_steal_emoji, :hosts], []) - defp steal_emoji({shortcode, url}) do + defp steal_emoji({shortcode, url}, emoji_dir_path) do url = Pleroma.Web.MediaProxy.url(url) {:ok, response} = Pleroma.HTTP.get(url) size_limit = Config.get([:mrf_steal_emoji, :size_limit], 50_000) if byte_size(response.body) <= size_limit do - emoji_dir_path = - Config.get( - [:mrf_steal_emoji, :path], - Path.join(Config.get([:instance, :static_dir]), "emoji/stolen") - ) - extension = url |> URI.parse() @@ -35,11 +29,9 @@ defp steal_emoji({shortcode, url}) do file_path = Path.join([emoji_dir_path, shortcode <> (extension || ".png")]) - try do - :ok = File.write(file_path, response.body) - + with :ok <- File.write(file_path, response.body) do shortcode - rescue + else e -> Logger.warn("MRF.StealEmojiPolicy: Failed to write to #{file_path}: #{inspect(e)}") nil @@ -66,6 +58,16 @@ def filter(%{"object" => %{"emoji" => foreign_emojis, "actor" => actor}} = messa if remote_host?(host) and accept_host?(host) do installed_emoji = Pleroma.Emoji.get_all() |> Enum.map(fn {k, _} -> k end) + emoji_dir_path = + Config.get( + [:mrf_steal_emoji, :path], + Path.join(Config.get([:instance, :static_dir]), "emoji/stolen") + ) + + if not Config.get([:mrf_steal_emoji, :dir_exists?], false) do + create_dir(emoji_dir_path) + end + new_emojis = foreign_emojis |> Enum.filter(fn {shortcode, _url} -> shortcode not in installed_emoji end) @@ -76,7 +78,7 @@ def filter(%{"object" => %{"emoji" => foreign_emojis, "actor" => actor}} = messa !reject_emoji? end) - |> Enum.map(&steal_emoji(&1)) + |> Enum.map(&steal_emoji(&1, emoji_dir_path)) |> Enum.filter(& &1) if !Enum.empty?(new_emojis) do @@ -94,4 +96,12 @@ def filter(message), do: {:ok, message} def describe do {:ok, %{}} end + + defp create_dir(path) do + if not File.exists?(path) do + File.mkdir_p!(path) + end + + Config.put([:mrf_steal_emoji, :dir_exists?], true) + end end