Skip to content

Commit

Permalink
ssh问题修复
Browse files Browse the repository at this point in the history
  • Loading branch information
lingen committed Jan 6, 2023
1 parent 1efb37d commit d45b16f
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 46 deletions.
22 changes: 22 additions & 0 deletions common/index.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package common

import (
"encoding/hex"
"math/rand"
"time"
)

/*
获取随机字符
num 字符长度
*/
func Random(num int) string {
rand.Seed(time.Now().UnixNano())
uLen := 10
b := make([]byte, uLen)
rand.Read(b)

rand_str := hex.EncodeToString(b)[0:uLen]
return rand_str
}
20 changes: 11 additions & 9 deletions file/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import (
"os"
"path"
"strings"
"sync"

. "github.com/MrYZhou/outil/common"
)

/*
合并文件
Expand All @@ -14,8 +18,7 @@ import (
*/
func combineFile(fileList []string,target string) {
chunkTotal := make([]byte, 0)
for i, name := range fileList {
fmt.Println(i)
for _, name := range fileList {
chunk, _ := os.ReadFile(name)
chunkTotal = append(chunkTotal, chunk...)
}
Expand All @@ -31,9 +34,11 @@ filePath 切片的文件路径
num 切片数量
*/
func SliceFile(out string,filePath string, num int,) []string {

os.MkdirAll(out, os.ModePerm)

f, _ := os.Open(filePath)
fileInfo, _ := f.Stat()

defer f.Close()

size := fileInfo.Size() / int64(num)
Expand All @@ -53,13 +58,10 @@ func SliceFile(out string,filePath string, num int,) []string {
}
// 从源文件读取chunk大小的数据
f.Read(chunk)
rand.Seed(time.Now().UnixNano())
uLen := 20
b := make([]byte, uLen)
rand.Read(b)
rand_str := hex.EncodeToString(b)[0:uLen]

rand_str := Random(10)

targetPath := path.Join(out, rand_str)
targetPath := path.Join(out,"chunk"+ rand_str)
fileList = append(fileList, targetPath)

os.WriteFile(targetPath, []byte(chunk), os.ModePerm)
Expand Down
94 changes: 57 additions & 37 deletions ssh/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"os"
"path"
"strings"
"sync"

. "github.com/MrYZhou/outil/common"

. "github.com/MrYZhou/outil/file"
"github.com/pkg/sftp"
Expand Down Expand Up @@ -67,8 +70,8 @@ func (c Cli) Run(shell string) (string, error) {

r, err := session.StdoutPipe()
if err != nil {
fmt.Println(err)
os.Exit(1001)
fmt.Println(err)
os.Exit(1001)
}
go io.Copy(os.Stdout, r)

Expand All @@ -86,8 +89,15 @@ filePath 切片的文件路径
num 切片数量
*/
func (c *Cli) SliceUpload(target string ,filePath string, num int) []string {
f, _ := os.Open(filePath)
func (c *Cli) SliceUpload(target string, filePath string, num int) []string {

c.createDir(target)

f, err := os.Open(filePath)
if err != nil {
fmt.Println("文件不存在")
return nil
}
fileInfo, _ := f.Stat()

defer f.Close()
Expand All @@ -110,14 +120,9 @@ func (c *Cli) SliceUpload(target string ,filePath string, num int) []string {

f.Read(chunk)

rand.Seed(time.Now().UnixNano())
uLen := 20
b := make([]byte, uLen)
rand.Read(b)
rand_str := Random(10)

rand_str := hex.EncodeToString(b)[0:uLen]

targetPath := path.Join(target, rand_str)
targetPath := path.Join(target, "chunk"+rand_str)
fileList = append(fileList, targetPath)
ftpFile, _ := c.SftpClient.Create(targetPath)
ftpFile.Write([]byte(chunk))
Expand All @@ -136,39 +141,33 @@ func (c *Cli) SliceUpload(target string ,filePath string, num int) []string {
/*
合并远程文件
文件列表
fileList 文件列表
文件合成名
target 文件合成路径
*/
func (c *Cli) combineRemoteFile(fileList []string,target string) {
func (c *Cli) CombineRemoteFile(fileList []string, target string) {

chunkTotal := make([]byte, 0)
// file,_ := c.SftpClient.Create(target)
// fileTemp,_:=c.SftpClient.Create(target)
// reader:=bufio.NewReader(file)
// writer:=bufio.NewWriter(file)

for i, name := range fileList {
fmt.Println(i)
chunk, _ := os.ReadFile(name)
chunkTotal = append(chunkTotal, chunk...)
}
os.WriteFile(target, []byte(chunkTotal), os.ModePerm)
}
ftpBase, _ := c.SftpClient.Open(name)
defer ftpBase.Close()

fileInfo, _ := ftpBase.Stat()
size := fileInfo.Size()
buffer := make([]byte, size)
ftpBase.Read(buffer)
chunkTotal = append(chunkTotal, buffer...)


// 创建目录
func (c *Cli) createDir(dir string) {
c.SftpClient.MkdirAll(dir)
}

// 批量创建目录
func (c *Cli) createDirList(list []string) {
for _, dir := range list {
c.createDir(dir)
}
}

// 判断文件是否存在
func (c *Cli) IsFileExist(path string) bool {
info, _ := c.SftpClient.Stat(path)
if info != nil {
return true
}
return false
ftpFile, _ := c.CreateFile(target)
ftpFile.Write([]byte(chunkTotal))
}

/*
Expand All @@ -193,6 +192,27 @@ func initClient(c *Cli) *Cli {

}

// 创建目录
func (c *Cli) createDir(dir string) {
c.SftpClient.MkdirAll(dir)
}

// 批量创建目录
func (c *Cli) createDirList(list []string) {
for _, dir := range list {
c.createDir(dir)
}
}

// 判断文件是否存在
func (c *Cli) IsFileExist(path string) bool {
info, _ := c.SftpClient.Stat(path)
if info != nil {
return true
}
return false
}

/*
上传目录到服务器
Expand Down

0 comments on commit d45b16f

Please sign in to comment.