You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
2.8 KiB

9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
6 years ago
5 years ago
9 years ago
5 years ago
6 years ago
5 years ago
  1. package weed_server
  2. import (
  3. "context"
  4. "io"
  5. "mime"
  6. "net/http"
  7. "path/filepath"
  8. "strconv"
  9. "strings"
  10. "github.com/chrislusf/seaweedfs/weed/filer2"
  11. "github.com/chrislusf/seaweedfs/weed/glog"
  12. "github.com/chrislusf/seaweedfs/weed/images"
  13. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  14. "github.com/chrislusf/seaweedfs/weed/stats"
  15. "github.com/chrislusf/seaweedfs/weed/util"
  16. )
  17. func (fs *FilerServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request, isGetMethod bool) {
  18. path := r.URL.Path
  19. isForDirectory := strings.HasSuffix(path, "/")
  20. if isForDirectory && len(path) > 1 {
  21. path = path[:len(path)-1]
  22. }
  23. entry, err := fs.filer.FindEntry(context.Background(), util.FullPath(path))
  24. if err != nil {
  25. if path == "/" {
  26. fs.listDirectoryHandler(w, r)
  27. return
  28. }
  29. if err == filer_pb.ErrNotFound {
  30. glog.V(1).Infof("Not found %s: %v", path, err)
  31. stats.FilerRequestCounter.WithLabelValues("read.notfound").Inc()
  32. w.WriteHeader(http.StatusNotFound)
  33. } else {
  34. glog.V(0).Infof("Internal %s: %v", path, err)
  35. stats.FilerRequestCounter.WithLabelValues("read.internalerror").Inc()
  36. w.WriteHeader(http.StatusInternalServerError)
  37. }
  38. return
  39. }
  40. if entry.IsDirectory() {
  41. if fs.option.DisableDirListing {
  42. w.WriteHeader(http.StatusMethodNotAllowed)
  43. return
  44. }
  45. fs.listDirectoryHandler(w, r)
  46. return
  47. }
  48. if isForDirectory {
  49. w.WriteHeader(http.StatusNotFound)
  50. return
  51. }
  52. if len(entry.Chunks) == 0 {
  53. glog.V(1).Infof("no file chunks for %s, attr=%+v", path, entry.Attr)
  54. stats.FilerRequestCounter.WithLabelValues("read.nocontent").Inc()
  55. w.WriteHeader(http.StatusNoContent)
  56. return
  57. }
  58. w.Header().Set("Accept-Ranges", "bytes")
  59. w.Header().Set("Last-Modified", entry.Attr.Mtime.Format(http.TimeFormat))
  60. // mime type
  61. mimeType := entry.Attr.Mime
  62. if mimeType == "" {
  63. if ext := filepath.Ext(entry.Name()); ext != "" {
  64. mimeType = mime.TypeByExtension(ext)
  65. }
  66. }
  67. if mimeType != "" {
  68. w.Header().Set("Content-Type", mimeType)
  69. }
  70. // set etag
  71. setEtag(w, filer2.ETag(entry.Chunks))
  72. if r.Method == "HEAD" {
  73. w.Header().Set("Content-Length", strconv.FormatInt(int64(filer2.TotalSize(entry.Chunks)), 10))
  74. return
  75. }
  76. filename := entry.Name()
  77. adjustHeadersAfterHEAD(w, r, filename)
  78. totalSize := int64(filer2.TotalSize(entry.Chunks))
  79. if rangeReq := r.Header.Get("Range"); rangeReq == "" {
  80. ext := filepath.Ext(filename)
  81. width, height, mode, shouldResize := shouldResizeImages(ext, r)
  82. if shouldResize {
  83. chunkedFileReader := filer2.NewChunkStreamReaderFromFiler(fs.filer.MasterClient, entry.Chunks)
  84. rs, _, _ := images.Resized(ext, chunkedFileReader, width, height, mode)
  85. io.Copy(w, rs)
  86. return
  87. }
  88. }
  89. processRangeRequest(r, w, totalSize, mimeType, func(writer io.Writer, offset int64, size int64) error {
  90. return filer2.StreamContent(fs.filer.MasterClient, writer, entry.Chunks, offset, size)
  91. })
  92. }