summaryrefslogtreecommitdiff
path: root/priv/scrubbers/media_proxy.ex
blob: 9935b38294b2353b2f85c3126707db08472c645f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.HTML.Transform.MediaProxy do
  @moduledoc "Transforms inline image URIs to use MediaProxy."

  alias Pleroma.Web.MediaProxy

  def before_scrub(html), do: html

  def scrub_attribute(:img, {"src", "http" <> target}) do
    media_url =
      ("http" <> target)
      |> MediaProxy.url()

    {"src", media_url}
  end

  def scrub_attribute(_tag, attribute), do: attribute

  def scrub({:img, attributes, children}) do
    attributes =
      attributes
      |> Enum.map(fn attr -> scrub_attribute(:img, attr) end)
      |> Enum.reject(&is_nil(&1))

    {:img, attributes, children}
  end

  def scrub({:comment, _text, _children}), do: ""

  def scrub({tag, attributes, children}), do: {tag, attributes, children}
  def scrub({_tag, children}), do: children
  def scrub(text), do: text
end