filer_server_handlers_read.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package weed_server
  2. import (
  3. "io"
  4. "net/http"
  5. "net/url"
  6. "strconv"
  7. "strings"
  8. "github.com/chrislusf/seaweedfs/weed/filer"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/operation"
  11. ui "github.com/chrislusf/seaweedfs/weed/server/filer_ui"
  12. "github.com/chrislusf/seaweedfs/weed/util"
  13. "github.com/syndtr/goleveldb/leveldb"
  14. )
  15. // listDirectoryHandler lists directories and folers under a directory
  16. // files are sorted by name and paginated via "lastFileName" and "limit".
  17. // sub directories are listed on the first page, when "lastFileName"
  18. // is empty.
  19. func (fs *FilerServer) listDirectoryHandler(w http.ResponseWriter, r *http.Request) {
  20. if !strings.HasSuffix(r.URL.Path, "/") {
  21. return
  22. }
  23. limit, limit_err := strconv.Atoi(r.FormValue("limit"))
  24. if limit_err != nil {
  25. limit = 100
  26. }
  27. lastFileName := r.FormValue("lastFileName")
  28. files, err := fs.filer.ListFiles(r.URL.Path, lastFileName, limit)
  29. if err == leveldb.ErrNotFound {
  30. glog.V(0).Infof("Error %s", err)
  31. w.WriteHeader(http.StatusNotFound)
  32. return
  33. }
  34. directories, err2 := fs.filer.ListDirectories(r.URL.Path)
  35. if err2 == leveldb.ErrNotFound {
  36. glog.V(0).Infof("Error %s", err)
  37. w.WriteHeader(http.StatusNotFound)
  38. return
  39. }
  40. shouldDisplayLoadMore := len(files) > 0
  41. lastFileName = ""
  42. if len(files) > 0 {
  43. lastFileName = files[len(files)-1].Name
  44. files2, err3 := fs.filer.ListFiles(r.URL.Path, lastFileName, limit)
  45. if err3 == leveldb.ErrNotFound {
  46. glog.V(0).Infof("Error %s", err)
  47. w.WriteHeader(http.StatusNotFound)
  48. return
  49. }
  50. shouldDisplayLoadMore = len(files2) > 0
  51. }
  52. args := struct {
  53. Path string
  54. Files interface{}
  55. Directories interface{}
  56. Limit int
  57. LastFileName string
  58. ShouldDisplayLoadMore bool
  59. }{
  60. r.URL.Path,
  61. files,
  62. directories,
  63. limit,
  64. lastFileName,
  65. shouldDisplayLoadMore,
  66. }
  67. if r.Header.Get("Accept") == "application/json" {
  68. writeJsonQuiet(w, r, http.StatusOK, args)
  69. } else {
  70. ui.StatusTpl.Execute(w, args)
  71. }
  72. }
  73. func (fs *FilerServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request, isGetMethod bool) {
  74. if strings.HasSuffix(r.URL.Path, "/") {
  75. if fs.disableDirListing {
  76. w.WriteHeader(http.StatusMethodNotAllowed)
  77. return
  78. }
  79. fs.listDirectoryHandler(w, r)
  80. return
  81. }
  82. fileId, err := fs.filer.FindFile(r.URL.Path)
  83. if err == filer.ErrNotFound {
  84. glog.V(3).Infoln("Not found in db", r.URL.Path)
  85. w.WriteHeader(http.StatusNotFound)
  86. return
  87. }
  88. urlLocation, err := operation.LookupFileId(fs.getMasterNode(), fileId)
  89. if err != nil {
  90. glog.V(1).Infof("operation LookupFileId %s failed, err:%v", fileId, err)
  91. w.WriteHeader(http.StatusNotFound)
  92. return
  93. }
  94. urlString := urlLocation
  95. if fs.redirectOnRead {
  96. http.Redirect(w, r, urlString, http.StatusFound)
  97. return
  98. }
  99. u, _ := url.Parse(urlString)
  100. q := u.Query()
  101. for key, values := range r.URL.Query() {
  102. for _, value := range values {
  103. q.Add(key, value)
  104. }
  105. }
  106. u.RawQuery = q.Encode()
  107. request := &http.Request{
  108. Method: r.Method,
  109. URL: u,
  110. Proto: r.Proto,
  111. ProtoMajor: r.ProtoMajor,
  112. ProtoMinor: r.ProtoMinor,
  113. Header: r.Header,
  114. Body: r.Body,
  115. Host: r.Host,
  116. ContentLength: r.ContentLength,
  117. }
  118. glog.V(3).Infoln("retrieving from", u)
  119. resp, do_err := util.Do(request)
  120. if do_err != nil {
  121. glog.V(0).Infoln("failing to connect to volume server", do_err.Error())
  122. writeJsonError(w, r, http.StatusInternalServerError, do_err)
  123. return
  124. }
  125. defer resp.Body.Close()
  126. for k, v := range resp.Header {
  127. w.Header()[k] = v
  128. }
  129. w.WriteHeader(resp.StatusCode)
  130. io.Copy(w, resp.Body)
  131. }