Skip to content

Commit

Permalink
Automatically compress directory to tar.gz archive
Browse files Browse the repository at this point in the history
  • Loading branch information
clouedoc committed Oct 21, 2017
1 parent 17d7b76 commit 0040f71
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"github.com/verybluebot/tarinator-go"
"io"
"net"
"os"
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit 0040f71

Please sign in to comment.