mirror of https://github.com/schollz/croc.git
move file processing until after recipient chimes in
This commit is contained in:
parent
4d9455c24b
commit
9cf6bdf4a8
27
main.go
27
main.go
|
@ -3,6 +3,9 @@ package main
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
@ -100,7 +103,26 @@ func send(c *cli.Context) error {
|
||||||
stat, _ := os.Stdin.Stat()
|
stat, _ := os.Stdin.Stat()
|
||||||
var fname string
|
var fname string
|
||||||
if (stat.Mode() & os.ModeCharDevice) == 0 {
|
if (stat.Mode() & os.ModeCharDevice) == 0 {
|
||||||
fname = "stdin"
|
f, err := ioutil.TempFile(".", "croc-stdin-")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = io.Copy(f, os.Stdin)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = f.Close()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fname = f.Name()
|
||||||
|
defer func() {
|
||||||
|
log.Println("removing %s", fname)
|
||||||
|
err = os.Remove(fname)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
} else {
|
} else {
|
||||||
fname = c.Args().First()
|
fname = c.Args().First()
|
||||||
}
|
}
|
||||||
|
@ -140,8 +162,7 @@ func send(c *cli.Context) error {
|
||||||
codePhrase,
|
codePhrase,
|
||||||
codePhrase,
|
codePhrase,
|
||||||
)
|
)
|
||||||
err = cr.Send(fname, codePhrase)
|
return cr.Send(fname, codePhrase)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func receive(c *cli.Context) error {
|
func receive(c *cli.Context) error {
|
||||||
|
|
|
@ -60,6 +60,7 @@ func (c *Croc) Send(fname, codephrase string) (err error) {
|
||||||
|
|
||||||
err = <-errChan
|
err = <-errChan
|
||||||
if err == nil || waitingFor == 1 {
|
if err == nil || waitingFor == 1 {
|
||||||
|
log.Debug("returning")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Debug(err)
|
log.Debug(err)
|
||||||
|
|
|
@ -42,42 +42,15 @@ func Send(done chan struct{}, c *websocket.Conn, fname string, codephrase string
|
||||||
}
|
}
|
||||||
|
|
||||||
func send(c *websocket.Conn, fname string, codephrase string) (err error) {
|
func send(c *websocket.Conn, fname string, codephrase string) (err error) {
|
||||||
// check that the file exists
|
var f *os.File
|
||||||
|
var fstats models.FileStats
|
||||||
|
|
||||||
|
// normalize the file name
|
||||||
fname, err = filepath.Abs(fname)
|
fname, err = filepath.Abs(fname)
|
||||||
_, filename := filepath.Split(fname)
|
|
||||||
f, err := os.Open(fname)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fstat, err := f.Stat()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
_, filename := filepath.Split(fname)
|
||||||
// get stats about the file
|
|
||||||
fstats := models.FileStats{filename, fstat.Size(), fstat.ModTime(), fstat.IsDir(), fstat.Name()}
|
|
||||||
if fstats.IsDir {
|
|
||||||
// zip the directory
|
|
||||||
fstats.SentName, err = zipper.ZipFile(fname, true)
|
|
||||||
// remove the file when leaving
|
|
||||||
defer os.Remove(fstats.SentName)
|
|
||||||
fname = fstats.SentName
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
f.Close()
|
|
||||||
// reopen file
|
|
||||||
f, err = os.Open(fstats.SentName)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fstat, err := f.Stat()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
// get new size
|
|
||||||
fstats.Size = fstat.Size()
|
|
||||||
}
|
|
||||||
|
|
||||||
// get ready to generate session key
|
// get ready to generate session key
|
||||||
var sessionKey []byte
|
var sessionKey []byte
|
||||||
|
@ -107,6 +80,40 @@ func send(c *websocket.Conn, fname string, codephrase string) (err error) {
|
||||||
log.Debugf("got %d: %s", messageType, message)
|
log.Debugf("got %d: %s", messageType, message)
|
||||||
switch step {
|
switch step {
|
||||||
case 0:
|
case 0:
|
||||||
|
// recipient might want file! gather file information
|
||||||
|
// get stats about the file
|
||||||
|
fstat, err := os.Stat(fname)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fstats = models.FileStats{filename, fstat.Size(), fstat.ModTime(), fstat.IsDir(), fstat.Name()}
|
||||||
|
if fstats.IsDir {
|
||||||
|
// zip the directory
|
||||||
|
fstats.SentName, err = zipper.ZipFile(fname, true)
|
||||||
|
// remove the file when leaving
|
||||||
|
defer os.Remove(fstats.SentName)
|
||||||
|
fname = fstats.SentName
|
||||||
|
|
||||||
|
fstat, err := os.Stat(fname)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// get new size
|
||||||
|
fstats.Size = fstat.Size()
|
||||||
|
}
|
||||||
|
|
||||||
|
// open the file
|
||||||
|
f, err = os.Open(fname)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
err = f.Close()
|
||||||
|
if err != nil {
|
||||||
|
log.Debugf("problem closing file: %s", err.Error())
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
// send pake data
|
// send pake data
|
||||||
log.Debugf("[%d] first, P sends u to Q", step)
|
log.Debugf("[%d] first, P sends u to Q", step)
|
||||||
c.WriteMessage(websocket.BinaryMessage, P.Bytes())
|
c.WriteMessage(websocket.BinaryMessage, P.Bytes())
|
||||||
|
|
Loading…
Reference in New Issue