From bf07192350aa2e052f7df28ae43c58a8b14b2034 Mon Sep 17 00:00:00 2001 From: xiaochanghai Date: Mon, 6 May 2024 19:45:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9EFluentFTP=20=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Tiobon.Core.PublishHelper/Src/SftpHelper.cs | 85 ++++++++++++++------- 1 file changed, 59 insertions(+), 26 deletions(-) diff --git a/Tiobon.Core.PublishHelper/Src/SftpHelper.cs b/Tiobon.Core.PublishHelper/Src/SftpHelper.cs index b9d97a2a..ef6ec315 100644 --- a/Tiobon.Core.PublishHelper/Src/SftpHelper.cs +++ b/Tiobon.Core.PublishHelper/Src/SftpHelper.cs @@ -1,8 +1,9 @@ using System.Net; using System.Text; using FluentFTP; +using File = System.IO.File; using Renci.SshNet; -using Renci.SshNet.Sftp; +using Renci.SshNet.Sftp; namespace Tiobon.PublishHelper { @@ -26,39 +27,71 @@ namespace Tiobon.PublishHelper try { if (!Utility.Ping(server.Ip, b_log)) - { return b_suc; - } - using (SftpClient sftp = new SftpClient(server.Ip, server.Port, "root", server.SuPassword)) + + using (var ftp = new FtpClient(server.Ip, server.UserName, server.SuPassword, server.Port)) { - sftp.Connect(); - if (sftp.Exists(remoteFile)) + ftp.Connect(); + if (b_log) + Utility.SendLog("下载", $"下载文件 {remoteFile}"); + if (File.Exists(localFile)) { - if (b_log) - Utility.SendLog("下载", $"下载文件 {remoteFile}"); - if (File.Exists(localFile)) - { - File.Delete(localFile); - System.Threading.Thread.Sleep(50); - } - var sftFile = sftp.ListDirectory(remoteFile.Substring(0, remoteFile.LastIndexOf('/'))).Where(o => o.FullName == remoteFile).FirstOrDefault(); - using (var file = File.OpenWrite(localFile)) + File.Delete(localFile); + Thread.Sleep(50); + } + var size = ftp.GetFileSize(remoteFile); + // define the progress tracking callback + Action progress1 = delegate (FtpProgress p) + { + if (p.Progress == 1) { - sftp.DownloadFile(remoteFile, file, pro => - { - progress?.Invoke(sftFile.Length, pro); - }); if (b_log) - Utility.SendLog("下载", $"成功 {remoteFile} =>> {localFile} len {file.Length} byte"); + Utility.SendLog("下载", $"成功 {remoteFile} =>> {localFile} len {size} byte"); b_suc = true; } - } - else - { - if (b_log) - Utility.SendLog("下载", $"文件不存在 {remoteFile}"); - } + else + //progress?.Invoke(sftFile.Length, pro); + { + // percent done = (p.Progress * 100) + progress?.Invoke(size, (ulong)p.Progress); + } + }; + + // download a file with progress tracking + ftp.DownloadFile(localFile, remoteFile, FtpLocalExists.Overwrite, FtpVerify.None, progress1); + } + + //using (SftpClient sftp = new SftpClient(server.Ip, server.Port, "root", server.SuPassword)) + //{ + // sftp.Connect(); + // if (sftp.Exists(remoteFile)) + // { + // if (b_log) + // Utility.SendLog("下载", $"下载文件 {remoteFile}"); + // if (File.Exists(localFile)) + // { + // File.Delete(localFile); + // System.Threading.Thread.Sleep(50); + // } + // var sftFile = sftp.ListDirectory(remoteFile.Substring(0, remoteFile.LastIndexOf('/'))).Where(o => o.FullName == remoteFile).FirstOrDefault(); + // using (var file = File.OpenWrite(localFile)) + // { + // sftp.DownloadFile(remoteFile, file, pro => + // { + // progress?.Invoke(sftFile.Length, pro); + // }); + // if (b_log) + // Utility.SendLog("下载", $"成功 {remoteFile} =>> {localFile} len {file.Length} byte"); + // b_suc = true; + // } + // } + // else + // { + // if (b_log) + // Utility.SendLog("下载", $"文件不存在 {remoteFile}"); + // } + //} } catch (Exception ex) {