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.

108 lines
2.8 KiB

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