From 2ebc5963218811a32695a458cd5ae5f39012cd4a Mon Sep 17 00:00:00 2001 From: "waffle.lord" Date: Mon, 11 Jul 2022 21:22:26 -0400 Subject: [PATCH] add file hash to mirror list --- .../EftPatchHelper/Interfaces/IFileUpload.cs | 1 + .../EftPatchHelper/Model/DownloadMirror.cs | 8 +++++++ .../EftPatchHelper/Model/GoFileUpload.cs | 8 +++---- .../EftPatchHelper/Model/MegaUpload.cs | 14 ++++++------ .../EftPatchHelper/Model/Options.cs | 2 +- .../Tasks/CreateReleaseTasks.cs | 10 ++++----- .../EftPatchHelper/Tasks/UploadTasks.cs | 22 +++++++++++++++++-- 7 files changed, 46 insertions(+), 19 deletions(-) create mode 100644 EftPatchHelper/EftPatchHelper/Model/DownloadMirror.cs diff --git a/EftPatchHelper/EftPatchHelper/Interfaces/IFileUpload.cs b/EftPatchHelper/EftPatchHelper/Interfaces/IFileUpload.cs index 307e052..3dfcdd1 100644 --- a/EftPatchHelper/EftPatchHelper/Interfaces/IFileUpload.cs +++ b/EftPatchHelper/EftPatchHelper/Interfaces/IFileUpload.cs @@ -11,6 +11,7 @@ namespace EftPatchHelper.Interfaces public string DisplayName { get; set; } public string ServiceName { get; set; } public string HubEntryText { get; set; } + public FileInfo UploadFileInfo { get; } public string GetLink(); public Task UploadAsync(IProgress? progress = null); } diff --git a/EftPatchHelper/EftPatchHelper/Model/DownloadMirror.cs b/EftPatchHelper/EftPatchHelper/Model/DownloadMirror.cs new file mode 100644 index 0000000..9e4efcd --- /dev/null +++ b/EftPatchHelper/EftPatchHelper/Model/DownloadMirror.cs @@ -0,0 +1,8 @@ +namespace EftPatchHelper.Model +{ + public class DownloadMirror + { + public string Link { get; set; } + public string Hash { get; set; } + } +} diff --git a/EftPatchHelper/EftPatchHelper/Model/GoFileUpload.cs b/EftPatchHelper/EftPatchHelper/Model/GoFileUpload.cs index 8b356ec..b86da33 100644 --- a/EftPatchHelper/EftPatchHelper/Model/GoFileUpload.cs +++ b/EftPatchHelper/EftPatchHelper/Model/GoFileUpload.cs @@ -6,7 +6,7 @@ namespace EftPatchHelper.Model { public class GoFileUpload : IFileUpload { - private FileInfo _file; + public FileInfo UploadFileInfo { get; private set; } private GoFileFile _uploadedFile; public string DisplayName { get; set; } @@ -16,9 +16,9 @@ namespace EftPatchHelper.Model public GoFileUpload(FileInfo file, string apiToken) { GoFile.ApiToken = apiToken; - _file = file; + UploadFileInfo = file; ServiceName = "GoFile"; - DisplayName = $"{ServiceName} Upload: {_file.Name}"; + DisplayName = $"{ServiceName} Upload: {UploadFileInfo.Name}"; HubEntryText = $"Download from {ServiceName}"; } @@ -29,7 +29,7 @@ namespace EftPatchHelper.Model public async Task UploadAsync(IProgress? progress = null) { - var uploadedFile = await GoFile.UploadFileAsync(_file, progress); + var uploadedFile = await GoFile.UploadFileAsync(UploadFileInfo, progress); if(uploadedFile == null) return false; diff --git a/EftPatchHelper/EftPatchHelper/Model/MegaUpload.cs b/EftPatchHelper/EftPatchHelper/Model/MegaUpload.cs index f82e427..18102c7 100644 --- a/EftPatchHelper/EftPatchHelper/Model/MegaUpload.cs +++ b/EftPatchHelper/EftPatchHelper/Model/MegaUpload.cs @@ -6,7 +6,7 @@ namespace EftPatchHelper.Model { public class MegaUpload : IFileUpload, IDisposable { - private FileInfo _file; + public FileInfo UploadFileInfo { get; private set; } private MegaApiClient _client; private string _email; private string _password; @@ -21,11 +21,11 @@ namespace EftPatchHelper.Model public MegaUpload(FileInfo file, string email, string password, string mfaKey = null) { _client = new MegaApiClient(); - _file = file; + UploadFileInfo = file; _email = email; _password = password; ServiceName = "Mega"; - DisplayName = $"{ServiceName} Upload: {_file.Name}"; + DisplayName = $"{ServiceName} Upload: {UploadFileInfo.Name}"; HubEntryText = $"Download from {ServiceName}"; } @@ -76,18 +76,18 @@ namespace EftPatchHelper.Model public async Task UploadAsync(IProgress? progress = null) { - _file.Refresh(); + UploadFileInfo.Refresh(); - if (!_file.Exists) return false; + if (!UploadFileInfo.Exists) return false; if(!await CheckLoginStatus()) { return false; } - using var fileStream = _file.OpenRead(); + using var fileStream = UploadFileInfo.OpenRead(); - _uploadedFile = await _client.UploadAsync(fileStream, _file.Name, _uploadFolder, progress); + _uploadedFile = await _client.UploadAsync(fileStream, UploadFileInfo.Name, _uploadFolder, progress); return _uploadedFile != null; } diff --git a/EftPatchHelper/EftPatchHelper/Model/Options.cs b/EftPatchHelper/EftPatchHelper/Model/Options.cs index 0675008..bc289f3 100644 --- a/EftPatchHelper/EftPatchHelper/Model/Options.cs +++ b/EftPatchHelper/EftPatchHelper/Model/Options.cs @@ -42,6 +42,6 @@ namespace EftPatchHelper.Model /// /// List of mirrors to upload to Gitea /// - public Dictionary MirrorList = new Dictionary(); + public Dictionary MirrorList = new Dictionary(); } } diff --git a/EftPatchHelper/EftPatchHelper/Tasks/CreateReleaseTasks.cs b/EftPatchHelper/EftPatchHelper/Tasks/CreateReleaseTasks.cs index 6558666..0355a79 100644 --- a/EftPatchHelper/EftPatchHelper/Tasks/CreateReleaseTasks.cs +++ b/EftPatchHelper/EftPatchHelper/Tasks/CreateReleaseTasks.cs @@ -84,7 +84,7 @@ namespace EftPatchHelper.Tasks public bool CreateMirrorList(FileInfo mirrorListFileInfo) { - List mirrors = _options.MirrorList.Values.ToList(); + List mirrors = _options.MirrorList.Values.ToList(); string json = JsonSerializer.Serialize(mirrors, new JsonSerializerOptions() { WriteIndented = true }); @@ -99,6 +99,10 @@ namespace EftPatchHelper.Tasks { AnsiConsole.WriteLine(); + var fileInfo = new FileInfo(Path.Join(Environment.CurrentDirectory, "mirrors.json")); + + CreateMirrorList(fileInfo); + if (!_options.CreateRelease) return; Configuration.Default.BasePath = _settings.GiteaApiBasePath; @@ -107,10 +111,6 @@ namespace EftPatchHelper.Tasks var repo = new RepositoryApi(Configuration.Default); - var fileInfo = new FileInfo(Path.Join(Environment.CurrentDirectory, "mirrors.json")); - - CreateMirrorList(fileInfo); - var release = MakeRelease(repo).ValidateOrExit(); UploadAsset(fileInfo, release, repo); diff --git a/EftPatchHelper/EftPatchHelper/Tasks/UploadTasks.cs b/EftPatchHelper/EftPatchHelper/Tasks/UploadTasks.cs index 1f10294..ef351b8 100644 --- a/EftPatchHelper/EftPatchHelper/Tasks/UploadTasks.cs +++ b/EftPatchHelper/EftPatchHelper/Tasks/UploadTasks.cs @@ -2,6 +2,7 @@ using EftPatchHelper.Interfaces; using EftPatchHelper.Model; using Spectre.Console; +using System.Security.Cryptography; namespace EftPatchHelper.Tasks { @@ -18,6 +19,17 @@ namespace EftPatchHelper.Tasks _settings = settings; } + private static string GetFileHash(FileInfo file) + { + using (MD5 md5Service = MD5.Create()) + using (var sourceStream = file.OpenRead()) + { + byte[] sourceHash = md5Service.ComputeHash(sourceStream); + + return Convert.ToBase64String(sourceHash); + } + } + private async Task BuildUploadList() { var patcherFile = new FileInfo(_options.OutputPatchPath + ".zip"); @@ -47,7 +59,7 @@ namespace EftPatchHelper.Tasks foreach (var pair in _options.MirrorList) { var displayText = pair.Key; - var link = pair.Value; + var link = pair.Value.Link; if(link.Contains("gofile.io/download/direct/")) { @@ -112,7 +124,13 @@ namespace EftPatchHelper.Tasks } else { - _options.MirrorList.Add(pair.Key.HubEntryText, pair.Key.GetLink()); + DownloadMirror mirror = new DownloadMirror() + { + Link = pair.Key.GetLink(), + Hash = GetFileHash(pair.Key.UploadFileInfo) + }; + + _options.MirrorList.Add(pair.Key.HubEntryText, mirror); } }