+ _ ->
+ {: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}} <- Tesla.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
+
+ defp update_sha_and_save_metadata(pack, data) do
+ with {:ok, %{body: zip}} <- Tesla.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