You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
134 lines
2.7 KiB
Go
134 lines
2.7 KiB
Go
4 years ago
|
package starssh
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"github.com/pkg/sftp"
|
||
|
"io"
|
||
|
"os"
|
||
|
)
|
||
|
|
||
|
func SftpTransferOut(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(remotePath)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
defer dstFile.Close()
|
||
|
for {
|
||
|
buf := make([]byte, 1024)
|
||
|
n, err := srcFile.Read(buf)
|
||
|
dstFile.Write(buf[:n])
|
||
|
if err == io.EOF {
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func SftpTransferOutByte(localData []byte, remotePath string, sftpClient *sftp.Client) error {
|
||
|
dstFile, err := sftpClient.Create(remotePath)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
defer dstFile.Close()
|
||
|
_, err = dstFile.Write(localData)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
func SftpTransferOutFunc(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()
|
||
|
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, bufcap)
|
||
|
n, err := srcFile.Read(buf)
|
||
|
num += n
|
||
|
go rtefunc(float64(num) / filebig * 100)
|
||
|
dstFile.Write(buf[:n])
|
||
|
if err == io.EOF {
|
||
|
break
|
||
|
}
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func SftpTransferInByte(remotePath string, sftpClient *sftp.Client) ([]byte, error) {
|
||
|
dstFile, err := sftpClient.Open(remotePath)
|
||
|
if err != nil {
|
||
|
return []byte{}, err
|
||
|
}
|
||
|
defer dstFile.Close()
|
||
|
buf := new(bytes.Buffer)
|
||
|
_, err = dstFile.WriteTo(buf)
|
||
|
return buf.Bytes(), err
|
||
|
}
|
||
|
|
||
|
func SftpTransferIn(src, dst string, sftpClient *sftp.Client) error {
|
||
|
srcFile, err := sftpClient.Open(src)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
defer srcFile.Close()
|
||
|
//var localFileName = filepath.Base(src)
|
||
|
dstFile, err := os.Create(dst)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
defer dstFile.Close()
|
||
|
|
||
|
if _, err = srcFile.WriteTo(dstFile); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func SftpTransferInFunc(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()
|
||
|
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, bufcap)
|
||
|
n, err := srcFile.Read(buf)
|
||
|
num += n
|
||
|
go rtefunc(float64(num) / filebig * 100)
|
||
|
dstFile.Write(buf[:n])
|
||
|
if err == io.EOF {
|
||
|
break
|
||
|
}
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
return nil
|
||
|
}
|