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.

146 lines
3.6 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  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).Infoln("operation LookupFileId %s failed, err is %s", fileId, err.Error())
  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. }