logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

aws_signed_url.ex (1296B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.RichMedia.Parser.TTL.AwsSignedUrl do
  5. @behaviour Pleroma.Web.RichMedia.Parser.TTL
  6. @impl true
  7. def ttl(data, _url) do
  8. image = Map.get(data, "image")
  9. if aws_signed_url?(image) do
  10. image
  11. |> parse_query_params()
  12. |> format_query_params()
  13. |> get_expiration_timestamp()
  14. else
  15. nil
  16. end
  17. end
  18. defp aws_signed_url?(image) when is_binary(image) and image != "" do
  19. %URI{host: host, query: query} = URI.parse(image)
  20. is_binary(host) and String.contains?(host, "amazonaws.com") and
  21. String.contains?(query, "X-Amz-Expires")
  22. end
  23. defp aws_signed_url?(_), do: nil
  24. defp parse_query_params(image) do
  25. %URI{query: query} = URI.parse(image)
  26. query
  27. end
  28. defp format_query_params(query) do
  29. query
  30. |> String.split(~r/&|=/)
  31. |> Enum.chunk_every(2)
  32. |> Map.new(fn [k, v] -> {k, v} end)
  33. end
  34. defp get_expiration_timestamp(params) when is_map(params) do
  35. {:ok, date} =
  36. params
  37. |> Map.get("X-Amz-Date")
  38. |> Timex.parse("{ISO:Basic:Z}")
  39. Timex.to_unix(date) + String.to_integer(Map.get(params, "X-Amz-Expires"))
  40. end
  41. end