Imports System.Text
Imports System.Threading
Imports FluentFTP

Namespace UTSModule
    Public Class UtsFtp
        ''' <summary>测试器句柄,全局唯一</summary>
        Private Shared _object As UtsFtp

        ''' <summary>初始化测试器线程锁</summary>
        Private Shared ReadOnly InitLock As New Object()

        Private Shared FtpPort As Integer
        Private Shared FtpUser As String
        Private Shared FtpPwd As String

        Private _ftpUser As String
        Private _ftpPwd As String
        Private _ftpPort As Integer
        Private _ftpHost As String

        ''' <summary>
        ''' 初始化FTP连接参数
        ''' </summary>
        ''' <param name="port">端口号</param>
        ''' <param name="user">用户名</param>
        ''' <param name="pwd">用户密码</param>
        Public Shared Sub InitConnectParams(port As Integer, user As String, pwd As String)
            FtpPort = port
            FtpUser = user
            FtpPwd = pwd
        End Sub

        ''' <summary>
        ''' 创建类单例对象
        ''' </summary>
        ''' <returns></returns>
        Public Shared Function CreateObject() As UtsFtp
            If _object Is Nothing Then
                SyncLock InitLock
                    Thread.MemoryBarrier()
                    If _object Is Nothing Then
                        _object = New UtsFtp(UtsRegistry.FtpHost, FtpPort, FtpUser, FtpPwd)
                    End If
                End SyncLock
            End If
            Return _object
        End Function

        Private 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


        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>
        ''' 创建文件夹
        ''' </summary>
        ''' <param name="remoteDir">Ftp文件夹路径</param>
        ''' <param name="force">创建所有不存在的文件夹路径</param>
        Public Sub CreateDir(remoteDir As String, Optional force As Boolean = False)
            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.CreateDirectory(remoteDir, force)
                ftpClient.Disconnect()
            End Using
        End Sub


        ''' <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

