add upload tasks, fix options not setting target client correctly

This commit is contained in:
IsWaffle 2022-07-04 12:26:54 -04:00
parent a879f686ff
commit 4141385ea7
13 changed files with 371 additions and 5 deletions

View File

@ -12,6 +12,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="FubarCoder.RestSharp.Portable.Core" Version="4.0.8" /> <PackageReference Include="FubarCoder.RestSharp.Portable.Core" Version="4.0.8" />
<PackageReference Include="FubarCoder.RestSharp.Portable.HttpClient" Version="4.0.8" /> <PackageReference Include="FubarCoder.RestSharp.Portable.HttpClient" Version="4.0.8" />
<PackageReference Include="MegaApiClient" Version="1.10.2" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" /> <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" /> <PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="6.0.0" /> <PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="6.0.0" />
@ -23,6 +24,9 @@
<Reference Include="Gitea"> <Reference Include="Gitea">
<HintPath>Resources\Gitea.dll</HintPath> <HintPath>Resources\Gitea.dll</HintPath>
</Reference> </Reference>
<Reference Include="GoFileSharp">
<HintPath>Resources\GoFileSharp.dll</HintPath>
</Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -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<bool> UploadAsync(IProgress<double>? progress = null);
}
}

View File

@ -0,0 +1,6 @@
namespace EftPatchHelper.Interfaces
{
public interface IUploadTasks : ITaskable
{
}
}

View File

@ -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<bool> UploadAsync(IProgress<double>? progress = null)
{
var uploadedFile = await GoFile.UploadFileAsync(_file, progress);
if(uploadedFile == null) return false;
_uploadedFile = uploadedFile;
return true;
}
}
}

View File

@ -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<bool> 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<bool> 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<bool> UploadAsync(IProgress<double>? 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();
}
}
}

View File

@ -28,5 +28,15 @@ namespace EftPatchHelper.Model
/// Whether or not the user opted to create a release on gitea /// Whether or not the user opted to create a release on gitea
/// </summary> /// </summary>
public bool CreateRelease = false; public bool CreateRelease = false;
/// <summary>
/// Whether or not to upload the patcher to gofile.io
/// </summary>
public bool UploadToGoFile = false;
/// <summary>
/// Whether or not to upload the pather to mega.io
/// </summary>
public bool UploadToMega = false;
} }
} }

View File

