1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 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
10 image = Map.get(data, "image")
12 if aws_signed_url?(image) do
14 |> parse_query_params()
15 |> format_query_params()
16 |> get_expiration_timestamp()
22 defp aws_signed_url?(image) when is_binary(image) and image != "" do
23 %URI{host: host, query: query} = URI.parse(image)
25 is_binary(host) and String.contains?(host, "amazonaws.com") and
26 String.contains?(query, "X-Amz-Expires")
29 defp aws_signed_url?(_), do: nil
31 defp parse_query_params(image) do
32 %URI{query: query} = URI.parse(image)
36 defp format_query_params(query) do
38 |> String.split(~r/&|=/)
39 |> Enum.chunk_every(2)
40 |> Map.new(fn [k, v] -> {k, v} end)
43 defp get_expiration_timestamp(params) when is_map(params) do
46 |> Map.get("X-Amz-Date")
47 |> Timex.parse("{ISO:Basic:Z}")
49 Timex.to_unix(date) + String.to_integer(Map.get(params, "X-Amz-Expires"))