mirror of
https://github.com/schollz/croc.git
synced 2025-10-11 13:21:00 +02:00
Merge pull request #35 from jesuiscamille/master
Automatically compress directory to a tar.gz archive
This commit is contained in:
commit
3bd96564f7
2 changed files with 47 additions and 6 deletions
|
@ -11,6 +11,6 @@ before_install:
|
||||||
- go get github.com/schollz/mnemonicode
|
- go get github.com/schollz/mnemonicode
|
||||||
- go get github.com/pkg/errors
|
- go get github.com/pkg/errors
|
||||||
- go get github.com/sirupsen/logrus
|
- go get github.com/sirupsen/logrus
|
||||||
|
- go get github.com/verybluebot/tarinator-go
|
||||||
script:
|
script:
|
||||||
- go test -v
|
- go test -v
|
||||||
|
|
51
connect.go
51
connect.go
|
@ -4,6 +4,7 @@ import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/verybluebot/tarinator-go"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
@ -33,12 +34,17 @@ type Connection struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type FileMetaData struct {
|
type FileMetaData struct {
|
||||||
Name string
|
Name string
|
||||||
Size int
|
Size int
|
||||||
Hash string
|
Hash string
|
||||||
Path string
|
Path string
|
||||||
|
IsDir bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
tmpTarGzFileName = "to_send.tmp.tar.gz"
|
||||||
|
)
|
||||||
|
|
||||||
func NewConnection(flags *Flags) *Connection {
|
func NewConnection(flags *Flags) *Connection {
|
||||||
c := new(Connection)
|
c := new(Connection)
|
||||||
c.Debug = flags.Debug
|
c.Debug = flags.Debug
|
||||||
|
@ -49,7 +55,33 @@ func NewConnection(flags *Flags) *Connection {
|
||||||
c.NumberOfConnections = flags.NumberOfConnections
|
c.NumberOfConnections = flags.NumberOfConnections
|
||||||
c.rate = flags.Rate
|
c.rate = flags.Rate
|
||||||
if len(flags.File) > 0 {
|
if len(flags.File) > 0 {
|
||||||
c.File.Name = path.Base(flags.File)
|
// 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...")
|
||||||
|
|
||||||
|
// we "tarify" the file
|
||||||
|
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 = 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.Path = path.Dir(flags.File)
|
c.File.Path = path.Dir(flags.File)
|
||||||
c.IsSender = true
|
c.IsSender = true
|
||||||
} else {
|
} else {
|
||||||
|
@ -146,6 +178,14 @@ func (c *Connection) Run() error {
|
||||||
if err := os.Remove(c.File.Name + ".enc"); err != nil {
|
if err := os.Remove(c.File.Name + ".enc"); err != nil {
|
||||||
return err
|
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("Sending %d byte file named '%s'\n", c.File.Size, c.File.Name)
|
||||||
fmt.Printf("Code is: %s\n", c.Code)
|
fmt.Printf("Code is: %s\n", c.Code)
|
||||||
}
|
}
|
||||||
|
@ -349,6 +389,7 @@ func (c *Connection) runClient() error {
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("\nReceived file written to %s\n", c.File.Name)
|
fmt.Printf("\nReceived file written to %s\n", c.File.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue