add check for release hash in gitea release body

This commit is contained in:
IsWaffle 2023-03-06 18:28:02 -05:00
parent e7a38ac30d
commit 2467665cbb
5 changed files with 50 additions and 28 deletions

View File

@ -36,6 +36,8 @@ namespace SPT_AKI_Installer.Aki.Core
/// </summary>
public string AkiReleaseDownloadLink { get; set; }
public string AkiReleaseHash { get; set; } = null;
/// <summary>
/// The release download link for the patcher mirror list
/// </summary>

View File

@ -117,7 +117,7 @@ namespace SPT_AKI_Installer.Aki.Core.Tasks
var akiProgress = new Progress<double>((d) => { Progress = (int)Math.Floor(d); });
_data.AkiZipInfo = await DownloadCacheHelper.GetOrDownloadFileAsync("sptaki.zip", _data.AkiReleaseDownloadLink, akiProgress);
_data.AkiZipInfo = await DownloadCacheHelper.GetOrDownloadFileAsync("sptaki.zip", _data.AkiReleaseDownloadLink, akiProgress, _data.AkiReleaseHash);
if (_data.AkiZipInfo == null)
{

View File

@ -1,7 +1,10 @@
using Gitea.Api;
using Gitea.Client;
using Gitea.Model;
using SPT_AKI_Installer.Aki.Core.Model;
using SPT_AKI_Installer.Aki.Helper;
using System;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace SPT_AKI_Installer.Aki.Core.Tasks
@ -37,6 +40,7 @@ namespace SPT_AKI_Installer.Aki.Core.Tasks
_data.PatcherMirrorsLink = comparePatchToAki?.Assets[0].BrowserDownloadUrl;
_data.AkiReleaseDownloadLink = latestAkiRelease.Assets[0].BrowserDownloadUrl;
_data.AkiReleaseHash = FileHashHelper.GetGiteaReleaseHash(latestAkiRelease);
int IntAkiVersion = int.Parse(latestAkiVersion);
int IntGameVersion = int.Parse(_data.OriginalGameVersion);

View File

@ -13,6 +13,32 @@ namespace SPT_AKI_Installer.Aki.Helper
private static string _cachePath = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "spt-installer/cache");
private static bool CheckCache(FileInfo cacheFile, string expectedHash = null)
{
try
{
cacheFile.Refresh();
Directory.CreateDirectory(_cachePath);
if (cacheFile.Exists)
{
if (expectedHash != null && FileHashHelper.CheckHash(cacheFile, expectedHash))
{
return true;
}
cacheFile.Delete();
cacheFile.Refresh();
}
return false;
}
catch
{
return false;
}
}
private static async Task<GenericResult> DownloadFile(FileInfo outputFile, string targetLink, IProgress<double> progress, string expectedHash = null)
{
try
@ -45,19 +71,7 @@ namespace SPT_AKI_Installer.Aki.Helper
{
try
{
cacheFile.Refresh();
Directory.CreateDirectory(_cachePath);
if (cacheFile.Exists)
{
if (expectedHash != null && FileHashHelper.CheckHash(cacheFile, expectedHash))
{
return GenericResult.FromSuccess();
}
cacheFile.Delete();
cacheFile.Refresh();
}
if (CheckCache(cacheFile, expectedHash)) return GenericResult.FromSuccess();
using var patcherFileStream = cacheFile.Open(FileMode.Create);
{
@ -83,19 +97,7 @@ namespace SPT_AKI_Installer.Aki.Helper
{
try
{
cacheFile.Refresh();
Directory.CreateDirectory(_cachePath);
if (cacheFile.Exists)
{
if (expectedHash != null && FileHashHelper.CheckHash(cacheFile, expectedHash))
{
return GenericResult.FromSuccess();
}
cacheFile.Delete();
cacheFile.Refresh();
}
if (CheckCache(cacheFile, expectedHash)) return GenericResult.FromSuccess();
return await DownloadFile(cacheFile, targetLink, progress, expectedHash);
}

View File

@ -1,12 +1,26 @@
using System;
using Gitea.Model;
using System;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
namespace SPT_AKI_Installer.Aki.Helper
{
public static class FileHashHelper
{
public static string GetGiteaReleaseHash(Release release)
{
var regex = Regex.Match(release.Body, @"Release Hash: (?<hash>\S+)");
if (regex.Success)
{
return regex.Groups["hash"].Value;
}
return null;
}
public static bool CheckHash(FileInfo file, string expectedHash)
{
using (MD5 md5Service = MD5.Create())