Fix content-disposition parsing
ci/woodpecker/push/woodpecker Pipeline was successful Details

This commit is contained in:
Elara 2023-10-07 15:07:44 -07:00
parent e20ed6b5eb
commit 0c6cdadd82
1 changed files with 9 additions and 10 deletions

View File

@ -22,12 +22,12 @@ import (
"bytes" "bytes"
"context" "context"
"io" "io"
"mime"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"regexp"
"strings" "strings"
"time" "time"
@ -247,19 +247,18 @@ func extractFile(r io.Reader, format archiver.Format, name string, opts Options)
return nil return nil
} }
var cdHeaderRgx = regexp.MustCompile(`filename="?(.+)"?`)
// getFilename attempts to parse the Content-Disposition // getFilename attempts to parse the Content-Disposition
// HTTP response header and extract a filename. If the // HTTP response header and extract a filename. If the
// header does not exist, it will use the last element // header does not exist, it will use the last element
// of the path. // of the path.
func getFilename(res *http.Response) (name string) { func getFilename(res *http.Response) (name string) {
cd := res.Header.Get("Content-Disposition") _, params, err := mime.ParseMediaType(res.Header.Get("Content-Disposition"))
matches := cdHeaderRgx.FindStringSubmatch(cd) if err != nil {
if len(matches) > 1 { return path.Base(res.Request.URL.Path)
name = matches[1] }
} else { if filename, ok := params["filename"]; ok {
name = path.Base(res.Request.URL.Path) return filename
} else {
return path.Base(res.Request.URL.Path)
} }
return name
} }