2019-11-11 00:35:52 +01:00
|
|
|
defmodule Camo.Image do
|
|
|
|
def image_url(input) do
|
|
|
|
%{host: host} = URI.parse(input)
|
|
|
|
|
2020-12-06 18:40:38 +01:00
|
|
|
if !host or String.ends_with?(host, cdn_host()) or is_nil(camo_key()) do
|
2019-11-11 00:35:52 +01:00
|
|
|
input
|
|
|
|
else
|
2019-11-11 01:24:58 +01:00
|
|
|
camo_digest = :crypto.hmac(:sha, camo_key(), input) |> Base.encode16(case: :lower)
|
2020-01-11 05:20:19 +01:00
|
|
|
|
2019-11-11 00:35:52 +01:00
|
|
|
camo_uri = %URI{
|
|
|
|
host: camo_host(),
|
|
|
|
path: "/" <> camo_digest,
|
2019-11-11 01:22:41 +01:00
|
|
|
query: URI.encode_query(url: input),
|
2019-11-11 00:35:52 +01:00
|
|
|
scheme: "https"
|
|
|
|
}
|
|
|
|
|
|
|
|
URI.to_string(camo_uri)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp cdn_host do
|
|
|
|
Application.get_env(:philomena, :cdn_host)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp camo_key do
|
|
|
|
Application.get_env(:philomena, :camo_key)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp camo_host do
|
|
|
|
Application.get_env(:philomena, :camo_host)
|
|
|
|
end
|
2020-01-11 05:20:19 +01:00
|
|
|
end
|