mirror of
https://github.com/schollz/croc.git
synced 2025-10-11 13:21:00 +02:00
fix file split bugs and sending server:port display error
This commit is contained in:
parent
33e41b2b0a
commit
407d85270c
3 changed files with 13 additions and 29 deletions
|
@ -13,7 +13,7 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
humanize "github.com/dustin/go-humanize"
|
"github.com/dustin/go-humanize"
|
||||||
"github.com/schollz/progressbar"
|
"github.com/schollz/progressbar"
|
||||||
"github.com/schollz/tarinator-go"
|
"github.com/schollz/tarinator-go"
|
||||||
|
|
||||||
|
|
2
relay.go
2
relay.go
|
@ -254,7 +254,7 @@ func receiveMessage(connection net.Conn) string {
|
||||||
logger.Warn("read deadline, no response")
|
logger.Warn("read deadline, no response")
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
return strings.Replace(string(messageByte), ":", "", -1)
|
return strings.TrimRight(string(messageByte), ":")
|
||||||
}
|
}
|
||||||
|
|
||||||
func fillString(retunString string, toLength int) string {
|
func fillString(retunString string, toLength int) string {
|
||||||
|
|
30
utils.go
30
utils.go
|
@ -49,38 +49,22 @@ func SplitFile(fileName string, numPieces int) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
bytesPerPiece := int(math.Ceil(float64(fi.Size()) / float64(numPieces)))
|
bytesPerPiece := int(math.Ceil(float64(fi.Size()) / float64(numPieces)))
|
||||||
bytesRead := 0
|
|
||||||
i := 0
|
buf := make([]byte, bytesPerPiece)
|
||||||
|
for i := 0; i < numPieces; i++ {
|
||||||
|
|
||||||
out, err := os.Create(fileName + "." + strconv.Itoa(i))
|
out, err := os.Create(fileName + "." + strconv.Itoa(i))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
buf := make([]byte, 4096)
|
|
||||||
if bytesPerPiece < 4096/numPieces {
|
|
||||||
buf = make([]byte, bytesPerPiece)
|
|
||||||
}
|
|
||||||
for {
|
|
||||||
n, err := file.Read(buf)
|
n, err := file.Read(buf)
|
||||||
out.Write(buf[:n])
|
out.Write(buf[:n])
|
||||||
// If written bytes count is smaller than lenght of buffer
|
out.Close()
|
||||||
// then we don't create one more empty file
|
|
||||||
if err == io.EOF || n < len(buf) {
|
if err == io.EOF {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
bytesRead += n
|
|
||||||
|
|
||||||
if bytesRead >= bytesPerPiece {
|
|
||||||
// Close file and open a new one
|
|
||||||
out.Close()
|
|
||||||
i++
|
|
||||||
out, err = os.Create(fileName + "." + strconv.Itoa(i))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
bytesRead = 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
out.Close()
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue