fix content-type and fallback to image/jpeg

This commit is contained in:
href 2017-12-12 11:45:55 +01:00
parent 9093b2cf49
commit 8e82547179
No known key found for this signature in database
GPG Key ID: EE8296C1A152C325
1 changed files with 9 additions and 3 deletions

View File

@ -33,10 +33,11 @@ defp proxy_request(link) do
options = [:insecure, {:follow_redirect, true}] options = [:insecure, {:follow_redirect, true}]
with \ with \
{:ok, 200, headers, client} <- :hackney.request(:get, link, headers, "", options), {:ok, 200, headers, client} <- :hackney.request(:get, link, headers, "", options),
{:ok, body} <- proxy_request_body(client) headers = Enum.into(headers, Map.new),
{:ok, body} <- proxy_request_body(client),
content_type <- proxy_request_content_type(headers, body)
do do
headers = Enum.into(headers, Map.new) {:ok, content_type, body}
{:ok, headers["Content-Type"], body}
else else
{:ok, status, _, _} -> {:ok, status, _, _} ->
Logger.warn "MediaProxy: request failed, status #{status}, link: #{link}" Logger.warn "MediaProxy: request failed, status #{status}, link: #{link}"
@ -73,5 +74,10 @@ defp proxy_request_body(client, _) do
{:error, :body_too_large} {:error, :body_too_large}
end end
# TODO: the body is passed here as well because some hosts do not provide a content-type.
# At some point we may want to use magic numbers to discover the content-type and reply a proper one.
defp proxy_request_content_type(headers, _body) do
headers["Content-Type"] || headers["content-type"] || "image/jpeg"
end
end end