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.
starssh/sftp.go

193 lines
4.2 KiB
Go

package starssh
import (
"bytes"
"github.com/pkg/sftp"
"io"
"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 {
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, 1048576)
n, err := srcFile.Read(buf)
dstFile.Write(buf[:n])
if err == io.EOF {
break
}
}
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 {
return err
}
defer dstFile.Close()
_, err = dstFile.Write(localData)
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)
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 (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 {
return []byte{}, err
}
defer dstFile.Close()
buf := new(bytes.Buffer)
_, err = dstFile.WriteTo(buf)
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 {
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 (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)
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
}