+ defp fetch_pack_info(remote_pack, uri, name) do
+ case remote_pack["pack"] do
+ %{"share-files" => true, "can-download" => true, "download-sha256" => sha} ->
+ {:ok,
+ %{
+ sha: sha,
+ url: URI.merge(uri, "/api/pleroma/emoji/packs/archive?name=#{name}") |> to_string()
+ }}
+
+ %{"fallback-src" => src, "fallback-src-sha256" => sha} when is_binary(src) ->
+ {:ok,
+ %{
+ sha: sha,
+ url: src,
+ fallback: true
+ }}
+
+ _ ->
+ {:error, "The pack was not set as shared and there is no fallback src to download from"}
+ end
+ end
+
+ defp download_archive(url, sha) do
+ with {:ok, %{body: archive}} <- Pleroma.HTTP.get(url) do
+ if Base.decode16!(sha) == :crypto.hash(:sha256, archive) do
+ {:ok, archive}
+ else
+ {:error, :invalid_checksum}
+ end
+ end
+ end
+
+ 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