2016-06-03 03:05:34 +00:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
2019-03-15 22:55:34 +00:00
|
|
|
"context"
|
2016-07-21 04:20:22 +00:00
|
|
|
"io"
|
2019-01-02 22:23:25 +00:00
|
|
|
"mime"
|
2016-06-03 03:05:34 +00:00
|
|
|
"net/http"
|
2020-03-08 22:42:44 +00:00
|
|
|
"path/filepath"
|
2019-01-02 22:23:25 +00:00
|
|
|
"strconv"
|
2016-06-03 03:05:34 +00:00
|
|
|
"strings"
|
|
|
|
|
2018-05-27 18:52:26 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer2"
|
2016-06-03 03:05:34 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2020-03-21 03:31:11 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/images"
|
2020-03-08 01:01:39 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
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
|
|
|
)
|
|
|
|
|
2016-07-21 04:20:22 +00:00
|
|
|
func (fs *FilerServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request, isGetMethod bool) {
|
2019-06-13 09:01:51 +00:00
|
|
|
|
2018-05-14 06:56:16 +00:00
|
|
|
path := r.URL.Path
|
2019-07-10 19:11:18 +00:00
|
|
|
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))
|
2018-05-26 10:49:46 +00:00
|
|
|
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 {
|
2019-10-24 08:51:26 +00:00
|
|
|
glog.V(1).Infof("Not found %s: %v", path, err)
|
|
|
|
stats.FilerRequestCounter.WithLabelValues("read.notfound").Inc()
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
} else {
|
|
|
|
glog.V(0).Infof("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
|
|
|
|
}
|
|
|
|
|
2019-07-10 19:11:18 +00:00
|
|
|
if isForDirectory {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-14 06:56:16 +00:00
|
|
|
if len(entry.Chunks) == 0 {
|
2018-05-26 10:49:46 +00:00
|
|
|
glog.V(1).Infof("no file chunks for %s, attr=%+v", path, entry.Attr)
|
2019-06-15 19:21:44 +00:00
|
|
|
stats.FilerRequestCounter.WithLabelValues("read.nocontent").Inc()
|
2018-05-14 06:56:16 +00:00
|
|
|
w.WriteHeader(http.StatusNoContent)
|
2016-07-21 04:20:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-28 12:39:12 +00:00
|
|
|
w.Header().Set("Accept-Ranges", "bytes")
|
2020-03-08 22:42:44 +00:00
|
|
|
w.Header().Set("Last-Modified", entry.Attr.Mtime.Format(http.TimeFormat))
|
2018-05-28 12:39:12 +00:00
|
|
|
|
2020-03-08 22:42:44 +00:00
|
|
|
// mime type
|
2019-03-27 18:41:11 +00:00
|
|
|
mimeType := entry.Attr.Mime
|
2018-05-31 03:24:57 +00:00
|
|
|
if mimeType == "" {
|
2020-03-08 22:42:44 +00:00
|
|
|
if ext := filepath.Ext(entry.Name()); ext != "" {
|
2018-05-31 03:24:57 +00:00
|
|
|
mimeType = mime.TypeByExtension(ext)
|
|
|
|
}
|
2018-05-28 12:39:12 +00:00
|
|
|
}
|
|
|
|
if mimeType != "" {
|
|
|
|
w.Header().Set("Content-Type", mimeType)
|
|
|
|
}
|
2020-03-08 22:42:44 +00:00
|
|
|
|
|
|
|
// set etag
|
2018-09-09 23:25:43 +00:00
|
|
|
setEtag(w, filer2.ETag(entry.Chunks))
|
2018-05-28 12:39:12 +00:00
|
|
|
|
2020-03-08 22:42:44 +00:00
|
|
|
if r.Method == "HEAD" {
|
|
|
|
w.Header().Set("Content-Length", strconv.FormatInt(int64(filer2.TotalSize(entry.Chunks)), 10))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
filename := entry.Name()
|
|
|
|
adjustHeadersAfterHEAD(w, r, filename)
|
|
|
|
|
2018-05-28 12:39:12 +00:00
|
|
|
totalSize := int64(filer2.TotalSize(entry.Chunks))
|
|
|
|
|
2020-03-21 03:31:11 +00:00
|
|
|
if rangeReq := r.Header.Get("Range"); rangeReq == "" {
|
|
|
|
ext := filepath.Ext(filename)
|
|
|
|
width, height, mode, shouldResize := shouldResizeImages(ext, r)
|
|
|
|
if shouldResize {
|
2020-03-22 08:00:36 +00:00
|
|
|
chunkedFileReader := filer2.NewChunkStreamReaderFromFiler(fs.filer.MasterClient, entry.Chunks)
|
2020-03-21 03:31:11 +00:00
|
|
|
rs, _, _ := images.Resized(ext, chunkedFileReader, width, height, mode)
|
|
|
|
io.Copy(w, rs)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
processRangeRequest(r, w, totalSize, mimeType, func(writer io.Writer, offset int64, size int64) error {
|
2020-03-22 08:37:46 +00:00
|
|
|
return filer2.StreamContent(fs.filer.MasterClient, writer, entry.Chunks, offset, size)
|
2020-03-08 02:06:48 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
}
|