logo

pleroma

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

aws_signed_url.ex (1314B)


  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. {:error, "Not aws signed url #{inspect(image)}"}
  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. String.contains?(host, "amazonaws.com") and String.contains?(query, "X-Amz-Expires")
  21. end
  22. defp aws_signed_url?(_), do: nil
  23. defp parse_query_params(image) do
  24. %URI{query: query} = URI.parse(image)
  25. query
  26. end
  27. defp format_query_params(query) do
  28. query
  29. |> String.split(~r/&|=/)
  30. |> Enum.chunk_every(2)
  31. |> Map.new(fn [k, v] -> {k, v} end)
  32. end
  33. defp get_expiration_timestamp(params) when is_map(params) do
  34. {:ok, date} =
  35. params
  36. |> Map.get("X-Amz-Date")
  37. |> Timex.parse("{ISO:Basic:Z}")
  38. Timex.to_unix(date) + String.to_integer(Map.get(params, "X-Amz-Expires"))
  39. end
  40. end