1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.RichMedia.Helpers do
8 def rich_media_get(url) do
9 headers = [{"user-agent", Pleroma.Application.user_agent() <> "; Bot"}]
12 case Pleroma.HTTP.head(url, headers, http_options()) do
13 # If the HEAD request didn't reach the server for whatever reason,
14 # we assume the GET that comes right after won't either
18 {:ok, %Tesla.Env{status: 200, headers: headers}} ->
19 with :ok <- check_content_type(headers),
20 :ok <- check_content_length(headers),
27 with :ok <- head_check, do: Pleroma.HTTP.get(url, headers, http_options())
30 defp check_content_type(headers) do
31 case List.keyfind(headers, "content-type", 0) do
33 case Plug.Conn.Utils.media_type(content_type) do
34 {:ok, "text", "html", _} -> :ok
35 _ -> {:error, {:content_type, content_type}}
43 defp check_content_length(headers) do
44 max_body = Keyword.get(http_options(), :max_body)
46 case List.keyfind(headers, "content-length", 0) do
47 {_, maybe_content_length} ->
48 case Integer.parse(maybe_content_length) do
49 {content_length, ""} when content_length <= max_body -> :ok
50 {_, ""} -> {:error, :body_too_large}
62 max_body: Config.get([:rich_media, :max_body], 5_000_000)