0
0
Fork 0
mirror of https://github.com/schollz/croc.git synced 2025-10-11 13:21:00 +02:00

allow for creating folders on remote

This commit is contained in:
Zack Scholl 2019-04-03 21:45:08 -07:00
parent 4c0c16cddb
commit f74816c7c7
2 changed files with 71 additions and 13 deletions

View file

@ -15,7 +15,12 @@ func main() {
panic(err) panic(err)
} }
if sender { if sender {
err = c.Send("test.txt") err = c.Send(croc.TransferOptions{
// PathToFile: "../wskeystore/README.md",
// PathToFile: "./src/croc/croc.go",
PathToFile: "C:\\Users\\zacks\\go\\src\\github.com\\schollz\\croc\\src\\croc\\croc.go",
KeepPathInRemote: false,
})
} else { } else {
err = c.Receive() err = c.Receive()
} }

View file

@ -7,6 +7,9 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"path"
"path/filepath"
"strings"
"sync" "sync"
"time" "time"
@ -37,6 +40,7 @@ type Client struct {
SharedSecret string SharedSecret string
Pake *pake.Pake Pake *pake.Pake
Filename string Filename string
Folder string
// steps involved in forming relationship // steps involved in forming relationship
Step1ChannelSecured bool Step1ChannelSecured bool
@ -74,9 +78,9 @@ type Chunk struct {
type FileInfo struct { type FileInfo struct {
Name string `json:"n,omitempty"` Name string `json:"n,omitempty"`
Folder string `json:"f,omitempty"`
Size int64 `json:"s,omitempty"` Size int64 `json:"s,omitempty"`
ModTime time.Time `json:"m,omitempty"` ModTime time.Time `json:"m,omitempty"`
IsDir bool `json:"d,omitempty"`
IsCompressed bool `json:"c,omitempty"` IsCompressed bool `json:"c,omitempty"`
IsEncrypted bool `json:"e,omitempty"` IsEncrypted bool `json:"e,omitempty"`
} }
@ -146,21 +150,64 @@ func New(sender bool, sharedSecret string) (c *Client, err error) {
return return
} }
type TransferOptions struct {
PathToFile string
KeepPathInRemote bool
}
// Send will send the specified file // Send will send the specified file
func (c *Client) Send(fname string) (err error) { func (c *Client) Send(options TransferOptions) (err error) {
return c.transfer(fname) return c.transfer(options)
} }
// Receive will receive a file // Receive will receive a file
func (c *Client) Receive() (err error) { func (c *Client) Receive() (err error) {
return c.transfer() return c.transfer(TransferOptions{})
} }
func (c *Client) transfer(fnameOption ...string) (err error) { func (c *Client) transfer(options TransferOptions) (err error) {
if len(fnameOption) > 0 { if c.IsSender {
c.Filename = fnameOption[0] var fstats os.FileInfo
} fstats, err = os.Stat(path.Join(options.PathToFile))
if err != nil {
return
}
c.FileInfo = FileInfo{
Name: fstats.Name(),
Folder: ".",
Size: fstats.Size(),
ModTime: fstats.ModTime(),
}
if options.KeepPathInRemote {
var fullPath, curFolder string
fullPath, err = filepath.Abs(options.PathToFile)
if err != nil {
return
}
fullPath = filepath.Clean(fullPath)
folderName, _ := filepath.Split(fullPath)
curFolder, err = os.Getwd()
if err != nil {
return
}
curFolder, err = filepath.Abs(curFolder)
if err != nil {
return
}
if !strings.HasPrefix(folderName, curFolder) {
err = fmt.Errorf("remote directory must be relative to current")
return
}
c.FileInfo.Folder = strings.TrimPrefix(folderName, curFolder)
c.FileInfo.Folder = filepath.ToSlash(c.FileInfo.Folder)
c.FileInfo.Folder = strings.TrimPrefix(c.FileInfo.Folder, "/")
if c.FileInfo.Folder == "" {
c.FileInfo.Folder = "."
}
}
log.Debugf("file info: %+v", c.FileInfo)
}
// create channel for quitting // create channel for quitting
// quit with c.quit <- true // quit with c.quit <- true
c.quit = make(chan bool) c.quit = make(chan bool)
@ -265,7 +312,13 @@ func (c *Client) processMessage(m Message) (err error) {
return return
} }
c.log.Debug(c.FileInfo) c.log.Debug(c.FileInfo)
c.f, err = os.Create("d.txt") if c.FileInfo.Folder != "." {
err = os.MkdirAll(c.FileInfo.Folder, os.ModeDir)
if err != nil {
return
}
}
c.f, err = os.Create(path.Join(c.FileInfo.Folder, c.FileInfo.Name))
if err != nil { if err != nil {
return return
} }
@ -321,15 +374,15 @@ func (c *Client) processMessage(m Message) (err error) {
func (c *Client) updateState() (err error) { func (c *Client) updateState() (err error) {
if c.IsSender && c.Step1ChannelSecured && !c.Step2FileInfoTransfered { if c.IsSender && c.Step1ChannelSecured && !c.Step2FileInfoTransfered {
var fstats os.FileInfo var fstats os.FileInfo
fstats, err = os.Stat(c.Filename) fstats, err = os.Stat(path.Join(c.Folder, c.Filename))
if err != nil { if err != nil {
return return
} }
c.FileInfo = FileInfo{ c.FileInfo = FileInfo{
Name: fstats.Name(), Name: c.Filename,
Folder: c.Folder,
Size: fstats.Size(), Size: fstats.Size(),
ModTime: fstats.ModTime(), ModTime: fstats.ModTime(),
IsDir: fstats.IsDir(),
} }
b, _ := json.Marshal(c.FileInfo) b, _ := json.Marshal(c.FileInfo)
err = c.redisdb.Publish(c.nameOutChannel, Message{ err = c.redisdb.Publish(c.nameOutChannel, Message{