1 Star 0 Fork 0

h79 / goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
filepath.go 1.17 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2023-04-05 16:09 . scp
package ssh
import (
"gitee.com/h79/goutils/common/file"
"io"
"os"
"path"
)
type Path struct {
Mode os.FileMode
Name string
IsDir bool
}
type FileInfo struct {
Name string
Size int64
Mode os.FileMode
}
func (fi *FileInfo) Receive(opt *Option, src io.Reader) error {
dst, err := fi.CreateFile(opt)
if err != nil {
return err
}
defer dst.Close()
n, err := file.CopyN(dst, src, fi.Size)
if err != nil {
return err
}
if n != fi.Size {
return ErrCopy
}
return nil
}
func (fi *FileInfo) CreateFile(opt *Option) (io.WriteCloser, error) {
// Create a local file to write to
dst, err := os.OpenFile(localFile(opt), os.O_RDWR|os.O_CREATE, opt.Local.Mode|fi.Mode)
if err != nil {
return nil, err
}
return dst, nil
}
func remoteFile(opt *Option) string {
if opt.Remote.IsDir {
filename := path.Base(opt.Local.Name)
opt.Remote.Name = path.Join(opt.Remote.Name, filename)
opt.Remote.IsDir = false
return filename
}
return path.Base(opt.Remote.Name)
}
func localFile(opt *Option) string {
if opt.Local.IsDir {
filename := path.Base(opt.Remote.Name)
opt.Local.Name = path.Join(opt.Local.Name, filename)
opt.Local.IsDir = false
}
return opt.Local.Name
}
Go
1
https://gitee.com/h79/goutils.git
git@gitee.com:h79/goutils.git
h79
goutils
goutils
v1.20.57

搜索帮助

53164aa7 5694891 3bd8fe86 5694891