initial implementation for reading input files from stdin

This commit is contained in:
Travis 2018-04-13 09:31:43 -07:00 committed by Zack Scholl
parent a35612abfe
commit 19bfbcb9c2
2 changed files with 31 additions and 10 deletions

View File

@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"os/signal"
@ -40,12 +41,13 @@ type Connection struct {
}
type FileMetaData struct {
Name string
Size int
Hash string
Path string
IsDir bool
IsEncrypted bool
Name string
Size int
Hash string
Path string
IsDir bool
IsEncrypted bool
DeleteAfterSending bool
}
const (
@ -63,6 +65,22 @@ func NewConnection(config *AppConfig) (*Connection, error) {
c.NumberOfConnections = config.NumberOfConnections
c.rate = config.Rate
if len(config.File) > 0 {
if config.File == "stdin" {
f, err := ioutil.TempFile(os.TempDir(), "croc-stdin-")
if err != nil {
return c, err
}
_, err = io.Copy(f, os.Stdin)
if err != nil {
return c, err
}
config.File = f.Name()
err = f.Close()
if err != nil {
return c, err
}
c.File.DeleteAfterSending = true
}
// check wether the file is a dir
info, err := os.Stat(config.File)
if err != nil {
@ -108,7 +126,6 @@ func (c *Connection) cleanup() {
os.Remove(path.Join(c.Path, c.File.Name+".enc."+strconv.Itoa(id)))
}
os.Remove(path.Join(c.Path, c.File.Name+".enc"))
}
func (c *Connection) Run() error {
@ -635,5 +652,8 @@ func (c *Connection) sendFile(id int, connection net.Conn) error {
file.Close()
err = os.Remove(c.File.Name + ".enc." + strconv.Itoa(id))
}
if err != nil && c.File.DeleteAfterSending {
err = os.Remove(path.Join(c.File.Path, c.File.Name))
}
return err
}

View File

@ -3,10 +3,11 @@ package main
import (
"bufio"
"fmt"
"github.com/urfave/cli"
"github.com/yudai/gotty/pkg/homedir"
"os"
"strings"
"github.com/urfave/cli"
"github.com/yudai/gotty/pkg/homedir"
)
const BUFFERSIZE = 1024
@ -19,7 +20,7 @@ type AppConfig struct {
PathSpec bool `yaml:"ask-save" flagName:"ask-save" flagSName:"q" flagDescribe:"Ask for path to save to" default:"false"`
DontEncrypt bool `yaml:"no-encrypt" flagName:"no-encrypt" flagSName:"g" flagDescribe:"Turn off encryption" default:"false"`
Server string `yaml:"server" flagName:"server" flagSName:"l" flagDescribe:"Address of relay server" default:"cowyo.com"`
File string `yaml:"send" flagName:"send" flagSName:"s" flagDescribe:"File to send" default:""`
File string `yaml:"send" flagName:"send" flagSName:"s" flagDescribe:"File to send (\"stdin\" to read from stdin)" default:""`
Path string `yaml:"save" flagName:"save" flagSName:"p" flagDescribe:"Path to save to" default:""`
Code string `yaml:"code" flagName:"code" flagSName:"c" flagDescribe:"Use your own code phrase" default:""`
Rate int `yaml:"rate" flagName:"rate" flagSName:"R" flagDescribe:"Throttle down to speed in kbps" default:"1000000"`