aboutsummaryrefslogtreecommitdiff
path: root/priv/scrubbers/media_proxy.ex
diff options
context:
space:
mode:
authordcc <dcc@logografos.com>2023-09-02 00:52:52 -0700
committerdcc <dcc@logografos.com>2023-09-02 00:52:52 -0700
commit3a4773c3c2bd0bbef244eb519b07208da9108e49 (patch)
tree973567a6f3abb37bfb0f785b1cad14ed55840ef5 /priv/scrubbers/media_proxy.ex
downloadanni-3a4773c3c2bd0bbef244eb519b07208da9108e49.tar.gz
anni-3a4773c3c2bd0bbef244eb519b07208da9108e49.tar.bz2
anni-3a4773c3c2bd0bbef244eb519b07208da9108e49.zip
First
Diffstat (limited to 'priv/scrubbers/media_proxy.ex')
-rw-r--r--priv/scrubbers/media_proxy.ex36
1 files changed, 36 insertions, 0 deletions
diff --git a/priv/scrubbers/media_proxy.ex b/priv/scrubbers/media_proxy.ex
new file mode 100644
index 0000000..9935b38
--- /dev/null
+++ b/priv/scrubbers/media_proxy.ex
@@ -0,0 +1,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