Imports System.Text
Imports FluentFTP

Namespace UTSModule
    Public Class FtpService
        Private _ftpUser As String
        Private _ftpPwd As String
        Private _ftpPort As Integer
        Private _ftpHost As String

        Sub New(host As String, port As Integer, user As String, pwd As String)
            _ftpHost = host
            _ftpPort = port
            _ftpUser = user
            _ftpPwd = pwd

        End Sub

        ''' <summary>
        ''' Ftp服务器地址
        ''' </summary>
        ''' <returns></returns>
        Public Property FtpHost As String
            Get
                Return _ftpHost
            End Get
            Set(value As String)
                _ftpHost = value
            End Set
        End Property


        Private Sub OnValidateCertificate(control As FtpClient, e As FtpSslValidationEventArgs)
            e.Accept = True
        End Sub

        ''' <summary>
        ''' Ftp是否能正常连接，连接正常返回True,连接失败则产生异常
        ''' </summary>
        ''' <returns></returns>
        Public Function CanConnected() As Boolean
            Dim result As Boolean
            Using ftpClient As FtpClient = New FtpClient(_ftpHost, _ftpPort, _ftpUser, _ftpPwd)
                AddHandler ftpClient.ValidateCertificate, AddressOf OnValidateCertificate
                ftpClient.EncryptionMode = FtpEncryptionMode.None
                ftpClient.DataConnectionType = FtpDataConnectionType.PASV
                ftpClient.Encoding = Encoding.UTF8

                ftpClient.Connect()
                ftpClient.Disconnect()
            End Using
            result = True

            Return result
        End Function

        ''' <summary>
        ''' 文件是否存在
        ''' </summary>
        ''' <param name="path"></param>
        ''' <returns></returns>
        Public Function FtpFileExists(path As String) As Boolean
            Dim result As Boolean
            Using ftpClient As FtpClient = New FtpClient(_ftpHost, _ftpPort, _ftpUser, _ftpPwd)
                AddHandler ftpClient.ValidateCertificate, AddressOf OnValidateCertificate
                ftpClient.EncryptionMode = FtpEncryptionMode.None
                ftpClient.DataConnectionType = FtpDataConnectionType.PASV
                ftpClient.Encoding = Encoding.UTF8

                ftpClient.Connect()

                result = ftpClient.FileExists(path)

                ftpClient.Disconnect()
            End Using

            Return result
        End Function


        ''' <summary>
        ''' 文件上传
        ''' 将本地指定路径压缩包上传到FTP服务器上manager文件夹下
        ''' </summary>
        Public Sub FtpUpload(remotePath As String, loadPath As String)
            Using ftpClient As FtpClient = New FtpClient(_ftpHost, _ftpPort, _ftpUser, _ftpPwd)
                AddHandler ftpClient.ValidateCertificate, AddressOf OnValidateCertificate
                ftpClient.EncryptionMode = FtpEncryptionMode.None
                ftpClient.DataConnectionType = FtpDataConnectionType.PASV
                ftpClient.Encoding = Encoding.UTF8

                ftpClient.Connect()
                ftpClient.UploadFile(loadPath, remotePath, FtpRemoteExists.Overwrite, True)
                ftpClient.Disconnect()
            End Using
        End Sub


        ''' <summary>
        ''' 文件下载
        ''' 从FTP下载压缩包，到本地指定路径
        ''' </summary>
        Public Sub FtpDownload(remotePath As String, loadPath As String)
            Using ftpClient As FtpClient = New FtpClient(_ftpHost, _ftpPort, _ftpUser, _ftpPwd)
                AddHandler ftpClient.ValidateCertificate, AddressOf OnValidateCertificate
                ftpClient.EncryptionMode = FtpEncryptionMode.None
                ftpClient.DataConnectionType = FtpDataConnectionType.PASV
                ftpClient.Encoding = Encoding.UTF8

                ftpClient.Connect()
                ftpClient.DownloadFile(loadPath, remotePath)
                ftpClient.Disconnect()
            End Using
        End Sub
    End Class
End Namespace