Refactor Fetcher.get_object for readability
Apart from slightly different error reasons wrt content-type, this does not change functionality in any way.
This commit is contained in:
parent
ddd79ff22d
commit
c806adbfdb
1 changed files with 24 additions and 22 deletions
|
@ -275,37 +275,39 @@ def get_object(id) do
|
||||||
|> maybe_date_fetch(date)
|
|> maybe_date_fetch(date)
|
||||||
|> sign_fetch(id, date)
|
|> sign_fetch(id, date)
|
||||||
|
|
||||||
case HTTP.get(id, headers) do
|
with {:ok, %{body: body, status: code, headers: headers}} when code in 200..299 <-
|
||||||
{:ok, %{body: body, status: code, headers: headers}} when code in 200..299 ->
|
HTTP.get(id, headers),
|
||||||
case List.keyfind(headers, "content-type", 0) do
|
{:has_content_type, {_, content_type}} <-
|
||||||
{_, content_type} ->
|
{:has_content_type, List.keyfind(headers, "content-type", 0)},
|
||||||
case Plug.Conn.Utils.media_type(content_type) do
|
{:parse_content_type, {:ok, "application", subtype, type_params}} <-
|
||||||
{:ok, "application", "activity+json", _} ->
|
{:parse_content_type, Plug.Conn.Utils.media_type(content_type)} do
|
||||||
|
case {subtype, type_params} do
|
||||||
|
{"activity+json", _} ->
|
||||||
{:ok, body}
|
{:ok, body}
|
||||||
|
|
||||||
{:ok, "application", "ld+json",
|
{"ld+json", %{"profile" => "https://www.w3.org/ns/activitystreams"}} ->
|
||||||
%{"profile" => "https://www.w3.org/ns/activitystreams"}} ->
|
|
||||||
{:ok, body}
|
{:ok, body}
|
||||||
|
|
||||||
# pixelfed sometimes (and only sometimes) responds with http instead of https
|
# pixelfed sometimes (and only sometimes) responds with http instead of https
|
||||||
{:ok, "application", "ld+json",
|
{"ld+json", %{"profile" => "http://www.w3.org/ns/activitystreams"}} ->
|
||||||
%{"profile" => "http://www.w3.org/ns/activitystreams"}} ->
|
|
||||||
{:ok, body}
|
{:ok, body}
|
||||||
|
|
||||||
_ ->
|
_ ->
|
||||||
{:error, {:content_type, content_type}}
|
{:error, {:content_type, content_type}}
|
||||||
end
|
end
|
||||||
|
else
|
||||||
_ ->
|
|
||||||
{:error, {:content_type, nil}}
|
|
||||||
end
|
|
||||||
|
|
||||||
{:ok, %{status: code}} when code in [404, 410] ->
|
{:ok, %{status: code}} when code in [404, 410] ->
|
||||||
{:error, {"Object has been deleted", id, code}}
|
{:error, {"Object has been deleted", id, code}}
|
||||||
|
|
||||||
{:error, e} ->
|
{:error, e} ->
|
||||||
{:error, e}
|
{:error, e}
|
||||||
|
|
||||||
|
{:has_content_type, _} ->
|
||||||
|
{:error, {:content_type, nil}}
|
||||||
|
|
||||||
|
{:parse_content_type, e} ->
|
||||||
|
{:error, {:content_type, e}}
|
||||||
|
|
||||||
e ->
|
e ->
|
||||||
{:error, e}
|
{:error, e}
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue