diff --git a/lib/service/http/rich_media_preview_client.ex b/lib/service/http/rich_media_preview_client.ex new file mode 100644 index 000000000..edd190818 --- /dev/null +++ b/lib/service/http/rich_media_preview_client.ex @@ -0,0 +1,23 @@ +defmodule Mobilizon.Service.HTTP.RichMediaPreviewClient do + @moduledoc """ + Tesla HTTP Basic Client + with JSON middleware + """ + + use Tesla + alias Mobilizon.Config + + @default_opts [ + recv_timeout: 20_000 + ] + + adapter(Tesla.Adapter.Hackney, @default_opts) + + @user_agent Config.instance_user_agent() + + plug(Tesla.Middleware.FollowRedirects) + + plug(Tesla.Middleware.Timeout, timeout: 10_000) + + plug(Tesla.Middleware.Headers, [{"User-Agent", @user_agent}]) +end diff --git a/lib/service/rich_media/parser.ex b/lib/service/rich_media/parser.ex index fa16a328c..c313990f0 100644 --- a/lib/service/rich_media/parser.ex +++ b/lib/service/rich_media/parser.ex @@ -17,6 +17,7 @@ defmodule Mobilizon.Service.RichMedia.Parser do ] alias Mobilizon.Config + alias Mobilizon.Service.HTTP.RichMediaPreviewClient alias Mobilizon.Service.RichMedia.Favicon alias Plug.Conn.Utils require Logger @@ -56,7 +57,7 @@ defmodule Mobilizon.Service.RichMedia.Parser do with {:ok, _} <- prevent_local_address(url), {:ok, %{body: body, status: code, headers: response_headers}} when code in 200..299 <- - Tesla.get( + RichMediaPreviewClient.get( url, headers: headers, opts: @options