diff --git a/cmd/mdrop-client/get_download.go b/cmd/mdrop-client/get_download.go index 2919af6..a175ba0 100644 --- a/cmd/mdrop-client/get_download.go +++ b/cmd/mdrop-client/get_download.go @@ -60,7 +60,18 @@ func GetDownload(localPort int, fileNameOpt string, uuid string) string { if fileNameOpt != "" { fileName = fileNameOpt } - fmt.Println("File found:", fileName) + fmt.Println("File found:", fileName, fmt.Sprintf("[%v]", resp.Header.Get("X-Mime-Type"))) + + // Ask client if they wanna download it or not + fmt.Print("Download? [(Y)es/(N)o] [Default: Y] -> ") + prompt, err := reader.ReadString('\n') + if err != nil { + internal.PrintErrorWithExit("sendPromptError", err, 1) + } + prompt = strings.Replace(prompt, "\n", "", -1) + if strings.ToLower(prompt) == "n" { + internal.PrintErrorWithExit("sendPromptCancel", errors.New("Canceled by action"), 0) + } // Check if there's duplicate file filePath, err := os.Getwd() diff --git a/cmd/mdrop-client/send_webserver.go b/cmd/mdrop-client/send_webserver.go index cf44397..80ec434 100644 --- a/cmd/mdrop-client/send_webserver.go +++ b/cmd/mdrop-client/send_webserver.go @@ -12,6 +12,7 @@ import ( "strings" "time" + "github.com/gabriel-vasile/mimetype" "github.com/mplus-oss/mdrop/internal" "github.com/schollz/progressbar/v3" ) @@ -28,8 +29,14 @@ func SendWebserver(localPort int, file []string, uuid []string) (err error) { server.Addr = ":"+strconv.Itoa(localPort) for i, _ := range file { + // Check mimetype + mimeType, err := mimetype.DetectFile(file[i]) + if err != nil { + senderErrorChan <- internal.CustomizeError("receiveMimeType", err) + } + http.Handle("/"+uuid[i], http.HandlerFunc(func (w http.ResponseWriter, request *http.Request) { - receiveSendWebserver(w, request, file[i]) + receiveSendWebserver(w, request, file[i], mimeType.String()) })) http.Handle("/checksum-"+uuid[i], http.HandlerFunc(func (w http.ResponseWriter, request *http.Request) { checksumSendWebserver(w, request, file[i]) @@ -79,7 +86,7 @@ func checksumSendWebserver(w http.ResponseWriter, request *http.Request, filePat request.Close = true } -func receiveSendWebserver(w http.ResponseWriter, request *http.Request, filePath string) { +func receiveSendWebserver(w http.ResponseWriter, request *http.Request, filePath string, mimeType string) { if request.Method != "POST" { w.WriteHeader(http.StatusMethodNotAllowed) return @@ -91,6 +98,7 @@ func receiveSendWebserver(w http.ResponseWriter, request *http.Request, filePath // This prevent from MITM after transfering file isStillUsed = true + // File open file, err := os.Open(filePath) if err != nil { senderErrorChan <- internal.CustomizeError("receiveOpenFile", err) @@ -102,6 +110,7 @@ func receiveSendWebserver(w http.ResponseWriter, request *http.Request, filePath senderErrorChan <- internal.CustomizeError("receiveOpenFileStat", err) } + w.Header().Set("Transfer-Encoding", "identity") w.Header().Set( "Content-Length", @@ -113,6 +122,7 @@ func receiveSendWebserver(w http.ResponseWriter, request *http.Request, filePath fmt.Sprintf("attachment; filename=\"%v\"", fileInfo.Name()), ) w.Header().Set("X-Attachment-Name", fileInfo.Name()) + w.Header().Set("X-Mime-Type", mimeType) bar := progressbar.DefaultBytes(fileInfo.Size(), fileInfo.Name()) _, err = io.Copy(io.MultiWriter(bar, w), file) diff --git a/go.mod b/go.mod index c1f9bcc..5e32a47 100644 --- a/go.mod +++ b/go.mod @@ -2,13 +2,17 @@ module github.com/mplus-oss/mdrop go 1.22.5 -require github.com/schollz/progressbar/v3 v3.14.4 +require ( + github.com/gabriel-vasile/mimetype v1.4.5 + github.com/google/uuid v1.6.0 + github.com/schollz/progressbar/v3 v3.14.4 +) require ( - github.com/google/uuid v1.6.0 // indirect github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect github.com/rivo/uniseg v0.4.7 // indirect github.com/stretchr/testify v1.8.4 // indirect + golang.org/x/net v0.27.0 // indirect golang.org/x/sys v0.22.0 // indirect golang.org/x/term v0.22.0 // indirect ) diff --git a/go.sum b/go.sum index 7064f7f..bee3d0e 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gabriel-vasile/mimetype v1.4.5 h1:J7wGKdGu33ocBOhGy0z653k/lFKLFDPJMG8Gql0kxn4= +github.com/gabriel-vasile/mimetype v1.4.5/go.mod h1:ibHel+/kbxn9x2407k1izTA1S81ku1z/DlgOW2QE0M4= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw= @@ -17,6 +19,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys= +golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=