check dir existence in policy

This commit is contained in:
Alexander Strizhakov 2020-12-24 20:27:28 +03:00
parent e02889edb2
commit aafd7b44ce
No known key found for this signature in database
GPG Key ID: 022896A53AEF1381
2 changed files with 22 additions and 31 deletions

View File

@ -114,9 +114,6 @@ defmodule Pleroma.Application do
set_postgres_server_version()
# Requires Config.TransferTask so ConfigDB values are loaded
steal_emoji_policy_setup()
result
end
@ -303,20 +300,4 @@ defmodule Pleroma.Application 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

View File

@ -14,18 +14,12 @@ defmodule Pleroma.Web.ActivityPub.MRF.StealEmojiPolicy do
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 @@ defmodule Pleroma.Web.ActivityPub.MRF.StealEmojiPolicy 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 @@ defmodule Pleroma.Web.ActivityPub.MRF.StealEmojiPolicy do
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 @@ defmodule Pleroma.Web.ActivityPub.MRF.StealEmojiPolicy do
!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 @@ defmodule Pleroma.Web.ActivityPub.MRF.StealEmojiPolicy do
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