- struct(__MODULE__, %{files: map["files"], pack: map["pack"]})
- end
-
- defp shareable_packs_available?(uri) do
- uri
- |> URI.merge("/.well-known/nodeinfo")
- |> to_string()
- |> Tesla.get!()
- |> Map.get(:body)
- |> Jason.decode!()
- |> Map.get("links")
- |> List.last()
- |> Map.get("href")
- # Get the actual nodeinfo address and fetch it
- |> Tesla.get!()
- |> Map.get(:body)
- |> Jason.decode!()
- |> get_in(["metadata", "features"])
- |> Enum.member?("shareable_emoji_packs")
+ defp fetch_archive(pack) do
+ hash = :crypto.hash(:md5, File.read!(pack.pack_file))
+
+ case @cachex.get!(:emoji_packs_cache, pack.name) do
+ %{hash: ^hash, pack_data: archive} -> archive
+ _ -> create_archive_and_cache(pack, hash)
+ end
+ end
+
+ defp fallback_sha_changed?(pack, data) do
+ is_binary(data[:"fallback-src"]) and data[:"fallback-src"] != pack.pack["fallback-src"]
+ end
+
+ defp update_sha_and_save_metadata(pack, data) do
+ with {:ok, %{body: zip}} <- Pleroma.HTTP.get(data[:"fallback-src"]),
+ :ok <- validate_has_all_files(pack, zip) do
+ fallback_sha = :sha256 |> :crypto.hash(zip) |> Base.encode16()
+
+ data
+ |> Map.put("fallback-src-sha256", fallback_sha)
+ |> save_metadata(pack)
+ end
+ end
+
+ defp validate_has_all_files(pack, zip) do
+ with {:ok, f_list} <- :zip.unzip(zip, [:memory]) do
+ # Check if all files from the pack.json are in the archive
+ pack.files
+ |> Enum.all?(fn {_, from_manifest} ->
+ List.keyfind(f_list, to_charlist(from_manifest), 0)
+ end)
+ |> if(do: :ok, else: {:error, :incomplete})
+ end