using GameBackup3H3.DbOp;
using ryCommon;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WinSCP;
namespace FTPop
{
///
/// FTP管理
///
public class FTPWinSCP : IFTP, IDisposable
{
private bool disposed = false;
readonly Session session = new Session();
public string CurRemoteFolder { get; private set; } = "";
private FTPInfo _ftpinfo = null;
///
/// 打开FTP
///
///
public int Open(FTPInfo ftpinfo)
{
if(ftpinfo==null)
{
return -1;
}
_ftpinfo = ftpinfo;
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Ftp,
HostName = ftpinfo.IP,
PortNumber = ftpinfo.Port,
UserName = ftpinfo.UserName,
Password = ftpinfo.Pwd,
};
CurRemoteFolder = ftpinfo.RemoteDir;
if (ftpinfo.Encrypt > 0)
{
sessionOptions.FtpSecure = ftpinfo.Encrypt == 2 ? FtpSecure.Explicit : FtpSecure.Implicit;
sessionOptions.GiveUpSecurityAndAcceptAnyTlsHostCertificate = true;
}
else
{
sessionOptions.FtpSecure = FtpSecure.None;
}
if (FileTransferProgress != null)
{
session.FileTransferProgress += Session_FileTransferProgress; ;
}
try
{
session.Open(sessionOptions);
if (session.Opened)
{
isopen = true;
return 1;
}
else
{
isopen = false;
return -1;
}
}
catch(Exception ex)
{
isopen = false;
ErrorMsg = ex.Message;
return -2;
}
}
private bool isopen = false;
///
/// ftp是否打开着
///
public bool IsOpen
{
get {
try {
return session.Opened && isopen;
}
catch { return false; }
}
}
///
/// ftp是否打开着
///
public int ReConnect
{
get { return Open(_ftpinfo); }
}
private void Session_FileTransferProgress(object sender, WinSCP.FileTransferProgressEventArgs e)
{
var progress= new FileTransferProgressEventArgs()
{
Cancel = e.Cancel,
LocalPath = e.FileName,
FileProgress=e.FileProgress,
IsComplete=e.FileProgress==1
};
FileTransferProgress?.Invoke(this, progress);
if (progress.Cancel) { e.Cancel = progress.Cancel; }
}
///
/// 关闭FTP
///
///
public int Close()
{
try
{
if (session.Opened)
{ session.Close(); }
session?.Dispose();
return 1;
}
catch { return 0; }
}
///
/// 移除文件
///
///
///
public int RemoveFiles(string remotePath)
{
var result= session.RemoveFiles(remotePath);
return result.IsSuccess?1:-1;
}
///
/// 移动文件
///
///
///
///
public int MoveFile(string fromPath, string toPath)
{
session.MoveFile(fromPath, toPath);
return 1;
}
///
/// 删除文件
///
///
///
public int DelFile(string remotePath) {
try {
session.RemoveFiles(remotePath);
return 1;
}
catch { return -1; }
}
///
/// 移动文件
///
///
///
public int CreateDir(string DirPath)
{
try
{
session.CreateDirectory(DirPath);
return 1;
}
catch
{
return -1;
}
}
///
/// 文件是否存在
///
///
///
public bool FileExists(string remotePath)
{
if(remotePath.Length==0 || remotePath == "/") { return true; }
try
{
return session.FileExists(remotePath);
}
catch { return false; }
}
///
/// 文件大小
///
///
///
public Int64 FileSize(string remotePath)
{
try
{
return session.GetFileInfo(remotePath).Length;
}
catch { return -1; }
}
///
/// 文件最后修改时间
///
///
///
public DateTime FileLastWriteTime(string remotePath)
{
try
{
return session.GetFileInfo(remotePath).LastWriteTime;
}
catch { return new DateTime(2000,1,1); }
}
///
/// 文件信息
///
///
///
public RemoteFileInfo GetFileInfo(string remotePath)
{
try
{
var info = session.GetFileInfo(remotePath);
return new RemoteFileInfo() { LastWriteTime = info.LastWriteTime, Length = info.Length, Name = info.Name, IsDirectory = info.IsDirectory };
}
catch { return new RemoteFileInfo(); }
}
///
/// 显示文件夹列表
///
///
///
public List ListDirectory(string CurFolder, out string error)
{
error = "";
var list = new List();
try
{
var result = session.ListDirectory(CurFolder);
for (int i = 0; i < result.Files.Count; i++)
{
var file = result.Files[i];
list.Add(new RemoteFileInfo()
{
FullName = file.FullName,
IsDirectory = file.IsDirectory,
Name = file.Name,
Length = file.Length,
LastWriteTime = file.LastWriteTime
});
}
}
catch(Exception ex)
{
isopen = false;
error = ex.Message;
}
return list;
}
///
/// 上传
///
///
///
///
public int Upload(string localPath, string remotePath)
{
try
{
TransferOptions transferOptions = new TransferOptions
{
TransferMode = TransferMode.Binary,
OverwriteMode= OverwriteMode.Overwrite,
};
TransferOperationResult transferResult;
transferResult =
session.PutFiles(localPath, remotePath, false, transferOptions);
//Throw on any error
transferResult.Check();
if (transferResult.IsSuccess)
{
if (System.IO.Directory.Exists(localPath))
{ return 1; }
else
{
if (FileSize(remotePath) == RyFiles.GetFileSize(localPath))
{
return 1;
}
else
{
ErrorMsg = "文件不完整。";
return -3;
}
}
}
else
{
return -2;
}
}
catch(Exception ex)
{
ErrorMsg = ex.Message;
return -1;
}
}
///
/// 下载
///
///
///
///
public int Download(string remotePath, string localPath)
{
if(!RyFiles.FileOrDirExist(System.IO.Path.GetDirectoryName(localPath)))
{
RyFiles.CreateDirectory(System.IO.Path.GetDirectoryName(localPath));
}
try
{
TransferOptions transferOptions = new TransferOptions
{
TransferMode = TransferMode.Binary,
OverwriteMode = OverwriteMode.Overwrite
};
TransferOperationResult transferResult;
transferResult =
session.GetFiles(remotePath, localPath, false, transferOptions);
//Throw on any error
transferResult.Check();
if (transferResult.IsSuccess)
{
if (FileSize(remotePath) == RyFiles.GetFileSize(localPath))
{
return 1;
}
else
{
ErrorMsg = "文件不完整。";
return -3;
}
}
else
{
return -2;
}
}
catch (Exception ex)
{
ErrorMsg = ex.Message;
return -1;
}
}
public string ErrorMsg { get; set; } = "";
///
/// 文件传输事件
///
public event FileTransferProgressEventHandler FileTransferProgress;
~FTPWinSCP()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
Close();
// If disposing equals true, dispose all managed
// and unmanaged resources.
if (disposing)
{
// Dispose managed resources.
session.Dispose();
}
// Release unmanaged resources. If disposing is false,
// only the following code is executed.
// Note that this is not thread safe.
// Another thread could start disposing the object
// after the managed resources are disposed,
// but before the disposed flag is set to true.
// If thread safety is necessary, it must be
// implemented by the client.
}
disposed = true;
}
}
}