Try implementing keeping old server jars
All checks were successful
Build & Test / nix-build (push) Successful in 1m46s

This commit is contained in:
Kodi Craft 2024-10-10 17:55:43 +02:00
parent 02c05122d7
commit d6ea25a64e
Signed by: kodi
GPG Key ID: 69D9EED60B242822

View File

@ -43,19 +43,35 @@ defmodule Amethyst.DataGenerator do
end end
end end
@spec download_server_jar(:latest | String.t(), path) :: {:ok, path} | {:error, term} when path: String.t() @spec get_server_jar(:latest | String.t(), path) :: {:ok, path} | {:error, term} when path: String.t()
def download_server_jar(version, path) do def get_server_jar(version, path) do
case get_version_meta(version) do case get_version_meta(version) do
{:ok, %{"downloads" => %{"server" => %{"url" => url, "sha1" => sha1}}}} -> {:ok, %{"downloads" => %{"server" => %{"url" => url, "sha1" => sha1}}}} ->
Logger.debug("Downloading server jar from #{url}, sha1: #{sha1}") # Check if the file already exists and has the right hash
case Req.get(url, into: File.stream!(path)) do Logger.debug("Checking if server jar exists at #{path} with hash #{sha1}")
{:ok, _} -> {:ok, path} case File.read(path) do
{:error, err} -> {:error, err} {:ok, data} ->
if :crypto.hash(:sha, data) == sha1 do
Logger.debug("Using cached server jar at #{path}")
{:ok, path}
else
Logger.debug("Mismatched hash, redownloading server jar")
download_jar(url, path)
end
{:error, _} -> download_jar(url, path)
end end
{:error, err} -> {:error, err} {:error, err} -> {:error, err}
end end
end end
defp download_jar(url, path) do
Logger.debug("Downloading server jar from #{url}")
case Req.get(url, into: File.stream!(path)) do
{:ok, _} -> {:ok, path}
{:error, err} -> {:error, err}
end
end
@spec get_latest_version() :: @spec get_latest_version() ::
{:error, %{:__exception__ => true, :__struct__ => atom(), optional(atom()) => any()}} {:error, %{:__exception__ => true, :__struct__ => atom(), optional(atom()) => any()}}
| {:ok, any()} | {:ok, any()}