mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-30 14:57:59 +01:00
283ec72743
* update docker deps * dep updates and warning fixes * update npm deps
40 lines
835 B
Elixir
40 lines
835 B
Elixir
defmodule Camo.Image do
|
|
def image_url(input) do
|
|
uri = URI.parse(input)
|
|
|
|
cond do
|
|
is_nil(uri.host) ->
|
|
""
|
|
|
|
is_nil(camo_key()) ->
|
|
input
|
|
|
|
uri.host in [cdn_host(), camo_host()] ->
|
|
URI.to_string(%{uri | scheme: "https", port: 443})
|
|
|
|
true ->
|
|
camo_digest = :crypto.mac(: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
|
|
|
|
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
|
|
end
|