From e4d26432d4ef366c9e4b969d4fdb8d4728b545b3 Mon Sep 17 00:00:00 2001 From: Slack Coder Date: Thu, 14 Sep 2023 11:31:31 -0500 Subject: Support files with spaces Lean on net/url/URL to properly format filenames when a single file is being hosted. --- go.mod | 8 +++++++- go.sum | 8 ++++++++ send_over_http.go | 21 +++++++++++++-------- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index 0de3a6d..bbc50f0 100644 --- a/go.mod +++ b/go.mod @@ -4,4 +4,10 @@ go 1.17 require github.com/mdp/qrterminal/v3 v3.0.0 -require rsc.io/qr v0.2.0 // indirect +require ( + github.com/yuin/goldmark v1.4.13 // indirect + golang.org/x/mod v0.12.0 // indirect + golang.org/x/sys v0.12.0 // indirect + golang.org/x/tools v0.13.0 // indirect + rsc.io/qr v0.2.0 // indirect +) diff --git a/go.sum b/go.sum index e5a936f..4f99edd 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,14 @@ github.com/mdp/qrterminal v1.0.1 h1:07+fzVDlPuBlXS8tB0ktTAyf+Lp1j2+2zK3fBOL5b7c= github.com/mdp/qrterminal v1.0.1/go.mod h1:Z33WhxQe9B6CdW37HaVqcRKzP+kByF3q/qLxOGe12xQ= github.com/mdp/qrterminal/v3 v3.0.0 h1:ywQqLRBXWTktytQNDKFjhAvoGkLVN3J2tAFZ0kMd9xQ= github.com/mdp/qrterminal/v3 v3.0.0/go.mod h1:NJpfAs7OAm77Dy8EkWrtE4aq+cE6McoLXlBqXQEwvE0= +github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc= +golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= +golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ= +golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= rsc.io/qr v0.2.0 h1:6vBLea5/NRMVTz8V66gipeLycZMl/+UlFmk8DvqQ6WY= rsc.io/qr v0.2.0/go.mod h1:IF+uZjkb9fqyeF/4tlBoynqmQxUoPfWEKh921coOuXs= diff --git a/send_over_http.go b/send_over_http.go index c34145b..98b7e6e 100644 --- a/send_over_http.go +++ b/send_over_http.go @@ -6,10 +6,15 @@ import ( "log" "net" "net/http" + "net/url" "os" "path/filepath" + "strconv" ) +// port is the IP port address to listen on. +const port = 8081 + func preferredIP() (*net.IP, error) { ips, err := net.InterfaceAddrs() if err != nil { @@ -76,20 +81,20 @@ func (s *Server) Start() error { return err } - prot := "http" - port := "8081" - - url := fmt.Sprintf("%s://%s:%s", prot, ip, port) + u := url.URL{ + Scheme: "http", + Host: fmt.Sprintf("%s:%d", ip, port), + } if stat, _ := os.Stat(s.filepath); !stat.IsDir() { - url += "/" + filepath.Base(s.filepath) + u.Path = filepath.Base(s.filepath) } - qrCodeShower := NewTerminalQRShower(url) + qrCodeShower := NewTerminalQRShower(u.String()) go s.mr.Run(qrCodeShower) httpRunner := NewRunner() httpSrv := &http.Server{ - Addr: ":" + port, + Addr: ":" + strconv.Itoa(port), } var d http.FileSystem = http.Dir(s.filepath) @@ -100,7 +105,7 @@ func (s *Server) Start() error { httpSrv.Handler = http.FileServer(d) httpRunner.OnStart = func() error { - l, err := net.Listen("tcp4", ip.String()+":"+port) + l, err := net.Listen("tcp4", u.Host) if err != nil { log.Fatal(err) } -- cgit v1.2.3