@ -42,6 +42,18 @@ namespace EftPatchHelper.Model
[JsonPropertyName("giteaReleaseRepoName")] [JsonPropertyName("giteaReleaseRepoName")]
public string GiteaReleaseRepoName { get; set; } = ""; 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() public bool Save()
{ {
try try
@ -78,6 +90,22 @@ namespace EftPatchHelper.Model
return true; 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() public bool Validate()
{ {
if (string.IsNullOrWhiteSpace(TargetEftVersion)) return false; if (string.IsNullOrWhiteSpace(TargetEftVersion)) return false;

View File

@ -18,6 +18,7 @@ namespace EftPatchHelper
ITaskable _patchGenTasks; ITaskable _patchGenTasks;
ITaskable _patchTestingTasks; ITaskable _patchTestingTasks;
ITaskable _createReleaseTasks; ITaskable _createReleaseTasks;
ITaskable _uploadTasks;
public static void Main(string[] args) public static void Main(string[] args)
{ {
@ -38,7 +39,8 @@ namespace EftPatchHelper
IFileProcessingTasks fileProcessingTasks, IFileProcessingTasks fileProcessingTasks,
IPatchGenTasks patchGenTasks, IPatchGenTasks patchGenTasks,
IPatchTestingTasks patchTestingTasks, IPatchTestingTasks patchTestingTasks,
IReleaseCreator createReleaseTasks IReleaseCreator createReleaseTasks,
IUploadTasks uploadTasks
) )
{ {
_settingsTasks = settingsTasks; _settingsTasks = settingsTasks;
@ -47,6 +49,7 @@ namespace EftPatchHelper
_patchGenTasks = patchGenTasks; _patchGenTasks = patchGenTasks;
_patchTestingTasks = patchTestingTasks; _patchTestingTasks = patchTestingTasks;
_createReleaseTasks = createReleaseTasks; _createReleaseTasks = createReleaseTasks;
_uploadTasks = uploadTasks;
} }
public void Run() public void Run()
@ -57,6 +60,7 @@ namespace EftPatchHelper
_patchGenTasks.Run(); _patchGenTasks.Run();
_patchTestingTasks.Run(); _patchTestingTasks.Run();
_createReleaseTasks.Run(); _createReleaseTasks.Run();
_uploadTasks.Run();
} }
private static IHost ConfigureHost(string[] args) private static IHost ConfigureHost(string[] args)
@ -80,6 +84,7 @@ namespace EftPatchHelper
services.AddTransient<IPatchGenTasks, PatchGenTasks>(); services.AddTransient<IPatchGenTasks, PatchGenTasks>();
services.AddTransient<IPatchTestingTasks, PatchTestingTasks>(); services.AddTransient<IPatchTestingTasks, PatchTestingTasks>();
services.AddTransient<IReleaseCreator, CreateReleaseTasks>(); services.AddTransient<IReleaseCreator, CreateReleaseTasks>();
services.AddTransient<IUploadTasks, UploadTasks>();
services.AddTransient<Program>(); services.AddTransient<Program>();
}) })
.ConfigureAppConfiguration((_, config) => .ConfigureAppConfiguration((_, config) =>

View File

@ -21,14 +21,14 @@ namespace EftPatchHelper.Tasks
private bool ChangeSettingsTargetVersion() private bool ChangeSettingsTargetVersion()
{ {
var targetClient = _clientSelector.GetClientSelection("Select [yellow]Target[/] Version"); _options.TargetClient = _clientSelector.GetClientSelection("Select [yellow]Target[/] Version");
AnsiConsole.WriteLine(); 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)) if (changeVersion.Show(AnsiConsole.Console))
{ {
_settings.TargetEftVersion = targetClient.Version; _settings.TargetEftVersion = _options.TargetClient.Version;
return _settings.Save(); return _settings.Save();
} }

View File

@ -65,6 +65,22 @@ namespace EftPatchHelper.Tasks
if (!_settings.UsingGitea()) return; if (!_settings.UsingGitea()) return;
_options.CreateRelease = new ConfirmationPrompt("Create a release on gitea?").Show(AnsiConsole.Console); _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() public void Run()

View File

@ -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<IFileUpload> _fileUploads = new List<IFileUpload>();
private Dictionary<IFileUpload, ProgressTask> uploadTasks = new Dictionary<IFileUpload, ProgressTask>();
public UploadTasks(Options options, Settings settings)
{
_options = options;
_settings = settings;
}
private async Task<bool> 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 = $"<p>Downgrade EFT Client files from version {_options.SourceClient.Version} to {_options.TargetClient.Version}</p>\n<p><br></p>";
if(_options.UploadToGoFile)
{
output += $"\n<p><a href=\"{goFileLink}\">Download From GoFile</a></p>";
}
if(_options.UploadToMega)
{
output += $"\n<p><a href=\"{megaLink}\">Download From Mega</a></p>";
}
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<bool> 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<bool>(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<double>((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();
}
}
}

View File

@ -9,5 +9,9 @@
"giteaApiBasePath": "", //You can leave the gitea settings blank if you don't need to create releases on gitea "giteaApiBasePath": "", //You can leave the gitea settings blank if you don't need to create releases on gitea
"giteaApiKey": "", "giteaApiKey": "",
"giteaReleaseRepoOwner": "", "giteaReleaseRepoOwner": "",
"giteaReleaseRepoName": "" "giteaReleaseRepoName": "",
"megaEmail": "",
"megaPassword": "",
"megaUploadFolder": "",
"goFileApiKey": ""
} }