seaweedfs/weed/server/filer_server_handlers_read.go

173 lines
4.4 KiB
Go
Raw Normal View History

2016-06-03 03:05:34 +00:00
package weed_server
import (
2020-04-28 07:05:47 +00:00
"bytes"
2019-03-15 22:55:34 +00:00
"context"
2016-07-21 04:20:22 +00:00
"io"
"mime"
2016-06-03 03:05:34 +00:00
"net/http"
"net/url"
"path/filepath"
"strconv"
2016-06-03 03:05:34 +00:00
"strings"
"time"
2016-06-03 03:05:34 +00:00
2020-09-01 07:21:19 +00:00
"github.com/chrislusf/seaweedfs/weed/filer"
2016-06-03 03:05:34 +00:00
"github.com/chrislusf/seaweedfs/weed/glog"
"github.com/chrislusf/seaweedfs/weed/images"
2020-03-08 01:01:39 +00:00
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
2020-10-29 08:05:40 +00:00
xhttp "github.com/chrislusf/seaweedfs/weed/s3api/http"
2019-06-15 19:21:44 +00:00
"github.com/chrislusf/seaweedfs/weed/stats"
2020-03-23 07:01:34 +00:00
"github.com/chrislusf/seaweedfs/weed/util"
2016-06-03 03:05:34 +00:00
)
2021-07-19 09:59:12 +00:00
func (fs *FilerServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request) {
2018-05-14 06:56:16 +00:00
path := r.URL.Path
isForDirectory := strings.HasSuffix(path, "/")
if isForDirectory && len(path) > 1 {
2018-05-14 06:56:16 +00:00
path = path[:len(path)-1]
}
2020-03-23 07:01:34 +00:00
entry, err := fs.filer.FindEntry(context.Background(), util.FullPath(path))
if err != nil {
2018-10-12 07:45:28 +00:00
if path == "/" {
fs.listDirectoryHandler(w, r)
return
}
2020-03-08 01:01:39 +00:00
if err == filer_pb.ErrNotFound {
glog.V(1).Infof("Not found %s: %v", path, err)
stats.FilerRequestCounter.WithLabelValues("read.notfound").Inc()
w.WriteHeader(http.StatusNotFound)
} else {
2021-06-24 03:59:54 +00:00
glog.Errorf("Internal %s: %v", path, err)
stats.FilerRequestCounter.WithLabelValues("read.internalerror").Inc()
w.WriteHeader(http.StatusInternalServerError)
}
2018-05-14 06:56:16 +00:00
return
}
if entry.IsDirectory() {
2018-07-07 09:18:47 +00:00
if fs.option.DisableDirListing {
2016-07-21 04:20:22 +00:00
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
fs.listDirectoryHandler(w, r)
return
}
if isForDirectory {
w.WriteHeader(http.StatusNotFound)
return
}
2021-05-24 11:59:44 +00:00
// set etag
etag := filer.ETagEntry(entry)
if ifm := r.Header.Get("If-Match"); ifm != "" && ifm != "\""+etag+"\"" {
w.WriteHeader(http.StatusPreconditionFailed)
return
}
w.Header().Set("Accept-Ranges", "bytes")
// mime type
mimeType := entry.Attr.Mime
if mimeType == "" {
if ext := filepath.Ext(entry.Name()); ext != "" {
mimeType = mime.TypeByExtension(ext)
}
}
if mimeType != "" {
w.Header().Set("Content-Type", mimeType)
}
// if modified since
if !entry.Attr.Mtime.IsZero() {
w.Header().Set("Last-Modified", entry.Attr.Mtime.UTC().Format(http.TimeFormat))
if r.Header.Get("If-Modified-Since") != "" {
if t, parseError := time.Parse(http.TimeFormat, r.Header.Get("If-Modified-Since")); parseError == nil {
if !t.Before(entry.Attr.Mtime) {
w.WriteHeader(http.StatusNotModified)
return
}
}
}
}
// print out the header from extended properties
for k, v := range entry.Extended {
w.Header().Set(k, string(v))
}
//Seaweed custom header are not visible to Vue or javascript
seaweedHeaders := []string{}
for header, _ := range w.Header() {
if strings.HasPrefix(header, "Seaweed-") {
seaweedHeaders = append(seaweedHeaders, header)
}
}
seaweedHeaders = append(seaweedHeaders, "Content-Disposition")
w.Header().Set("Access-Control-Expose-Headers", strings.Join(seaweedHeaders, ","))
//set tag count
if r.Method == "GET" {
tagCount := 0
for k := range entry.Extended {
2020-10-29 08:05:40 +00:00
if strings.HasPrefix(k, xhttp.AmzObjectTagging+"-") {
tagCount++
}
}
if tagCount > 0 {
2020-10-29 08:05:40 +00:00
w.Header().Set(xhttp.AmzTagCount, strconv.Itoa(tagCount))
}
}
if inm := r.Header.Get("If-None-Match"); inm == "\""+etag+"\"" {
w.WriteHeader(http.StatusNotModified)
return
}
setEtag(w, etag)
filename := entry.Name()
filename = url.QueryEscape(filename)
2020-07-26 03:09:19 +00:00
adjustHeaderContentDisposition(w, r, filename)
totalSize := int64(entry.Size())
if r.Method == "HEAD" {
w.Header().Set("Content-Length", strconv.FormatInt(totalSize, 10))
return
}
if rangeReq := r.Header.Get("Range"); rangeReq == "" {
ext := filepath.Ext(filename)
width, height, mode, shouldResize := shouldResizeImages(ext, r)
if shouldResize {
2020-09-01 07:21:19 +00:00
data, err := filer.ReadAll(fs.filer.MasterClient, entry.Chunks)
2020-04-28 07:05:47 +00:00
if err != nil {
glog.Errorf("failed to read %s: %v", path, err)
w.WriteHeader(http.StatusNotModified)
return
}
rs, _, _ := images.Resized(ext, bytes.NewReader(data), width, height, mode)
io.Copy(w, rs)
return
}
}
processRangeRequest(r, w, totalSize, mimeType, func(writer io.Writer, offset int64, size int64) error {
2020-11-30 12:34:04 +00:00
if offset+size <= int64(len(entry.Content)) {
_, err := writer.Write(entry.Content[offset : offset+size])
2021-03-11 01:13:01 +00:00
if err != nil {
glog.Errorf("failed to write entry content: %v", err)
}
2020-11-30 12:34:04 +00:00
return err
}
err = filer.StreamContent(fs.filer.MasterClient, writer, entry.Chunks, offset, size)
if err != nil {
glog.Errorf("failed to stream content %s: %v", r.URL, err)
}
return err
2020-03-08 02:06:48 +00:00
})
}