fix validation and upload, finish stuff

This commit is contained in:
IsWaffle 2024-03-21 20:48:49 -04:00
parent 4a10964f7f
commit 21d96d113a
8 changed files with 42 additions and 21 deletions

View File

@ -5,8 +5,8 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AssemblyVersion>1.5.1</AssemblyVersion>
<FileVersion>1.5.1</FileVersion>
<AssemblyVersion>1.5.3</AssemblyVersion>
<FileVersion>1.5.3</FileVersion>
</PropertyGroup>
<ItemGroup>

View File

@ -39,6 +39,11 @@ namespace EftPatchHelper.Model
/// </summary>
public bool UploadToMega = false;
/// <summary>
/// Whether or not to upload to all sftp site listing
/// </summary>
public bool UploadToSftpSites = false;
/// <summary>
/// List of mirrors to upload to Gitea
/// </summary>

View File

@ -57,7 +57,7 @@ namespace EftPatchHelper.Model
[JsonPropertyName("goFileFolderId")]
public string GoFileFolderId { get; set; } = "";
[JsonPropertyName("sftpUploads")]
[JsonPropertyName("sftpUploads")]
public List<SftpUploadInfo> SftpUploads { get; set; } = new();
public bool Save()

View File

@ -22,6 +22,8 @@ public class SftpUpload : IFileUpload
_sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
UserName = _sftpInfo.Username,
Password = _sftpInfo.Password,
HostName = _sftpInfo.Hostname,
PortNumber = _sftpInfo.Port,
SshHostKeyFingerprint = _sftpInfo.HostKey
@ -40,25 +42,23 @@ public class SftpUpload : IFileUpload
public Task<bool> UploadAsync(IProgress<double>? progress = null)
{
TransferOptions transferOptions = new TransferOptions()
TransferOptions transferOptions = new TransferOptions
{
TransferMode = TransferMode.Binary,
};
using Session session = new Session();
using var uploadStream = UploadFileInfo.OpenRead();
if (progress != null)
{
session.FileTransferProgress += (_, args) => progress.Report(args.FileProgress);
session.FileTransferProgress += (_, args) => progress.Report(Math.Floor(args.FileProgress * 100));
}
try
{
session.Open(_sessionOptions);
session.PutFile(uploadStream, _sftpInfo.UploadPath, transferOptions);
session.PutFiles(UploadFileInfo.FullName, $"{_sftpInfo.UploadPath}/{UploadFileInfo.Name}", false, transferOptions).Check();
return Task.FromResult(true);
}

View File

@ -25,24 +25,27 @@ public class SftpUploadInfo
[JsonPropertyName("httpPath")]
public string HttpPath { get; set; } = "";
public bool Validate()
public bool IsValid()
{
if (!string.IsNullOrWhiteSpace(Username))
if (string.IsNullOrWhiteSpace(Username))
return false;
if (!string.IsNullOrWhiteSpace(Password))
if (string.IsNullOrWhiteSpace(Password))
return false;
if (string.IsNullOrWhiteSpace(Hostname))
return false;
if (!string.IsNullOrWhiteSpace(HostKey))
if (string.IsNullOrWhiteSpace(HostKey))
return false;
if (Port == 0)
return false;
if (!string.IsNullOrWhiteSpace(UploadPath))
if (string.IsNullOrWhiteSpace(UploadPath))
return false;
if (!string.IsNullOrWhiteSpace(HttpPath))
if (string.IsNullOrWhiteSpace(HttpPath))
return false;
return true;

View File

@ -80,6 +80,12 @@ namespace EftPatchHelper.Tasks
{
_options.UploadToGoFile = new ConfirmationPrompt("Upload to GoFile?").Show(AnsiConsole.Console);
}
if (_settings.SftpUploads.Count > 0)
{
_options.UploadToSftpSites =
new ConfirmationPrompt($"Upload to SFTP sites? ( {_settings.SftpUploads.Count} sites )").Show(AnsiConsole.Console);
}
}
public void Run()

View File

@ -38,11 +38,14 @@ namespace EftPatchHelper.Tasks
{
return false;
}
AnsiConsole.WriteLine("Building mirrors list ...");
if(_settings.UsingGoFile() && _options.UploadToGoFile)
{
var gofile = new GoFileUpload(patcherFile, _settings.GoFileApiKey, _settings.GoFileFolderId);
_fileUploads.Add(gofile);
AnsiConsole.WriteLine("Added MEGA");
}
if (_settings.UsingMega() && _options.UploadToMega)
@ -50,16 +53,21 @@ namespace EftPatchHelper.Tasks
var mega = new MegaUpload(patcherFile, _settings.MegaEmail, _settings.MegaPassword);
await mega.SetUploadFolder(_settings.MegaUploadFolder);
_fileUploads.Add(mega);
AnsiConsole.WriteLine("Added MEGA");
}
foreach (var sftpInfo in _settings.SftpUploads)
if (_settings.SftpUploads.Count > 0 && _options.UploadToSftpSites)
{
if (!sftpInfo.Validate())
foreach (var sftpInfo in _settings.SftpUploads)
{
continue;
if (!sftpInfo.IsValid())
{
continue;
}
AnsiConsole.WriteLine($"Added SFTP: {sftpInfo.Hostname}");
_fileUploads.Add(new SftpUpload(patcherFile, sftpInfo));
}
_fileUploads.Add(new SftpUpload(patcherFile, sftpInfo));
}
return true;
@ -76,7 +84,6 @@ namespace EftPatchHelper.Tasks
continue;
}
var displayText = pair.Key;
var link = pair.Value.Link;
@ -176,7 +183,7 @@ namespace EftPatchHelper.Tasks
public void Run()
{
if (!_options.UploadToGoFile && !_options.UploadToMega) return;
if (!_options.UploadToGoFile && !_options.UploadToMega && !_options.UploadToSftpSites) return;
UploadAllFiles().GetAwaiter().GetResult().ValidateOrExit();