@@ -5,13 +5,10 @@ package host
55import (
66 "encoding/binary"
77 "encoding/json"
8- "errors"
98 "fmt"
10- "io"
119 "net"
1210 "os"
1311 "path/filepath"
14- "strings"
1512 "time"
1613
1714 "github.com/AmirMirzayi/relay/config"
@@ -20,8 +17,8 @@ import (
2017
2118// Serve starts a file transfer server that listens on the specified IP and port.
2219// It serves files located at the provided paths to connected clients.
23- func Serve (ip net.IP , port , progressbarWidth int , timeout time.Duration , silentTransfer bool , pathes ... string ) error {
24- files , err := getFilesByPathes ( pathes ... )
20+ func Serve (ip net.IP , port , progressbarWidth int , timeout time.Duration , silentTransfer bool , paths ... string ) error {
21+ files , err := getFilesByPaths ( paths ... )
2522 if err != nil {
2623 return err
2724 }
@@ -53,12 +50,12 @@ func Serve(ip net.IP, port, progressbarWidth int, timeout time.Duration, silentT
5350 return nil
5451}
5552
56- func getFilesByPathes ( pathes ... string ) (config.Files , error ) {
53+ func getFilesByPaths ( paths ... string ) (config.Files , error ) {
5754 // preallocate files to args lengths
5855 // but, what if an arg was directory
59- files := make (config.Files , 0 , len (pathes ))
56+ files := make (config.Files , 0 , len (paths ))
6057
61- for _ , path := range pathes {
58+ for _ , path := range paths {
6259 fileInfo , err := os .Stat (path )
6360 if err != nil {
6461 return nil , fmt .Errorf ("failed to read information of %s, %v" , path , err )
@@ -108,33 +105,17 @@ func sendFile(conn net.Conn, file config.File, fileID, progressbarWidth int, sil
108105 }
109106 defer f .Close ()
110107
111- shortedFileName := utils .ShortedString (file .Name , 10 , 7 , 4 )
112-
113- var totalByteSent int64
114- buffer := make ([]byte , config .DefaultChunkSize )
115- for {
116- n , err := f .Read (buffer )
117- if err != nil {
118- if errors .Is (err , io .EOF ) {
119- break
120- }
121- return fmt .Errorf ("failed to buffering file %s, %v" , file .Path , err )
122- }
123-
124- byteSent , err := conn .Write (buffer [:n ])
125- if err != nil {
126- return fmt .Errorf ("failed to write over network, %v" , err )
127- }
128-
129- if ! silentTransfer {
130- totalByteSent += int64 (byteSent )
131- sentPercent := int (totalByteSent * 100 / file .Size )
132- utils .DrawProgressBar (sentPercent , progressbarWidth , shortedFileName )
133- }
108+ if silentTransfer {
109+ return utils .WriteFromReader (f , conn , file .Size , config .DefaultBufferSize )
134110 }
135- if ! silentTransfer {
136- fmt .Printf ("\r [%d] %s ✓%s\n " , fileID , file .Name , strings .Repeat (" " , progressbarWidth ))
111+
112+ shortedFileName := utils .ShortedString (file .Name , 10 , 8 , 3 )
113+ fileSize := utils .ConvertByteSizeToHumanReadable (float64 (file .Size ))
114+ barTitle := fmt .Sprintf ("<%s ^ %s>" , fileSize , shortedFileName )
115+ if err = utils .DrawRWProgressbar (f , conn , file .Size , config .DefaultBufferSize , progressbarWidth , barTitle ); err != nil {
116+ return err
137117 }
118+ fmt .Printf ("\r [%d] %s ✓\033 [K\n " , fileID , file .Name )
138119 return nil
139120}
140121
@@ -147,7 +128,7 @@ func readDirectoryFilesRecursively(path string, parents ...string) (config.Files
147128
148129 var files []config.File
149130
150- // we need to sepereate files and directories iteration over entries because of confusation
131+ // we need to separate files and directories iteration over entries because of confusing
151132 for _ , entry := range entries {
152133 if entry .IsDir () {
153134 continue
0 commit comments