filer_server_handlers_read.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package weed_server
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "io"
  7. "mime"
  8. "net/http"
  9. "path/filepath"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "github.com/chrislusf/seaweedfs/weed/filer"
  14. "github.com/chrislusf/seaweedfs/weed/glog"
  15. "github.com/chrislusf/seaweedfs/weed/images"
  16. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  17. xhttp "github.com/chrislusf/seaweedfs/weed/s3api/http"
  18. "github.com/chrislusf/seaweedfs/weed/stats"
  19. "github.com/chrislusf/seaweedfs/weed/util"
  20. )
  21. func (fs *FilerServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request) {
  22. path := r.URL.Path
  23. isForDirectory := strings.HasSuffix(path, "/")
  24. if isForDirectory && len(path) > 1 {
  25. path = path[:len(path)-1]
  26. }
  27. entry, err := fs.filer.FindEntry(context.Background(), util.FullPath(path))
  28. if err != nil {
  29. if path == "/" {
  30. fs.listDirectoryHandler(w, r)
  31. return
  32. }
  33. if err == filer_pb.ErrNotFound {
  34. glog.V(1).Infof("Not found %s: %v", path, err)
  35. stats.FilerRequestCounter.WithLabelValues("read.notfound").Inc()
  36. w.WriteHeader(http.StatusNotFound)
  37. } else {
  38. glog.Errorf("Internal %s: %v", path, err)
  39. stats.FilerRequestCounter.WithLabelValues("read.internalerror").Inc()
  40. w.WriteHeader(http.StatusInternalServerError)
  41. }
  42. return
  43. }
  44. if entry.IsDirectory() {
  45. if fs.option.DisableDirListing {
  46. w.WriteHeader(http.StatusMethodNotAllowed)
  47. return
  48. }
  49. fs.listDirectoryHandler(w, r)
  50. return
  51. }
  52. if isForDirectory {
  53. w.WriteHeader(http.StatusNotFound)
  54. return
  55. }
  56. // set etag
  57. etag := filer.ETagEntry(entry)
  58. if ifm := r.Header.Get("If-Match"); ifm != "" && (ifm != "\""+etag+"\"" && ifm != etag) {
  59. w.WriteHeader(http.StatusPreconditionFailed)
  60. return
  61. }
  62. w.Header().Set("Accept-Ranges", "bytes")
  63. // mime type
  64. mimeType := entry.Attr.Mime
  65. if mimeType == "" {
  66. if ext := filepath.Ext(entry.Name()); ext != "" {
  67. mimeType = mime.TypeByExtension(ext)
  68. }
  69. }
  70. if mimeType != "" {
  71. w.Header().Set("Content-Type", mimeType)
  72. }
  73. // if modified since
  74. if !entry.Attr.Mtime.IsZero() {
  75. w.Header().Set("Last-Modified", entry.Attr.Mtime.UTC().Format(http.TimeFormat))
  76. if r.Header.Get("If-Modified-Since") != "" {
  77. if t, parseError := time.Parse(http.TimeFormat, r.Header.Get("If-Modified-Since")); parseError == nil {
  78. if !t.Before(entry.Attr.Mtime) {
  79. w.WriteHeader(http.StatusNotModified)
  80. return
  81. }
  82. }
  83. }
  84. }
  85. // print out the header from extended properties
  86. for k, v := range entry.Extended {
  87. if !strings.HasPrefix(k, "xattr-") {
  88. // "xattr-" prefix is set in filesys.XATTR_PREFIX
  89. w.Header().Set(k, string(v))
  90. }
  91. }
  92. //Seaweed custom header are not visible to Vue or javascript
  93. seaweedHeaders := []string{}
  94. for header := range w.Header() {
  95. if strings.HasPrefix(header, "Seaweed-") {
  96. seaweedHeaders = append(seaweedHeaders, header)
  97. }
  98. }
  99. seaweedHeaders = append(seaweedHeaders, "Content-Disposition")
  100. w.Header().Set("Access-Control-Expose-Headers", strings.Join(seaweedHeaders, ","))
  101. //set tag count
  102. tagCount := 0
  103. for k := range entry.Extended {
  104. if strings.HasPrefix(k, xhttp.AmzObjectTagging+"-") {
  105. tagCount++
  106. }
  107. }
  108. if tagCount > 0 {
  109. w.Header().Set(xhttp.AmzTagCount, strconv.Itoa(tagCount))
  110. }
  111. if inm := r.Header.Get("If-None-Match"); inm == "\""+etag+"\"" {
  112. w.WriteHeader(http.StatusNotModified)
  113. return
  114. }
  115. setEtag(w, etag)
  116. filename := entry.Name()
  117. adjustPassthroughHeaders(w, r, filename)
  118. totalSize := int64(entry.Size())
  119. if r.Method == "HEAD" {
  120. w.Header().Set("Content-Length", strconv.FormatInt(totalSize, 10))
  121. return
  122. }
  123. if rangeReq := r.Header.Get("Range"); rangeReq == "" {
  124. ext := filepath.Ext(filename)
  125. if len(ext) > 0 {
  126. ext = strings.ToLower(ext)
  127. }
  128. width, height, mode, shouldResize := shouldResizeImages(ext, r)
  129. if shouldResize {
  130. data, err := filer.ReadAll(fs.filer.MasterClient, entry.Chunks)
  131. if err != nil {
  132. glog.Errorf("failed to read %s: %v", path, err)
  133. w.WriteHeader(http.StatusNotModified)
  134. return
  135. }
  136. rs, _, _ := images.Resized(ext, bytes.NewReader(data), width, height, mode)
  137. io.Copy(w, rs)
  138. return
  139. }
  140. }
  141. processRangeRequest(r, w, totalSize, mimeType, func(writer io.Writer, offset int64, size int64) error {
  142. if offset+size <= int64(len(entry.Content)) {
  143. _, err := writer.Write(entry.Content[offset : offset+size])
  144. if err != nil {
  145. glog.Errorf("failed to write entry content: %v", err)
  146. }
  147. return err
  148. }
  149. chunks := entry.Chunks
  150. if entry.IsInRemoteOnly() {
  151. dir, name := entry.FullPath.DirAndName()
  152. if resp, err := fs.CacheRemoteObjectToLocalCluster(context.Background(), &filer_pb.CacheRemoteObjectToLocalClusterRequest{
  153. Directory: dir,
  154. Name: name,
  155. }); err != nil {
  156. glog.Errorf("CacheRemoteObjectToLocalCluster %s: %v", entry.FullPath, err)
  157. return fmt.Errorf("cache %s: %v", entry.FullPath, err)
  158. } else {
  159. chunks = resp.Entry.Chunks
  160. }
  161. }
  162. err = filer.StreamContent(fs.filer.MasterClient, writer, chunks, offset, size)
  163. if err != nil {
  164. glog.Errorf("failed to stream content %s: %v", r.URL, err)
  165. }
  166. return err
  167. })
  168. }