Merge pull request 'Manually define PATH for Arch Linux users in systemd unit' (...
[akkoma] / priv / scrubbers / media_proxy.ex
1 defmodule Pleroma.HTML.Transform.MediaProxy do
2 @moduledoc "Transforms inline image URIs to use MediaProxy."
3
4 alias Pleroma.Web.MediaProxy
5
6 def before_scrub(html), do: html
7
8 def scrub_attribute(:img, {"src", "http" <> target}) do
9 media_url =
10 ("http" <> target)
11 |> MediaProxy.url()
12
13 {"src", media_url}
14 end
15
16 def scrub_attribute(_tag, attribute), do: attribute
17
18 def scrub({:img, attributes, children}) do
19 attributes =
20 attributes
21 |> Enum.map(fn attr -> scrub_attribute(:img, attr) end)
22 |> Enum.reject(&is_nil(&1))
23
24 {:img, attributes, children}
25 end
26
27 def scrub({:comment, _text, _children}), do: ""
28
29 def scrub({tag, attributes, children}), do: {tag, attributes, children}
30 def scrub({_tag, children}), do: children
31 def scrub(text), do: text
32 end