diff --git a/common/index.go b/common/index.go new file mode 100644 index 0000000..5871a85 --- /dev/null +++ b/common/index.go @@ -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 +} diff --git a/file/index.go b/file/index.go index 3b4dae5..27871b6 100644 --- a/file/index.go +++ b/file/index.go @@ -4,7 +4,11 @@ import ( "os" "path" "strings" + "sync" + + . "github.com/MrYZhou/outil/common" ) + /* 合并文件 @@ -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...) } @@ -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) @@ -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) diff --git a/ssh/index.go b/ssh/index.go index 909e6d9..6ea1410 100644 --- a/ssh/index.go +++ b/ssh/index.go @@ -7,6 +7,9 @@ import ( "os" "path" "strings" + "sync" + + . "github.com/MrYZhou/outil/common" . "github.com/MrYZhou/outil/file" "github.com/pkg/sftp" @@ -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) @@ -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() @@ -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)) @@ -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)) } /* @@ -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 +} + /* 上传目录到服务器