This commit is contained in:
Starainrt
2021-05-17 15:11:24 +08:00
parent bcc27ff52f
commit be040542bb
2 changed files with 164 additions and 106 deletions
+59 -1
View File
@@ -7,6 +7,19 @@ import (
"os"
)
func (star *StarSSH) CreateSftpClient() (*sftp.Client, error) {
return sftp.NewClient(star.Client)
}
func (star *StarSSH) SftpTransferOut(localFilePath, remotePath string) error {
sftpC, err := star.CreateSftpClient()
if err != nil {
return err
}
defer sftpC.Close()
return SftpTransferOut(localFilePath, remotePath, sftpC)
}
func SftpTransferOut(localFilePath, remotePath string, sftpClient *sftp.Client) error {
srcFile, err := os.Open(localFilePath)
if err != nil {
@@ -30,6 +43,15 @@ func SftpTransferOut(localFilePath, remotePath string, sftpClient *sftp.Client)
return nil
}
func (star *StarSSH) SftpTransferOutByte(localData []byte, remotePath string) error {
sftpC, err := star.CreateSftpClient()
if err != nil {
return err
}
defer sftpC.Close()
return SftpTransferOutByte(localData, remotePath, sftpC)
}
func SftpTransferOutByte(localData []byte, remotePath string, sftpClient *sftp.Client) error {
dstFile, err := sftpClient.Create(remotePath)
if err != nil {
@@ -40,6 +62,15 @@ func SftpTransferOutByte(localData []byte, remotePath string, sftpClient *sftp.C
return err
}
func (star *StarSSH) SftpTransferOutFunc(localFilePath, remotePath string, bufcap int, rtefunc func(float64)) error {
sftpC, err := star.CreateSftpClient()
if err != nil {
return err
}
defer sftpC.Close()
return SftpTransferOutFunc(localFilePath, remotePath, bufcap, rtefunc, sftpC)
}
func SftpTransferOutFunc(localFilePath, remotePath string, bufcap int, rtefunc func(float64), sftpClient *sftp.Client) error {
num := 0
srcFile, err := os.Open(localFilePath)
@@ -71,6 +102,15 @@ func SftpTransferOutFunc(localFilePath, remotePath string, bufcap int, rtefunc f
return nil
}
func (star *StarSSH) SftpTransferInByte(remotePath string) ([]byte, error) {
sftpC, err := star.CreateSftpClient()
if err != nil {
return nil, err
}
defer sftpC.Close()
return SftpTransferInByte(remotePath, sftpC)
}
func SftpTransferInByte(remotePath string, sftpClient *sftp.Client) ([]byte, error) {
dstFile, err := sftpClient.Open(remotePath)
if err != nil {
@@ -82,6 +122,15 @@ func SftpTransferInByte(remotePath string, sftpClient *sftp.Client) ([]byte, err
return buf.Bytes(), err
}
func (star *StarSSH) SftpTransferIn(src, dst string) error {
sftpC, err := star.CreateSftpClient()
if err != nil {
return err
}
defer sftpC.Close()
return SftpTransferIn(src, dst, sftpC)
}
func SftpTransferIn(src, dst string, sftpClient *sftp.Client) error {
srcFile, err := sftpClient.Open(src)
if err != nil {
@@ -102,6 +151,15 @@ func SftpTransferIn(src, dst string, sftpClient *sftp.Client) error {
return nil
}
func (star *StarSSH) SftpTransferInFunc(src, dst string, bufcap int, rtefunc func(float64)) error {
sftpC, err := star.CreateSftpClient()
if err != nil {
return err
}
defer sftpC.Close()
return SftpTransferInFunc(src, dst, bufcap, rtefunc, sftpC)
}
func SftpTransferInFunc(src, dst string, bufcap int, rtefunc func(float64), sftpClient *sftp.Client) error {
num := 0
srcFile, err := sftpClient.Open(src)
@@ -131,4 +189,4 @@ func SftpTransferInFunc(src, dst string, bufcap int, rtefunc func(float64), sftp
}
}
return nil
}
}