1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.RichMedia.Parser.TTL.AwsSignedUrl do
6 @behaviour Pleroma.Web.RichMedia.Parser.TTL
8 @impl Pleroma.Web.RichMedia.Parser.TTL
10 image = Map.get(data, :image)
12 if is_aws_signed_url(image) do
14 |> parse_query_params()
15 |> format_query_params()
16 |> get_expiration_timestamp()
18 {:error, "Not aws signed url #{inspect(image)}"}
22 defp is_aws_signed_url(image) when is_binary(image) and image != "" do
23 %URI{host: host, query: query} = URI.parse(image)
25 String.contains?(host, "amazonaws.com") and String.contains?(query, "X-Amz-Expires")
28 defp is_aws_signed_url(_), do: nil
30 defp parse_query_params(image) do
31 %URI{query: query} = URI.parse(image)
35 defp format_query_params(query) do
37 |> String.split(~r/&|=/)
38 |> Enum.chunk_every(2)
39 |> Map.new(fn [k, v] -> {k, v} end)
42 defp get_expiration_timestamp(params) when is_map(params) do
45 |> Map.get("X-Amz-Date")
46 |> Timex.parse("{ISO:Basic:Z}")
48 {:ok, Timex.to_unix(date) + String.to_integer(Map.get(params, "X-Amz-Expires"))}