From 9966469444ea00d7497f0e2f8ca4a812854ba126 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Tue, 20 Oct 2020 09:25:02 +0200 Subject: [PATCH] Use a RichMediaPreviewClient to get details on resources Signed-off-by: Thomas Citharel --- lib/service/http/rich_media_preview_client.ex | 23 +++++++++++++++++++ lib/service/rich_media/parser.ex | 3 ++- 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 lib/service/http/rich_media_preview_client.ex 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