diff --git a/EftPatchHelper/EftPatchHelper/EftPatchHelper.csproj b/EftPatchHelper/EftPatchHelper/EftPatchHelper.csproj index c26bc90..b65f54d 100644 --- a/EftPatchHelper/EftPatchHelper/EftPatchHelper.csproj +++ b/EftPatchHelper/EftPatchHelper/EftPatchHelper.csproj @@ -5,8 +5,8 @@ net8.0 enable enable - 1.5.1 - 1.5.1 + 1.5.3 + 1.5.3 @@ -25,6 +25,9 @@ Resources\Gitea.dll + + Resources\WinSCPnet.dll + diff --git a/EftPatchHelper/EftPatchHelper/Interfaces/IFileUpload.cs b/EftPatchHelper/EftPatchHelper/Interfaces/IFileUpload.cs index 3dfcdd1..3484e85 100644 --- a/EftPatchHelper/EftPatchHelper/Interfaces/IFileUpload.cs +++ b/EftPatchHelper/EftPatchHelper/Interfaces/IFileUpload.cs @@ -12,6 +12,7 @@ namespace EftPatchHelper.Interfaces public string ServiceName { get; set; } public string HubEntryText { get; set; } public FileInfo UploadFileInfo { get; } + public bool AddHubEntry { get; } public string GetLink(); public Task UploadAsync(IProgress? progress = null); } diff --git a/EftPatchHelper/EftPatchHelper/Model/DownloadMirror.cs b/EftPatchHelper/EftPatchHelper/Model/DownloadMirror.cs index 9e4efcd..f5cec57 100644 --- a/EftPatchHelper/EftPatchHelper/Model/DownloadMirror.cs +++ b/EftPatchHelper/EftPatchHelper/Model/DownloadMirror.cs @@ -1,7 +1,11 @@ -namespace EftPatchHelper.Model +using System.Text.Json.Serialization; + +namespace EftPatchHelper.Model { public class DownloadMirror { + [JsonIgnore] + public bool AddHubEntry { get; set; } 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 2bb1e65..3ec7941 100644 --- a/EftPatchHelper/EftPatchHelper/Model/GoFileUpload.cs +++ b/EftPatchHelper/EftPatchHelper/Model/GoFileUpload.cs @@ -18,6 +18,7 @@ namespace EftPatchHelper.Model public string DisplayName { get; set; } public string ServiceName { get; set; } public string HubEntryText { get; set; } + public bool AddHubEntry { get; } public GoFileUpload(FileInfo file, string apiToken, string folderId) { @@ -31,6 +32,7 @@ namespace EftPatchHelper.Model ServiceName = "GoFile"; DisplayName = $"{ServiceName} Upload: {UploadFileInfo.Name}"; HubEntryText = $"Download from {ServiceName}"; + AddHubEntry = true; } public string GetLink() diff --git a/EftPatchHelper/EftPatchHelper/Model/MegaUpload.cs b/EftPatchHelper/EftPatchHelper/Model/MegaUpload.cs index 18102c7..87583d2 100644 --- a/EftPatchHelper/EftPatchHelper/Model/MegaUpload.cs +++ b/EftPatchHelper/EftPatchHelper/Model/MegaUpload.cs @@ -17,6 +17,8 @@ namespace EftPatchHelper.Model public string DisplayName { get; set; } public string ServiceName { get; set; } public string HubEntryText { get; set; } + public bool AddHubEntry { get; } + public MegaUpload(FileInfo file, string email, string password, string mfaKey = null) { @@ -27,6 +29,7 @@ namespace EftPatchHelper.Model ServiceName = "Mega"; DisplayName = $"{ServiceName} Upload: {UploadFileInfo.Name}"; HubEntryText = $"Download from {ServiceName}"; + AddHubEntry = true; } private async Task CheckLoginStatus() diff --git a/EftPatchHelper/EftPatchHelper/Model/Options.cs b/EftPatchHelper/EftPatchHelper/Model/Options.cs index bc289f3..8f61a80 100644 --- a/EftPatchHelper/EftPatchHelper/Model/Options.cs +++ b/EftPatchHelper/EftPatchHelper/Model/Options.cs @@ -39,6 +39,11 @@ namespace EftPatchHelper.Model /// public bool UploadToMega = false; + /// + /// Whether or not to upload to all sftp site listing + /// + public bool UploadToSftpSites = false; + /// /// List of mirrors to upload to Gitea /// diff --git a/EftPatchHelper/EftPatchHelper/Model/Settings.cs b/EftPatchHelper/EftPatchHelper/Model/Settings.cs index 9ff6cb6..0c24b4c 100644 --- a/EftPatchHelper/EftPatchHelper/Model/Settings.cs +++ b/EftPatchHelper/EftPatchHelper/Model/Settings.cs @@ -57,6 +57,9 @@ namespace EftPatchHelper.Model [JsonPropertyName("goFileFolderId")] public string GoFileFolderId { get; set; } = ""; + [JsonPropertyName("sftpUploads")] + public List SftpUploads { get; set; } = new(); + public bool Save() { try diff --git a/EftPatchHelper/EftPatchHelper/Model/SftpUpload.cs b/EftPatchHelper/EftPatchHelper/Model/SftpUpload.cs new file mode 100644 index 0000000..e412ab8 --- /dev/null +++ b/EftPatchHelper/EftPatchHelper/Model/SftpUpload.cs @@ -0,0 +1,72 @@ +using EftPatchHelper.Interfaces; +using WinSCP; + +namespace EftPatchHelper.Model; + +public class SftpUpload : IFileUpload +{ + private readonly SftpUploadInfo _sftpInfo; + private readonly SessionOptions _sessionOptions; + public string DisplayName { get; set; } + public string ServiceName { get; set; } + public string HubEntryText { get; set; } + public bool AddHubEntry { get; } + + public FileInfo UploadFileInfo { get; } + + public SftpUpload(FileInfo file, SftpUploadInfo sftpInfo) + { + UploadFileInfo = file; + _sftpInfo = sftpInfo; + + _sessionOptions = new SessionOptions + { + Protocol = Protocol.Sftp, + UserName = _sftpInfo.Username, + Password = _sftpInfo.Password, + HostName = _sftpInfo.Hostname, + PortNumber = _sftpInfo.Port, + SshHostKeyFingerprint = _sftpInfo.HostKey + }; + + ServiceName = _sftpInfo.Hostname; + DisplayName = $"{ServiceName} Upload: {UploadFileInfo.Name}"; + HubEntryText = $"Download from {ServiceName}"; + AddHubEntry = false; + } + + public string GetLink() + { + return $"{_sftpInfo.HttpPath}/${UploadFileInfo.Name}"; + } + + public Task UploadAsync(IProgress? progress = null) + { + TransferOptions transferOptions = new TransferOptions + { + TransferMode = TransferMode.Binary, + }; + + using Session session = new Session(); + + if (progress != null) + { + session.FileTransferProgress += (_, args) => progress.Report(Math.Floor(args.FileProgress * 100)); + } + + try + { + session.Open(_sessionOptions); + + session.PutFiles(UploadFileInfo.FullName, $"{_sftpInfo.UploadPath}/{UploadFileInfo.Name}", false, transferOptions).Check(); + + return Task.FromResult(true); + } + catch + { + // ignored + } + + return Task.FromResult(false); + } +} \ No newline at end of file diff --git a/EftPatchHelper/EftPatchHelper/Model/SftpUploadInfo.cs b/EftPatchHelper/EftPatchHelper/Model/SftpUploadInfo.cs new file mode 100644 index 0000000..b9d8f9c --- /dev/null +++ b/EftPatchHelper/EftPatchHelper/Model/SftpUploadInfo.cs @@ -0,0 +1,53 @@ +using System.Text.Json.Serialization; + +namespace EftPatchHelper.Model; + +public class SftpUploadInfo +{ + [JsonPropertyName("username")] + public string Username { get; set; } = ""; + + [JsonPropertyName("password")] + public string Password { get; set; } = ""; + + [JsonPropertyName("hostKey")] + public string HostKey { get; set; } = ""; + + [JsonPropertyName("hostname")] + public string Hostname { get; set; } = ""; + + [JsonPropertyName("port")] + public int Port { get; set; } = 0; + + [JsonPropertyName("uploadPath")] + public string UploadPath { get; set; } = ""; + + [JsonPropertyName("httpPath")] + public string HttpPath { get; set; } = ""; + + public bool IsValid() + { + if (string.IsNullOrWhiteSpace(Username)) + return false; + + if (string.IsNullOrWhiteSpace(Password)) + return false; + + if (string.IsNullOrWhiteSpace(Hostname)) + return false; + + if (string.IsNullOrWhiteSpace(HostKey)) + return false; + + if (Port == 0) + return false; + + if (string.IsNullOrWhiteSpace(UploadPath)) + return false; + + if (string.IsNullOrWhiteSpace(HttpPath)) + return false; + + return true; + } +} \ No newline at end of file diff --git a/EftPatchHelper/EftPatchHelper/Resources/WinSCPnet.dll b/EftPatchHelper/EftPatchHelper/Resources/WinSCPnet.dll new file mode 100644 index 0000000..fe34dc9 Binary files /dev/null and b/EftPatchHelper/EftPatchHelper/Resources/WinSCPnet.dll differ diff --git a/EftPatchHelper/EftPatchHelper/Tasks/StartupSettingsTask.cs b/EftPatchHelper/EftPatchHelper/Tasks/StartupSettingsTask.cs index ba47508..d863c11 100644 --- a/EftPatchHelper/EftPatchHelper/Tasks/StartupSettingsTask.cs +++ b/EftPatchHelper/EftPatchHelper/Tasks/StartupSettingsTask.cs @@ -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() diff --git a/EftPatchHelper/EftPatchHelper/Tasks/UploadTasks.cs b/EftPatchHelper/EftPatchHelper/Tasks/UploadTasks.cs index 68ae0c5..1685a76 100644 --- a/EftPatchHelper/EftPatchHelper/Tasks/UploadTasks.cs +++ b/EftPatchHelper/EftPatchHelper/Tasks/UploadTasks.cs @@ -34,12 +34,18 @@ namespace EftPatchHelper.Tasks { var patcherFile = new FileInfo(_options.OutputPatchPath + ".zip"); - if (!patcherFile.Exists) return false; + if (!patcherFile.Exists) + { + 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) @@ -47,6 +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"); + } + + if (_settings.SftpUploads.Count > 0 && _options.UploadToSftpSites) + { + foreach (var sftpInfo in _settings.SftpUploads) + { + if (!sftpInfo.IsValid()) + { + continue; + } + + AnsiConsole.WriteLine($"Added SFTP: {sftpInfo.Hostname}"); + _fileUploads.Add(new SftpUpload(patcherFile, sftpInfo)); + } } return true; @@ -58,6 +79,11 @@ namespace EftPatchHelper.Tasks foreach (var pair in _options.MirrorList) { + if (!pair.Value.AddHubEntry) + { + continue; + } + var displayText = pair.Key; var link = pair.Value.Link; @@ -90,8 +116,12 @@ namespace EftPatchHelper.Tasks static string BytesToString(long byteCount) { string[] suf = { "B", "KB", "MB", "GB", "TB", "PB", "EB" }; + if (byteCount == 0) + { return "0" + suf[0]; + } + long bytes = Math.Abs(byteCount); int place = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024))); double num = Math.Round(bytes / Math.Pow(1024, place), 1); @@ -134,8 +164,9 @@ namespace EftPatchHelper.Tasks } else { - DownloadMirror mirror = new DownloadMirror() + DownloadMirror mirror = new DownloadMirror { + AddHubEntry = pair.Key.AddHubEntry, Link = pair.Key.GetLink(), Hash = GetFileHash(pair.Key.UploadFileInfo) }; @@ -152,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(); diff --git a/EftPatchHelper/EftPatchHelper/settings.json b/EftPatchHelper/EftPatchHelper/settings.json index ab63447..949f118 100644 --- a/EftPatchHelper/EftPatchHelper/settings.json +++ b/EftPatchHelper/EftPatchHelper/settings.json @@ -14,5 +14,18 @@ "megaPassword": "", "megaUploadFolder": "", "goFileApiKey": "", - "goFileFolderId": "" -} \ No newline at end of file + "goFileFolderId": "", + "sftpUploads": [ + { + "username": "example-remove-before-using", + "password": "password123", + "hostKey": "ssh-ed12345 SLKDJFK3928D2LDKFJ2", + "hostname": "sftp.slugma-ligma.com", + "port": 12345, + "uploadPath": "/public/patchers", + "httpPath": "https://mirror.slugma-ligma.com/patchers" + } + ] +} + +