mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2024-01-19 02:48:24 +00:00
Merge branch 'content_disposition_download'
This commit is contained in:
commit
8329cf86a7
|
@ -52,3 +52,12 @@ func GetBucketAndObject(r *http.Request) (bucket, object string) {
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var PassThroughHeaders = map[string]string{
|
||||||
|
"response-cache-control": "Cache-Control",
|
||||||
|
"response-content-disposition": "Content-Disposition",
|
||||||
|
"response-content-encoding": "Content-Encoding",
|
||||||
|
"response-content-language": "Content-Language",
|
||||||
|
"response-content-type": "Content-Type",
|
||||||
|
"response-expires": "Expires",
|
||||||
|
}
|
||||||
|
|
|
@ -306,15 +306,6 @@ func (s3a *S3ApiServer) doDeleteEmptyDirectories(client filer_pb.SeaweedFilerCli
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var passThroughHeaders = []string{
|
|
||||||
"response-cache-control",
|
|
||||||
"response-content-disposition",
|
|
||||||
"response-content-encoding",
|
|
||||||
"response-content-language",
|
|
||||||
"response-content-type",
|
|
||||||
"response-expires",
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s3a *S3ApiServer) proxyToFiler(w http.ResponseWriter, r *http.Request, destUrl string, responseFn func(proxyResponse *http.Response, w http.ResponseWriter) (statusCode int)) {
|
func (s3a *S3ApiServer) proxyToFiler(w http.ResponseWriter, r *http.Request, destUrl string, responseFn func(proxyResponse *http.Response, w http.ResponseWriter) (statusCode int)) {
|
||||||
|
|
||||||
glog.V(3).Infof("s3 proxying %s to %s", r.Method, destUrl)
|
glog.V(3).Infof("s3 proxying %s to %s", r.Method, destUrl)
|
||||||
|
@ -328,24 +319,13 @@ func (s3a *S3ApiServer) proxyToFiler(w http.ResponseWriter, r *http.Request, des
|
||||||
}
|
}
|
||||||
|
|
||||||
proxyReq.Header.Set("X-Forwarded-For", r.RemoteAddr)
|
proxyReq.Header.Set("X-Forwarded-For", r.RemoteAddr)
|
||||||
|
for k, v := range r.URL.Query() {
|
||||||
|
if _, ok := xhttp.PassThroughHeaders[strings.ToLower(k)]; ok {
|
||||||
|
proxyReq.Header[k] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
for header, values := range r.Header {
|
for header, values := range r.Header {
|
||||||
// handle s3 related headers
|
proxyReq.Header[header] = values
|
||||||
passed := false
|
|
||||||
for _, h := range passThroughHeaders {
|
|
||||||
if strings.ToLower(header) == h && len(values) > 0 {
|
|
||||||
proxyReq.Header.Add(header[len("response-"):], values[0])
|
|
||||||
passed = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if passed {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
// handle other headers
|
|
||||||
for _, value := range values {
|
|
||||||
proxyReq.Header.Add(header, value)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, postErr := client.Do(proxyReq)
|
resp, postErr := client.Do(proxyReq)
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
xhttp "github.com/chrislusf/seaweedfs/weed/s3api/http"
|
||||||
"io"
|
"io"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
|
@ -250,13 +251,16 @@ func handleStaticResources2(r *mux.Router) {
|
||||||
r.PathPrefix("/seaweedfsstatic/").Handler(http.StripPrefix("/seaweedfsstatic", http.FileServer(http.FS(StaticFS))))
|
r.PathPrefix("/seaweedfsstatic/").Handler(http.StripPrefix("/seaweedfsstatic", http.FileServer(http.FS(StaticFS))))
|
||||||
}
|
}
|
||||||
|
|
||||||
func adjustHeaderContentDisposition(w http.ResponseWriter, r *http.Request, filename string) {
|
func adjustPassthroughHeaders(w http.ResponseWriter, r *http.Request, filename string) {
|
||||||
responseContentDisposition := r.FormValue("response-content-disposition")
|
for header, values := range r.Header {
|
||||||
if responseContentDisposition != "" {
|
if normalizedHeader, ok := xhttp.PassThroughHeaders[strings.ToLower(header)]; ok {
|
||||||
w.Header().Set("Content-Disposition", responseContentDisposition)
|
w.Header()[normalizedHeader] = values
|
||||||
return
|
}
|
||||||
}
|
}
|
||||||
if w.Header().Get("Content-Disposition") != "" {
|
adjustHeaderContentDisposition(w, r, filename)
|
||||||
|
}
|
||||||
|
func adjustHeaderContentDisposition(w http.ResponseWriter, r *http.Request, filename string) {
|
||||||
|
if contentDisposition := w.Header().Get("Content-Disposition"); contentDisposition != "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if filename != "" {
|
if filename != "" {
|
||||||
|
|
|
@ -130,7 +130,7 @@ func (fs *FilerServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request)
|
||||||
setEtag(w, etag)
|
setEtag(w, etag)
|
||||||
|
|
||||||
filename := entry.Name()
|
filename := entry.Name()
|
||||||
adjustHeaderContentDisposition(w, r, filename)
|
adjustPassthroughHeaders(w, r, filename)
|
||||||
|
|
||||||
totalSize := int64(entry.Size())
|
totalSize := int64(entry.Size())
|
||||||
|
|
||||||
|
|
|
@ -301,7 +301,7 @@ func writeResponseContent(filename, mimeType string, rs io.ReadSeeker, w http.Re
|
||||||
}
|
}
|
||||||
w.Header().Set("Accept-Ranges", "bytes")
|
w.Header().Set("Accept-Ranges", "bytes")
|
||||||
|
|
||||||
adjustHeaderContentDisposition(w, r, filename)
|
adjustPassthroughHeaders(w, r, filename)
|
||||||
|
|
||||||
if r.Method == "HEAD" {
|
if r.Method == "HEAD" {
|
||||||
w.Header().Set("Content-Length", strconv.FormatInt(totalSize, 10))
|
w.Header().Set("Content-Length", strconv.FormatInt(totalSize, 10))
|
||||||
|
|
Loading…
Reference in a new issue