1 # Pleroma: A lightweight social networking server
2 # Copyright _ 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.RichMedia.Helpers do
10 alias Pleroma.Web.RichMedia.Parser
14 receive_timeout: 2_000
17 @spec validate_page_url(URI.t() | binary()) :: :ok | :error
18 defp validate_page_url(page_url) when is_binary(page_url) do
19 validate_tld = Config.get([Pleroma.Formatter, :validate_tld])
22 |> Linkify.Parser.url?(validate_tld: validate_tld)
23 |> parse_uri(page_url)
26 defp validate_page_url(%URI{host: host, scheme: "https", authority: authority})
27 when is_binary(authority) do
29 host in Config.get([:rich_media, :ignore_hosts], []) ->
32 get_tld(host) in Config.get([:rich_media, :ignore_tld], []) ->
40 defp validate_page_url(_), do: :error
42 defp parse_uri(true, url) do
48 defp parse_uri(_, _), do: :error
57 def fetch_data_for_object(object) do
58 with true <- Config.get([:rich_media, :enabled]),
60 HTML.extract_first_external_url_from_object(object),
61 :ok <- validate_page_url(page_url),
62 {:ok, rich_media} <- Parser.parse(page_url) do
63 %{page_url: page_url, rich_media: rich_media}
69 def fetch_data_for_activity(%Activity{data: %{"type" => "Create"}} = activity) do
70 with true <- Config.get([:rich_media, :enabled]),
71 %Object{} = object <- Object.normalize(activity, fetch: false) do
72 fetch_data_for_object(object)
78 def fetch_data_for_activity(_), do: %{}
80 def rich_media_get(url) do
81 headers = [{"user-agent", Pleroma.Application.user_agent() <> "; Bot"}]
84 case Pleroma.HTTP.head(url, headers, @options) do
85 # If the HEAD request didn't reach the server for whatever reason,
86 # we assume the GET that comes right after won't either
90 {:ok, %Tesla.Env{status: 200, headers: headers}} ->
91 with :ok <- check_content_type(headers),
92 :ok <- check_content_length(headers),
99 with :ok <- head_check, do: Pleroma.HTTP.get(url, headers, @options)
102 defp check_content_type(headers) do
103 case List.keyfind(headers, "content-type", 0) do
105 case Plug.Conn.Utils.media_type(content_type) do
106 {:ok, "text", "html", _} -> :ok
107 _ -> {:error, {:content_type, content_type}}
115 @max_body @options[:max_body]
116 defp check_content_length(headers) do
117 case List.keyfind(headers, "content-length", 0) do
118 {_, maybe_content_length} ->
119 case Integer.parse(maybe_content_length) do
120 {content_length, ""} when content_length <= @max_body -> :ok
121 {_, ""} -> {:error, :body_too_large}