diff --git a/ssh.go b/ssh.go index e7fce89..dd727c6 100644 --- a/ssh.go +++ b/ssh.go @@ -9,8 +9,6 @@ import ( "io/ioutil" "net" "os" - "path" - "path/filepath" "regexp" "strings" "time" @@ -469,14 +467,14 @@ func CreateSftp(client *ssh.Client) (*sftp.Client, error) { return sftpClient, err } -func FtpTransferOut(localFilePath, remoteDir string, sftpClient *sftp.Client) error { +func FtpTransferOut(localFilePath, remotePath string, sftpClient *sftp.Client) error { srcFile, err := os.Open(localFilePath) if err != nil { return err } defer srcFile.Close() - var remoteFileName = filepath.Base(localFilePath) - dstFile, err := sftpClient.Create(path.Join(remoteDir, remoteFileName)) + // var remoteFileName = filepath.Base(localFilePath) + dstFile, err := sftpClient.Create(remotePath) if err != nil { return err } @@ -492,28 +490,33 @@ func FtpTransferOut(localFilePath, remoteDir string, sftpClient *sftp.Client) er return nil } -func FtpTransferOutFunc(localFilePath, remoteDir string, bufcap int, rtefunc func(int), sftpClient *sftp.Client) error { +func FtpTransferOutFunc(localFilePath, remotePath string, bufcap int, rtefunc func(float64), sftpClient *sftp.Client) error { num := 0 srcFile, err := os.Open(localFilePath) if err != nil { return err } defer srcFile.Close() - var remoteFileName = filepath.Base(localFilePath) - dstFile, err := sftpClient.Create(path.Join(remoteDir, remoteFileName)) + stat, _ := os.Stat(localFilePath) + filebig := float64(stat.Size()) + //var remoteFileName = filepath.Base(localFilePath) + dstFile, err := sftpClient.Create(remotePath) if err != nil { return err } defer dstFile.Close() for { - buf := make([]byte, 1024) + buf := make([]byte, bufcap) n, err := srcFile.Read(buf) - dstFile.Write(buf[:n]) - num++ - rtefunc(num) if err == io.EOF { break } + if err != nil { + return err + } + num += n + go rtefunc(float64(num) / filebig * 100) + dstFile.Write(buf[:n]) } return nil } @@ -524,8 +527,8 @@ func FtpTransferIn(src, dst string, sftpClient *sftp.Client) error { return err } defer srcFile.Close() - var localFileName = filepath.Base(src) - dstFile, err := os.Create(filepath.Join(dst, localFileName)) + //var localFileName = filepath.Base(src) + dstFile, err := os.Create(dst) if err != nil { return err } @@ -538,28 +541,33 @@ func FtpTransferIn(src, dst string, sftpClient *sftp.Client) error { return nil } -func FtpTransferInFunc(src, dst string, bufcap int, rtefunc func(int), sftpClient *sftp.Client) error { +func FtpTransferInFunc(src, dst string, bufcap int, rtefunc func(float64), sftpClient *sftp.Client) error { num := 0 srcFile, err := sftpClient.Open(src) if err != nil { return err } defer srcFile.Close() - var localFileName = filepath.Base(src) - dstFile, err := os.Create(filepath.Join(dst, localFileName)) + stat, _ := srcFile.Stat() + filebig := float64(stat.Size()) + //var localFileName = filepath.Base(src) + dstFile, err := os.Create(dst) if err != nil { return err } defer dstFile.Close() for { - buf := make([]byte, 1024) + buf := make([]byte, bufcap) n, err := srcFile.Read(buf) - dstFile.Write(buf[:n]) - num++ - rtefunc(num) if err == io.EOF { break } + if err != nil { + return err + } + num += n + go rtefunc(float64(num) / filebig * 100) + dstFile.Write(buf[:n]) } return nil }