~howeyc/fetchp

52f36701c5afc0bd72f55cec6a6c19005646d06f — Chris Howey 1 year, 1 month ago 1e456b0 v0.1.2
attempt to get filename from header
1 files changed, 10 insertions(+), 3 deletions(-)

M main.go
M main.go => main.go +10 -3
@@ 5,6 5,7 @@ import (
	"fmt"
	"io"
	"log"
	"mime"
	"net/http"
	"net/url"
	"os"


@@ 40,15 41,21 @@ func main() {

	u, _ := url.Parse(reqURL)
	fname := filepath.Base(u.Path)
	if opath != "" {
		fname = opath
	}

	resp, err := http.Get(reqURL)
	if err != nil {
		log.Fatalln(err)
	}

	// Get from header
	if disposition, params, err := mime.ParseMediaType(resp.Header.Get("Content-Disposition")); err == nil && disposition == "attachment" {
		fname = params["filename"]
	}

	if opath != "" {
		fname = opath
	}

	ofile, oerr := os.Create(fname)
	if oerr != nil {
		log.Fatalln(oerr)