Skip to content
This repository has been archived by the owner on Jan 20, 2023. It is now read-only.

Commit

Permalink
Refactor a bit the code to make clearer
Browse files Browse the repository at this point in the history
after @amisevsk comment:
#60 (comment)

Signed-off-by: David Festal <dfestal@redhat.com>
  • Loading branch information
davidfestal committed May 20, 2019
1 parent f2b1b8d commit 103113f
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions utils/ioutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,28 +69,37 @@ func (util *impl) Download(URL string, destPath string, useContentDisposition bo
return "", NewHTTPError(resp, fmt.Sprintf("Downloading %s failed. Status code %v", URL, resp.StatusCode))
}

filePath, filename := filepath.Split(destPath)

if useContentDisposition {
filePath, filename := filepath.Split(destPath)
fromHeader := func () (found bool, filename string) {
dispo := resp.Header.Get("Content-Disposition")
if dispo == "" {
return
}

if dispo := resp.Header.Get("Content-Disposition"); dispo != "" {
contentDispoFilename := ""
if _, params, err := mime.ParseMediaType(dispo); err == nil {
contentDispoFilename = params["filename"]
_, params, err := mime.ParseMediaType(dispo);
if err != nil {
return
}

contentDispoFilename := params["filename"]
if strings.HasSuffix(contentDispoFilename, "/") || strings.Contains(contentDispoFilename, "\x00") {
contentDispoFilename = ""
return
}
contentDispoFilename = path.Base(path.Clean("/" + contentDispoFilename))
if contentDispoFilename == "." || contentDispoFilename == "/" {
contentDispoFilename = ""
}
if contentDispoFilename != "" {
filename = contentDispoFilename
return
}
return true, contentDispoFilename
}

destPath = filepath.Join(filePath, filename)
if found, contentDispoFilename := fromHeader(); found {
filename = contentDispoFilename
}
}

destPath = filepath.Join(filePath, filename)

out, err := os.Create(destPath)
if err != nil {
Expand Down

0 comments on commit 103113f

Please sign in to comment.