diff --git a/EftPatchHelper/EftPatchHelper/EftPatchHelper.csproj b/EftPatchHelper/EftPatchHelper/EftPatchHelper.csproj index 9cfad73..d31d5f2 100644 --- a/EftPatchHelper/EftPatchHelper/EftPatchHelper.csproj +++ b/EftPatchHelper/EftPatchHelper/EftPatchHelper.csproj @@ -12,6 +12,7 @@ + @@ -23,6 +24,9 @@ Resources\Gitea.dll + + Resources\GoFileSharp.dll + diff --git a/EftPatchHelper/EftPatchHelper/Interfaces/IFileUpload.cs b/EftPatchHelper/EftPatchHelper/Interfaces/IFileUpload.cs new file mode 100644 index 0000000..0910f73 --- /dev/null +++ b/EftPatchHelper/EftPatchHelper/Interfaces/IFileUpload.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace EftPatchHelper.Interfaces +{ + public interface IFileUpload + { + public string DisplayName { get; set; } + public string GetLink(); + public Task UploadAsync(IProgress? progress = null); + } +} diff --git a/EftPatchHelper/EftPatchHelper/Interfaces/IUploadTasks.cs b/EftPatchHelper/EftPatchHelper/Interfaces/IUploadTasks.cs new file mode 100644 index 0000000..e7ecc18 --- /dev/null +++ b/EftPatchHelper/EftPatchHelper/Interfaces/IUploadTasks.cs @@ -0,0 +1,6 @@ +namespace EftPatchHelper.Interfaces +{ + public interface IUploadTasks : ITaskable + { + } +} diff --git a/EftPatchHelper/EftPatchHelper/Model/GoFileUpload.cs b/EftPatchHelper/EftPatchHelper/Model/GoFileUpload.cs new file mode 100644 index 0000000..773d908 --- /dev/null +++ b/EftPatchHelper/EftPatchHelper/Model/GoFileUpload.cs @@ -0,0 +1,37 @@ +using EftPatchHelper.Interfaces; +using GoFileSharp; +using GoFileSharp.Model.GoFileData.Wrappers; + +namespace EftPatchHelper.Model +{ + public class GoFileUpload : IFileUpload + { + private FileInfo _file; + private GoFileFile _uploadedFile; + + public string DisplayName { get; set; } + + public GoFileUpload(FileInfo file, string apiToken) + { + GoFile.ApiToken = apiToken; + _file = file; + DisplayName = $"GoFile Upload: {_file.Name}"; + } + + public string GetLink() + { + return _uploadedFile.Link; + } + + public async Task UploadAsync(IProgress? progress = null) + { + var uploadedFile = await GoFile.UploadFileAsync(_file, progress); + + if(uploadedFile == null) return false; + + _uploadedFile = uploadedFile; + + return true; + } + } +} diff --git a/EftPatchHelper/EftPatchHelper/Model/MegaUpload.cs b/EftPatchHelper/EftPatchHelper/Model/MegaUpload.cs new file mode 100644 index 0000000..1e3a51b --- /dev/null +++ b/EftPatchHelper/EftPatchHelper/Model/MegaUpload.cs @@ -0,0 +1,96 @@ +using EftPatchHelper.Interfaces; +using CG.Web.MegaApiClient; +using Spectre.Console; + +namespace EftPatchHelper.Model +{ + public class MegaUpload : IFileUpload, IDisposable + { + private FileInfo _file; + private MegaApiClient _client; + private string _email; + private string _password; + private string _mfaKey; + private INode _uploadFolder; + private INode _uploadedFile; + + public string DisplayName { get; set; } + + public MegaUpload(FileInfo file, string email, string password, string mfaKey = null) + { + _client = new MegaApiClient(); + _file = file; + _email = email; + _password = password; + DisplayName = $"Mega Upload: {_file.Name}"; + } + + private async Task CheckLoginStatus() + { + if (!_client.IsLoggedIn) + { + AnsiConsole.Markup("[blue]Logging into mega ... [/]"); + + await _client.LoginAsync(_email, _password, _mfaKey); + + if (!_client.IsLoggedIn) + { + AnsiConsole.MarkupLine("[red]failed[/]"); + return false; + } + AnsiConsole.MarkupLine("[green]ok[/]"); + } + + return true; + } + + public async Task SetUploadFolder(string folderName) + { + if (!await CheckLoginStatus()) + { + return false; + } + + AnsiConsole.Markup("[blue]Getting node ... [/]"); + var nodes = await _client.GetNodesAsync(); + + var trashNode = nodes.SingleOrDefault(x => x.Type == NodeType.Trash); + + _uploadFolder = nodes.SingleOrDefault(x => x.Name == folderName && x.ParentId != trashNode.Id); + + bool nodeSet = _uploadFolder != null; + + AnsiConsole.MarkupLine(nodeSet != false ? "[green]node set[/]" : "[red]failed to set node[/]"); + + return nodeSet; + } + + public string GetLink() + { + return _client.GetDownloadLink(_uploadedFile).ToString(); + } + + public async Task UploadAsync(IProgress? progress = null) + { + _file.Refresh(); + + if (!_file.Exists) return false; + + if(!await CheckLoginStatus()) + { + return false; + } + + using var fileStream = _file.OpenRead(); + + _uploadedFile = await _client.UploadAsync(fileStream, _file.Name, _uploadFolder, progress); + + return _uploadedFile != null; + } + + public void Dispose() + { + _client.Logout(); + } + } +} diff --git a/EftPatchHelper/EftPatchHelper/Model/Options.cs b/EftPatchHelper/EftPatchHelper/Model/Options.cs index ebdb44f..7d72b50 100644 --- a/EftPatchHelper/EftPatchHelper/Model/Options.cs +++ b/EftPatchHelper/EftPatchHelper/Model/Options.cs @@ -28,5 +28,15 @@ namespace EftPatchHelper.Model /// Whether or not the user opted to create a release on gitea /// public bool CreateRelease = false; + + /// + /// Whether or not to upload the patcher to gofile.io + /// + public bool UploadToGoFile = false; + + /// + /// Whether or not to upload the pather to mega.io + /// + public bool UploadToMega = false; } } diff --git a/EftPatchHelper/EftPatchHelper/Model/Settings.cs b/EftPatchHelper/EftPatchHelper/Model/Settings.cs index cd1400e..437c346 100644 --- a/EftPatchHelper/EftPatchHelper/Model/Settings.cs +++ b/EftPatchHelper/EftPatchHelper/Model/Settings.cs @@ -42,6 +42,18 @@ namespace EftPatchHelper.Model [JsonPropertyName("giteaReleaseRepoName")] public string GiteaReleaseRepoName { get; set; } = ""; + [JsonPropertyName("meagEmail")] + public string MegaEmail { get; set; } = ""; + + [JsonPropertyName("megaPassword")] + public string MegaPassword { get; set; } = ""; + + [JsonPropertyName("megaUploadFolder")] + public string MegaUploadFolder { get; set; } = ""; + + [JsonPropertyName("goFileApiKey")] + public string GoFileApiKey { get; set; } = ""; + public bool Save() { try @@ -78,6 +90,22 @@ namespace EftPatchHelper.Model return true; } + public bool UsingMega() + { + if (string.IsNullOrWhiteSpace(MegaEmail)) return false; + + if (string.IsNullOrWhiteSpace(MegaPassword)) return false; + + return true; + } + + public bool UsingGoFile() + { + if (string.IsNullOrWhiteSpace(GoFileApiKey)) return false; + + return true; + } + public bool Validate() { if (string.IsNullOrWhiteSpace(TargetEftVersion)) return false; diff --git a/EftPatchHelper/EftPatchHelper/Program.cs b/EftPatchHelper/EftPatchHelper/Program.cs index af0cb4b..bd469ed 100644 --- a/EftPatchHelper/EftPatchHelper/Program.cs +++ b/EftPatchHelper/EftPatchHelper/Program.cs @@ -18,6 +18,7 @@ namespace EftPatchHelper ITaskable _patchGenTasks; ITaskable _patchTestingTasks; ITaskable _createReleaseTasks; + ITaskable _uploadTasks; public static void Main(string[] args) { @@ -38,7 +39,8 @@ namespace EftPatchHelper IFileProcessingTasks fileProcessingTasks, IPatchGenTasks patchGenTasks, IPatchTestingTasks patchTestingTasks, - IReleaseCreator createReleaseTasks + IReleaseCreator createReleaseTasks, + IUploadTasks uploadTasks ) { _settingsTasks = settingsTasks; @@ -47,6 +49,7 @@ namespace EftPatchHelper _patchGenTasks = patchGenTasks; _patchTestingTasks = patchTestingTasks; _createReleaseTasks = createReleaseTasks; + _uploadTasks = uploadTasks; } public void Run() @@ -57,6 +60,7 @@ namespace EftPatchHelper _patchGenTasks.Run(); _patchTestingTasks.Run(); _createReleaseTasks.Run(); + _uploadTasks.Run(); } private static IHost ConfigureHost(string[] args) @@ -80,6 +84,7 @@ namespace EftPatchHelper services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); }) .ConfigureAppConfiguration((_, config) => diff --git a/EftPatchHelper/EftPatchHelper/Resources/GoFileSharp.dll b/EftPatchHelper/EftPatchHelper/Resources/GoFileSharp.dll new file mode 100644 index 0000000..d0501ed Binary files /dev/null and b/EftPatchHelper/EftPatchHelper/Resources/GoFileSharp.dll differ diff --git a/EftPatchHelper/EftPatchHelper/Tasks/ClientSelectionTask.cs b/EftPatchHelper/EftPatchHelper/Tasks/ClientSelectionTask.cs index dc06f2d..9cc7e65 100644 --- a/EftPatchHelper/EftPatchHelper/Tasks/ClientSelectionTask.cs +++ b/EftPatchHelper/EftPatchHelper/Tasks/ClientSelectionTask.cs @@ -21,14 +21,14 @@ namespace EftPatchHelper.Tasks private bool ChangeSettingsTargetVersion() { - var targetClient = _clientSelector.GetClientSelection("Select [yellow]Target[/] Version"); + _options.TargetClient = _clientSelector.GetClientSelection("Select [yellow]Target[/] Version"); AnsiConsole.WriteLine(); - ConfirmationPrompt changeVersion = new ConfirmationPrompt($"Update settings target version to use [purple]{targetClient.Version}[/]?"); + ConfirmationPrompt changeVersion = new ConfirmationPrompt($"Update settings target version to use [purple]{_options.TargetClient.Version}[/]?"); if (changeVersion.Show(AnsiConsole.Console)) { - _settings.TargetEftVersion = targetClient.Version; + _settings.TargetEftVersion = _options.TargetClient.Version; return _settings.Save(); } diff --git a/EftPatchHelper/EftPatchHelper/Tasks/StartupSettingsTask.cs b/EftPatchHelper/EftPatchHelper/Tasks/StartupSettingsTask.cs index 474f211..d78a39a 100644 --- a/EftPatchHelper/EftPatchHelper/Tasks/StartupSettingsTask.cs +++ b/EftPatchHelper/EftPatchHelper/Tasks/StartupSettingsTask.cs @@ -65,6 +65,22 @@ namespace EftPatchHelper.Tasks if (!_settings.UsingGitea()) return; _options.CreateRelease = new ConfirmationPrompt("Create a release on gitea?").Show(AnsiConsole.Console); + + if(_options.CreateRelease) + { + // only allow upload options if the release is not being made + return; + } + + if (_settings.UsingMega()) + { + _options.UploadToMega = new ConfirmationPrompt("Upload to Mega?").Show(AnsiConsole.Console); + } + + if (_settings.UsingGoFile()) + { + _options.UploadToGoFile = new ConfirmationPrompt("Upload to GoFile?").Show(AnsiConsole.Console); + } } public void Run() diff --git a/EftPatchHelper/EftPatchHelper/Tasks/UploadTasks.cs b/EftPatchHelper/EftPatchHelper/Tasks/UploadTasks.cs new file mode 100644 index 0000000..618e4a7 --- /dev/null +++ b/EftPatchHelper/EftPatchHelper/Tasks/UploadTasks.cs @@ -0,0 +1,145 @@ +using EftPatchHelper.Extensions; +using EftPatchHelper.Interfaces; +using EftPatchHelper.Model; +using Spectre.Console; + +namespace EftPatchHelper.Tasks +{ + public class UploadTasks : IUploadTasks + { + private Options _options; + private Settings _settings; + private List _fileUploads = new List(); + private Dictionary uploadTasks = new Dictionary(); + + public UploadTasks(Options options, Settings settings) + { + _options = options; + _settings = settings; + } + + private async Task BuildUploadList() + { + var patcherFile = new FileInfo(_options.OutputPatchPath + ".zip"); + + if (!patcherFile.Exists) return false; + + if (_settings.UsingMega() && _options.UploadToMega) + { + var mega = new MegaUpload(patcherFile, _settings.MegaEmail, _settings.MegaPassword); + await mega.SetUploadFolder(_settings.MegaUploadFolder); + _fileUploads.Add(mega); + } + + if(_settings.UsingGoFile() && _options.UploadToGoFile) + { + var gofile = new GoFileUpload(patcherFile, _settings.GoFileApiKey); + _fileUploads.Add(gofile); + } + + return true; + } + + private void CreateHubEntrySource() + { + var goFile = _fileUploads.SingleOrDefault(x => x.GetType() == typeof(GoFileUpload)); + var mega = _fileUploads.SingleOrDefault(x => x.GetType() == typeof(MegaUpload)); + + if(goFile == null || mega == null) + { + AnsiConsole.WriteLine("Failed to get required info to create hub entry source"); + return; + } + + var goFileLink = goFile.GetLink(); + var megaLink = mega.GetLink(); + + if(goFileLink == null || megaLink == null) + { + AnsiConsole.WriteLine("Failed to get link for uploaded files"); + return; + } + + string output = $"

