From c5287695d2d068b47b56e5cec6a5df280955d28f Mon Sep 17 00:00:00 2001 From: Ishan Jain Date: Mon, 11 Sep 2017 18:18:00 +0530 Subject: [PATCH] Added content disposition header support --- pluto/pluto.go | 23 +++++++++++++++++++++-- pluto_cli.go | 5 ++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/pluto/pluto.go b/pluto/pluto.go index 128d088..6a6f0eb 100644 --- a/pluto/pluto.go +++ b/pluto/pluto.go @@ -7,6 +7,7 @@ import ( "net/http" "net/url" "runtime" + "strings" "sync" "time" ) @@ -203,7 +204,7 @@ func FetchMeta(u *url.URL) (*FileMeta, error) { } defer resp.Body.Close() - if resp.StatusCode != 200 && resp.StatusCode != 206 { + if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusPartialContent { return nil, fmt.Errorf("status code is %d", resp.StatusCode) } @@ -218,7 +219,25 @@ func FetchMeta(u *url.URL) (*FileMeta, error) { m = false } - return &FileMeta{Size: uint64(size), u: u, MultipartSupported: m}, nil + resp, err = http.Get(u.String()) + if err != nil { + return nil, fmt.Errorf("error in sending GET request: %v", err) + } + + cDispose := strings.Split(resp.Header.Get("Content-Disposition"), "filename=") + + name := "" + + if len(cDispose) > 0 { + cdfilename := cDispose[1] + cdfilename = cdfilename[1:] + cdfilename = cdfilename[:len(cdfilename)-1] + name = cdfilename + } + + resp.Body.Close() + + return &FileMeta{Size: uint64(size), Name: name, u: u, MultipartSupported: m}, nil } func download(begin, end uint64, u *url.URL) (io.ReadCloser, error) { diff --git a/pluto_cli.go b/pluto_cli.go index d2e4dbe..bf15139 100644 --- a/pluto_cli.go +++ b/pluto_cli.go @@ -92,12 +92,15 @@ func download(u, filename string, parts uint, verbose bool) { return } - meta.Name = filename + if filename != "" { + meta.Name = filename + } if meta.Name == "" { meta.Name = fname } + fmt.Println(meta.Name) saveFile, err := os.Create(meta.Name) if err != nil { errored = true