Skip to content

Commit

Permalink
feat(client): add mimetype for transfer file
Browse files Browse the repository at this point in the history
  • Loading branch information
ikr4-m committed Aug 8, 2024
1 parent fb3b2d6 commit 64e99da
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
13 changes: 12 additions & 1 deletion cmd/mdrop-client/get_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
14 changes: 12 additions & 2 deletions cmd/mdrop-client/send_webserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"
"time"

"github.com/gabriel-vasile/mimetype"
"github.com/mplus-oss/mdrop/internal"
"github.com/schollz/progressbar/v3"
)
Expand All @@ -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])
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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",
Expand All @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand All @@ -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=
Expand Down

0 comments on commit 64e99da

Please sign in to comment.