projects
/
akkoma
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[akkoma]
/
lib
/
pleroma
/
web
/
rich_media
/
parser.ex
diff --git
a/lib/pleroma/web/rich_media/parser.ex
b/lib/pleroma/web/rich_media/parser.ex
index 76d977ac22a68f02a5f841f072fe998a161211cb..21cd47890abc8a17fa583241cdd8f970a91d3be4 100644
(file)
--- a/
lib/pleroma/web/rich_media/parser.ex
+++ b/
lib/pleroma/web/rich_media/parser.ex
@@
-9,9
+9,16
@@
defmodule Pleroma.Web.RichMedia.Parser do
Pleroma.Web.RichMedia.Parsers.OEmbed
]
Pleroma.Web.RichMedia.Parsers.OEmbed
]
+ @hackney_options [
+ pool: :media,
+ recv_timeout: 2_000,
+ max_body: 2_000_000,
+ with_body: true
+ ]
+
def parse(nil), do: {:error, "No URL provided"}
def parse(nil), do: {:error, "No URL provided"}
- if
Mix.env(
) == :test do
+ if
Pleroma.Config.get(:env
) == :test do
def parse(url), do: parse_url(url)
else
def parse(url) do
def parse(url), do: parse_url(url)
else
def parse(url) do
@@
-28,9
+35,12
@@
defmodule Pleroma.Web.RichMedia.Parser do
defp parse_url(url) do
try do
defp parse_url(url) do
try do
- {:ok, %Tesla.Env{body: html}} = Pleroma.HTTP.get(url)
+ {:ok, %Tesla.Env{body: html}} = Pleroma.HTTP.get(url
, [], adapter: @hackney_options
)
- html |> maybe_parse() |> get_parsed_data()
+ html
+ |> maybe_parse()
+ |> clean_parsed_data()
+ |> check_parsed_data()
rescue
e ->
{:error, "Parsing error: #{inspect(e)}"}
rescue
e ->
{:error, "Parsing error: #{inspect(e)}"}
@@
-46,11
+56,23
@@
defmodule Pleroma.Web.RichMedia.Parser do
end)
end
end)
end
- defp
get
_parsed_data(%{title: title} = data) when is_binary(title) and byte_size(title) > 0 do
+ defp
check
_parsed_data(%{title: title} = data) when is_binary(title) and byte_size(title) > 0 do
{:ok, data}
end
{:ok, data}
end
- defp
get
_parsed_data(data) do
+ defp
check
_parsed_data(data) do
{:error, "Found metadata was invalid or incomplete: #{inspect(data)}"}
end
{:error, "Found metadata was invalid or incomplete: #{inspect(data)}"}
end
+
+ defp clean_parsed_data(data) do
+ data
+ |> Enum.reject(fn {key, val} ->
+ with {:ok, _} <- Jason.encode(%{key => val}) do
+ false
+ else
+ _ -> true
+ end
+ end)
+ |> Map.new()
+ end
end
end