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

4 years ago
package starssh
import (
"bytes"
"github.com/pkg/sftp"
"io"
"os"
)
4 years ago
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)
}
4 years ago
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)
4 years ago
n, err := srcFile.Read(buf)
dstFile.Write(buf[:n])
if err == io.EOF {
break
}
}
return nil
}
4 years ago
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)
}
4 years ago
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
}
4 years ago
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)
}
4 years ago
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
}
4 years ago
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)
}
4 years ago
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
}
4 years ago
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)
}
4 years ago
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
}
4 years ago
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)
}
4 years ago
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
4 years ago
}