2019-11-11 00:35:52 +01:00
|
|
|
defmodule Camo.Image do
|
|
|
|
def image_url(input) do
|
2021-03-22 17:53:23 +01:00
|
|
|
uri = URI.parse(input)
|
2019-11-11 00:35:52 +01:00
|
|
|
|
2021-03-22 17:53:23 +01:00
|
|
|
cond do
|
|
|
|
is_nil(uri.host) ->
|
|
|
|
""
|
2020-01-11 05:20:19 +01:00
|
|
|
|
2021-03-22 17:53:23 +01:00
|
|
|
is_nil(camo_key()) ->
|
|
|
|
input
|
2019-11-11 00:35:52 +01:00
|
|
|
|
2021-03-22 17:53:23 +01:00
|
|
|
uri.host in [cdn_host(), camo_host()] ->
|
2021-03-22 19:37:17 +01:00
|
|
|
URI.to_string(%{uri | scheme: "https", port: 443})
|
2021-03-22 17:53:23 +01:00
|
|
|
|
|
|
|
true ->
|
2021-09-16 17:00:46 +02:00
|
|
|
camo_digest = :crypto.mac(:hmac, :sha, camo_key(), input) |> Base.url_encode64(padding: false)
|
2021-03-22 17:53:23 +01:00
|
|
|
|
|
|
|
camo_uri = %URI{
|
|
|
|
host: camo_host(),
|
2021-09-16 17:00:46 +02:00
|
|
|
path: "/" <> camo_digest <> "/" <> Base.url_encode64(input, padding: false),
|
2021-03-22 17:53:23 +01:00
|
|
|
scheme: "https"
|
|
|
|
}
|
|
|
|
|
|
|
|
URI.to_string(camo_uri)
|
2019-11-11 00:35:52 +01:00
|
|
|
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
|