|
|
@ -1,8 +1,9 @@ |
|
|
|
using System.Net; |
|
|
|
using System.Net; |
|
|
|
using System.Text; |
|
|
|
using System.Text; |
|
|
|
using FluentFTP; |
|
|
|
using FluentFTP; |
|
|
|
|
|
|
|
using File = System.IO.File; |
|
|
|
using Renci.SshNet; |
|
|
|
using Renci.SshNet; |
|
|
|
using Renci.SshNet.Sftp; |
|
|
|
using Renci.SshNet.Sftp; |
|
|
|
|
|
|
|
|
|
|
|
namespace Tiobon.PublishHelper |
|
|
|
namespace Tiobon.PublishHelper |
|
|
|
{ |
|
|
|
{ |
|
|
@ -26,39 +27,71 @@ namespace Tiobon.PublishHelper |
|
|
|
try |
|
|
|
try |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (!Utility.Ping(server.Ip, b_log)) |
|
|
|
if (!Utility.Ping(server.Ip, b_log)) |
|
|
|
{ |
|
|
|
|
|
|
|
return b_suc; |
|
|
|
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(); |
|
|
|
ftp.Connect(); |
|
|
|
if (sftp.Exists(remoteFile)) |
|
|
|
if (b_log) |
|
|
|
|
|
|
|
Utility.SendLog("下载", $"下载文件 {remoteFile}"); |
|
|
|
|
|
|
|
if (File.Exists(localFile)) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (b_log) |
|
|
|
File.Delete(localFile); |
|
|
|
Utility.SendLog("下载", $"下载文件 {remoteFile}"); |
|
|
|
Thread.Sleep(50); |
|
|
|
if (File.Exists(localFile)) |
|
|
|
} |
|
|
|
{ |
|
|
|
var size = ftp.GetFileSize(remoteFile); |
|
|
|
File.Delete(localFile); |
|
|
|
// define the progress tracking callback |
|
|
|
System.Threading.Thread.Sleep(50); |
|
|
|
Action<FtpProgress> progress1 = delegate (FtpProgress p) |
|
|
|
} |
|
|
|
{ |
|
|
|
var sftFile = sftp.ListDirectory(remoteFile.Substring(0, remoteFile.LastIndexOf('/'))).Where(o => o.FullName == remoteFile).FirstOrDefault(); |
|
|
|
if (p.Progress == 1) |
|
|
|
using (var file = File.OpenWrite(localFile)) |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
sftp.DownloadFile(remoteFile, file, pro => |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
progress?.Invoke(sftFile.Length, pro); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
if (b_log) |
|
|
|
if (b_log) |
|
|
|
Utility.SendLog("下载", $"成功 {remoteFile} =>> {localFile} len {file.Length} byte"); |
|
|
|
Utility.SendLog("下载", $"成功 {remoteFile} =>> {localFile} len {size} byte"); |
|
|
|
b_suc = true; |
|
|
|
b_suc = true; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
else |
|
|
|
//progress?.Invoke(sftFile.Length, pro); |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (b_log) |
|
|
|
// percent done = (p.Progress * 100) |
|
|
|
Utility.SendLog("下载", $"文件不存在 {remoteFile}"); |
|
|
|
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) |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
{ |
|
|
|