From 0040f7161da7b48259fb90b5bc3242083ade61a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camille=20Eyri=C3=A8s?= Date: Sat, 21 Oct 2017 23:52:03 +0200 Subject: [PATCH 1/4] Automatically compress directory to tar.gz archive --- connect.go | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/connect.go b/connect.go index 4d910fa0..9529315e 100644 --- a/connect.go +++ b/connect.go @@ -4,6 +4,7 @@ import ( "encoding/hex" "encoding/json" "fmt" + "github.com/verybluebot/tarinator-go" "io" "net" "os" @@ -33,10 +34,11 @@ type Connection struct { } type FileMetaData struct { - Name string - Size int - Hash string - Path string + Name string + Size int + Hash string + Path string + isDir bool } func NewConnection(flags *Flags) *Connection { @@ -49,6 +51,31 @@ func NewConnection(flags *Flags) *Connection { c.NumberOfConnections = flags.NumberOfConnections c.rate = flags.Rate if len(flags.File) > 0 { + // check wether the file is a dir + info, err := os.Stat(flags.File) + if err != nil { + fmt.Printf("Error! Please submit the following error to https://github.com/schollz/croc/issues:\n\n'%s'\n\n", err.Error()) + os.Exit(1) + } + + if info.Mode().IsDir() { // if our file is a dir + fmt.Print("The file you are trying to send is a directory; compressing...") + + tmpFileName := "to_send.tmp.tar.gz" + // we "tarify" the file + err = tarinator.Tarinate([]string{flags.File}, tmpFileName) + if err != nil { + fmt.Printf("Error! Please submit the following error to https://github.com/schollz/croc/issues:\n\n'%s'\n\n", err.Error()) + os.Exit(1) + } + + // now, we change the target file name to match the new archive created + flags.File = tmpFileName + // we set the value isDir to true + c.File.isDir = true + fmt.Println("Done !") + } + c.File.Name = path.Base(flags.File) c.File.Path = path.Dir(flags.File) c.IsSender = true From 4a66db57f02c02393a49d71ae84b84c7a146b1f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camille=20Eyri=C3=A8s?= Date: Sat, 21 Oct 2017 23:54:47 +0200 Subject: [PATCH 2/4] update .travis.yml --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index ae1b7387..2e4de0f0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,6 +11,6 @@ before_install: - go get github.com/schollz/mnemonicode - go get github.com/pkg/errors - go get github.com/sirupsen/logrus - + - go get github.com/verybluebot/tarinator-go script: - go test -v From 0df9890b8ba32c8064bac32215988495f5717f29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camille=20Eyri=C3=A8s?= Date: Sun, 22 Oct 2017 00:28:06 +0200 Subject: [PATCH 3/4] fix weird bug and remove tmp file after use --- connect.go | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/connect.go b/connect.go index 9529315e..208c6181 100644 --- a/connect.go +++ b/connect.go @@ -41,6 +41,10 @@ type FileMetaData struct { isDir bool } +const ( + tmpTarGzFileName = "to_send.tmp.tar.gz" +) + func NewConnection(flags *Flags) *Connection { c := new(Connection) c.Debug = flags.Debug @@ -61,22 +65,23 @@ func NewConnection(flags *Flags) *Connection { if info.Mode().IsDir() { // if our file is a dir fmt.Print("The file you are trying to send is a directory; compressing...") - tmpFileName := "to_send.tmp.tar.gz" // we "tarify" the file - err = tarinator.Tarinate([]string{flags.File}, tmpFileName) + err = tarinator.Tarinate([]string{flags.File}, tmpTarGzFileName) if err != nil { fmt.Printf("Error! Please submit the following error to https://github.com/schollz/croc/issues:\n\n'%s'\n\n", err.Error()) os.Exit(1) } // now, we change the target file name to match the new archive created - flags.File = tmpFileName + flags.File = tmpTarGzFileName // we set the value isDir to true c.File.isDir = true fmt.Println("Done !") + c.File.Name = path.Base(tmpTarGzFileName) + } else { + c.File.Name = path.Base(flags.File) } - c.File.Name = path.Base(flags.File) c.File.Path = path.Dir(flags.File) c.IsSender = true } else { @@ -173,6 +178,14 @@ func (c *Connection) Run() error { if err := os.Remove(c.File.Name + ".enc"); err != nil { return err } + + // remove compressed archive + if c.File.isDir { + if err := os.Remove(tmpTarGzFileName); err != nil { + return err + } + } + fmt.Printf("Sending %d byte file named '%s'\n", c.File.Size, c.File.Name) fmt.Printf("Code is: %s\n", c.Code) } @@ -374,6 +387,7 @@ func (c *Connection) runClient() error { } else { fmt.Printf("\nReceived file written to %s\n", c.File.Name) } + } return nil } From 14a168c9086ffec8c7de25d9aa1332faf8a06e19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camille=20Eyri=C3=A8s?= Date: Sun, 22 Oct 2017 00:55:34 +0200 Subject: [PATCH 4/4] export isDir --- connect.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/connect.go b/connect.go index 208c6181..49c277d3 100644 --- a/connect.go +++ b/connect.go @@ -38,7 +38,7 @@ type FileMetaData struct { Size int Hash string Path string - isDir bool + IsDir bool } const ( @@ -74,8 +74,8 @@ func NewConnection(flags *Flags) *Connection { // now, we change the target file name to match the new archive created flags.File = tmpTarGzFileName - // we set the value isDir to true - c.File.isDir = true + // we set the value IsDir to true + c.File.IsDir = true fmt.Println("Done !") c.File.Name = path.Base(tmpTarGzFileName) } else { @@ -180,7 +180,7 @@ func (c *Connection) Run() error { } // remove compressed archive - if c.File.isDir { + if c.File.IsDir { if err := os.Remove(tmpTarGzFileName); err != nil { return err }