mirror of
https://github.com/schollz/croc.git
synced 2025-10-11 13:21:00 +02:00
Simplified UI for sending directories
This commit is contained in:
parent
7fbfe7cecc
commit
6b9cc84313
2 changed files with 39 additions and 31 deletions
62
connect.go
62
connect.go
|
@ -4,7 +4,6 @@ import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/verybluebot/tarinator-go"
|
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
@ -14,6 +13,8 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/verybluebot/tarinator-go"
|
||||||
|
|
||||||
"github.com/gosuri/uiprogress"
|
"github.com/gosuri/uiprogress"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
@ -48,7 +49,7 @@ const (
|
||||||
tmpTarGzFileName = "to_send.tmp.tar.gz"
|
tmpTarGzFileName = "to_send.tmp.tar.gz"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewConnection(flags *Flags) *Connection {
|
func NewConnection(flags *Flags) (*Connection, error) {
|
||||||
c := new(Connection)
|
c := new(Connection)
|
||||||
c.Debug = flags.Debug
|
c.Debug = flags.Debug
|
||||||
c.DontEncrypt = flags.DontEncrypt
|
c.DontEncrypt = flags.DontEncrypt
|
||||||
|
@ -61,31 +62,24 @@ func NewConnection(flags *Flags) *Connection {
|
||||||
// check wether the file is a dir
|
// check wether the file is a dir
|
||||||
info, err := os.Stat(flags.File)
|
info, err := os.Stat(flags.File)
|
||||||
if err != nil {
|
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())
|
return c, err
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if info.Mode().IsDir() { // if our file is a dir
|
if info.Mode().IsDir() { // if our file is a dir
|
||||||
fmt.Print("The file you are trying to send is a directory; compressing...")
|
fmt.Println("compressing directory...")
|
||||||
|
|
||||||
// we "tarify" the file
|
// we "tarify" the file
|
||||||
err = tarinator.Tarinate([]string{flags.File}, tmpTarGzFileName)
|
err = tarinator.Tarinate([]string{flags.File}, path.Base(flags.File)+".tar")
|
||||||
if err != nil {
|
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())
|
return c, err
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// now, we change the target file name to match the new archive created
|
// now, we change the target file name to match the new archive created
|
||||||
flags.File = tmpTarGzFileName
|
flags.File = path.Base(flags.File) + ".tar"
|
||||||
// we set the value IsDir to true
|
// we set the value IsDir to true
|
||||||
c.File.IsDir = 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.File.Path = path.Dir(flags.File)
|
||||||
c.IsSender = true
|
c.IsSender = true
|
||||||
} else {
|
} else {
|
||||||
|
@ -101,7 +95,7 @@ func NewConnection(flags *Flags) *Connection {
|
||||||
log.SetLevel(log.WarnLevel)
|
log.SetLevel(log.WarnLevel)
|
||||||
}
|
}
|
||||||
|
|
||||||
return c
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Connection) Run() error {
|
func (c *Connection) Run() error {
|
||||||
|
@ -187,12 +181,18 @@ func (c *Connection) Run() error {
|
||||||
|
|
||||||
// remove compressed archive
|
// remove compressed archive
|
||||||
if c.File.IsDir {
|
if c.File.IsDir {
|
||||||
if err := os.Remove(tmpTarGzFileName); err != nil {
|
log.Debug("removing archive: " + c.File.Name)
|
||||||
|
if err := os.Remove(c.File.Name); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Sending %d byte file named '%s'\n", c.File.Size, c.File.Name)
|
if c.File.IsDir {
|
||||||
|
fmt.Printf("Sending %d byte folder named '%s'\n", c.File.Size, c.File.Name[:len(c.File.Name)-4])
|
||||||
|
} else {
|
||||||
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -399,18 +399,22 @@ func (c *Connection) runClient() error {
|
||||||
if c.File.Hash != fileHash {
|
if c.File.Hash != fileHash {
|
||||||
return fmt.Errorf("\nUh oh! %s is corrupted! Sorry, try again.\n", c.File.Name)
|
return fmt.Errorf("\nUh oh! %s is corrupted! Sorry, try again.\n", c.File.Name)
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("\nReceived file written to %s\n", path.Join(c.Path, c.File.Name))
|
if c.File.IsDir { // if the file was originally a dir
|
||||||
}
|
fmt.Print("decompressing folder")
|
||||||
|
log.Debug("untarring " + c.File.Name)
|
||||||
|
err := tarinator.UnTarinate(c.Path, path.Join(c.Path, c.File.Name))
|
||||||
|
|
||||||
if c.File.IsDir { // if the file was originally a dir
|
if err != nil {
|
||||||
fmt.Print("Since the receive file was originally a directory, uncompressing... ")
|
return err
|
||||||
tarinator.UnTarinate(crocReceiveDir, tmpTarGzFileName)
|
}
|
||||||
fmt.Println("Done !\nDirectory written into " + crocReceiveDir)
|
// we remove the old tar.gz file
|
||||||
|
err = os.Remove(path.Join(c.Path, c.File.Name))
|
||||||
// we remove the old tar.gz file
|
if err != nil {
|
||||||
err := os.RemoveAll(tmpTarGzFileName)
|
return err
|
||||||
if err != nil {
|
}
|
||||||
log.Error(err)
|
fmt.Printf("\nReceived folder written to %s\n", path.Join(c.Path, c.File.Name[:len(c.File.Name)-4]))
|
||||||
|
} else {
|
||||||
|
fmt.Printf("\nReceived file written to %s\n", path.Join(c.Path, c.File.Name))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
8
main.go
8
main.go
|
@ -59,8 +59,12 @@ func main() {
|
||||||
r := NewRelay(flags)
|
r := NewRelay(flags)
|
||||||
r.Run()
|
r.Run()
|
||||||
} else {
|
} else {
|
||||||
c := NewConnection(flags)
|
c, err := NewConnection(flags)
|
||||||
err := c.Run()
|
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())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = c.Run()
|
||||||
if err != nil {
|
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())
|
fmt.Printf("Error! Please submit the following error to https://github.com/schollz/croc/issues:\n\n'%s'\n\n", err.Error())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue