diff --git a/lib/camo/image.ex b/lib/camo/image.ex index 5f1bf5a7..5c021970 100644 --- a/lib/camo/image.ex +++ b/lib/camo/image.ex @@ -1,20 +1,28 @@ defmodule Camo.Image do def image_url(input) do - %{host: host} = URI.parse(input) + uri = URI.parse(input) - if !host or String.ends_with?(host, cdn_host()) or is_nil(camo_key()) do - input - else - camo_digest = :crypto.hmac(:sha, camo_key(), input) |> Base.encode16(case: :lower) + cond do + is_nil(uri.host) -> + "" - camo_uri = %URI{ - host: camo_host(), - path: "/" <> camo_digest, - query: URI.encode_query(url: input), - scheme: "https" - } + is_nil(camo_key()) -> + input - URI.to_string(camo_uri) + uri.host in [cdn_host(), camo_host()] -> + URI.to_string(update_in(uri.scheme, fn _ -> "https" end)) + + true -> + camo_digest = :crypto.hmac(:sha, camo_key(), input) |> Base.encode16(case: :lower) + + camo_uri = %URI{ + host: camo_host(), + path: "/" <> camo_digest, + query: URI.encode_query(url: input), + scheme: "https" + } + + URI.to_string(camo_uri) end end