Downgrade EFT Client files from version {_options.SourceClient.Version} to {_options.TargetClient.Version}

\n


"; + + if(_options.UploadToGoFile) + { + output += $"\n

Download From GoFile

"; + } + + if(_options.UploadToMega) + { + output += $"\n

Download From Mega

"; + } + + AnsiConsole.WriteLine(output); + + var unixTimestamp = (int)DateTime.UtcNow.Subtract(DateTime.UnixEpoch).TotalSeconds; + + string outputPath = $"{Environment.CurrentDirectory}\\hubEntry_{unixTimestamp}.txt"; + File.WriteAllText(outputPath, output); + + if(File.Exists(outputPath)) + { + AnsiConsole.MarkupLine($"[green]Hub Entry Source saved: {outputPath.EscapeMarkup()}[/]"); + } + else + { + AnsiConsole.MarkupLine($"[red]Hub Entry Source saved failed[/]"); + } + } + + private async Task UploadAllFiles() + { + if(!await BuildUploadList()) + { + return false; + } + + var succeeded = await AnsiConsole.Progress().Columns( + new TaskDescriptionColumn() { Alignment = Justify.Left}, + new ProgressBarColumn(), + new PercentageColumn(), + new RemainingTimeColumn(), + new SpinnerColumn(Spinner.Known.Dots2) + ).StartAsync(async context => + { + foreach(var file in _fileUploads) + { + var task = context.AddTask($"[purple][[Pending]][/] {file.DisplayName}"); + task.IsIndeterminate = true; + uploadTasks.Add(file, task); + } + + foreach(var pair in uploadTasks) + { + // set the value of the progress task object + var progress = new System.Progress((d) => pair.Value.Value = d); + + pair.Value.IsIndeterminate = false; + pair.Value.Description = pair.Key.DisplayName; + + if(!await pair.Key.UploadAsync(progress)) + { + AnsiConsole.MarkupLine($"[red]{pair.Key.DisplayName.EscapeMarkup()} failed[/]"); + return false; + } + } + + return true; + }); + + return succeeded; + } + + public void Run() + { + if (!_options.CreateRelease) + { + UploadAllFiles().GetAwaiter().GetResult().ValidateOrExit(); + } + + CreateHubEntrySource(); + } + } +} diff --git a/EftPatchHelper/EftPatchHelper/settings.json b/EftPatchHelper/EftPatchHelper/settings.json index 1631198..5e20433 100644 --- a/EftPatchHelper/EftPatchHelper/settings.json +++ b/EftPatchHelper/EftPatchHelper/settings.json @@ -9,5 +9,9 @@ "giteaApiBasePath": "", //You can leave the gitea settings blank if you don't need to create releases on gitea "giteaApiKey": "", "giteaReleaseRepoOwner": "", - "giteaReleaseRepoName": "" + "giteaReleaseRepoName": "", + "megaEmail": "", + "megaPassword": "", + "megaUploadFolder": "", + "goFileApiKey": "" } \ No newline at end of file