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.

97 lines
2.6 KiB

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/glog"
  9. "github.com/chrislusf/seaweedfs/weed/operation"
  10. "github.com/chrislusf/seaweedfs/weed/util"
  11. "github.com/syndtr/goleveldb/leveldb"
  12. )
  13. // listDirectoryHandler lists directories and folers under a directory
  14. // files are sorted by name and paginated via "lastFileName" and "limit".
  15. // sub directories are listed on the first page, when "lastFileName"
  16. // is empty.
  17. func (fs *FilerServer) listDirectoryHandler(w http.ResponseWriter, r *http.Request) {
  18. if !strings.HasSuffix(r.URL.Path, "/") {
  19. return
  20. }
  21. dirlist, err := fs.filer.ListDirectories(r.URL.Path)
  22. if err == leveldb.ErrNotFound {
  23. glog.V(3).Infoln("Directory Not Found in db", r.URL.Path)
  24. w.WriteHeader(http.StatusNotFound)
  25. return
  26. }
  27. m := make(map[string]interface{})
  28. m["Directory"] = r.URL.Path
  29. lastFileName := r.FormValue("lastFileName")
  30. if lastFileName == "" {
  31. m["Subdirectories"] = dirlist
  32. }
  33. limit, limit_err := strconv.Atoi(r.FormValue("limit"))
  34. if limit_err != nil {
  35. limit = 100
  36. }
  37. m["Files"], _ = fs.filer.ListFiles(r.URL.Path, lastFileName, limit)
  38. writeJsonQuiet(w, r, http.StatusOK, m)
  39. }
  40. func (fs *FilerServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request, isGetMethod bool) {
  41. if strings.HasSuffix(r.URL.Path, "/") {
  42. if fs.disableDirListing {
  43. w.WriteHeader(http.StatusMethodNotAllowed)
  44. return
  45. }
  46. fs.listDirectoryHandler(w, r)
  47. return
  48. }
  49. fileId, err := fs.filer.FindFile(r.URL.Path)
  50. if err == leveldb.ErrNotFound {
  51. glog.V(3).Infoln("Not found in db", r.URL.Path)
  52. w.WriteHeader(http.StatusNotFound)
  53. return
  54. }
  55. urlLocation, err := operation.LookupFileId(fs.getMasterNode(), fileId)
  56. if err != nil {
  57. glog.V(1).Infoln("operation LookupFileId %s failed, err is %s", fileId, err.Error())
  58. w.WriteHeader(http.StatusNotFound)
  59. return
  60. }
  61. urlString := urlLocation
  62. if fs.redirectOnRead {
  63. http.Redirect(w, r, urlString, http.StatusFound)
  64. return
  65. }
  66. u, _ := url.Parse(urlString)
  67. request := &http.Request{
  68. Method: r.Method,
  69. URL: u,
  70. Proto: r.Proto,
  71. ProtoMajor: r.ProtoMajor,
  72. ProtoMinor: r.ProtoMinor,
  73. Header: r.Header,
  74. Body: r.Body,
  75. Host: r.Host,
  76. ContentLength: r.ContentLength,
  77. }
  78. glog.V(3).Infoln("retrieving from", u)
  79. resp, do_err := util.Do(request)
  80. if do_err != nil {
  81. glog.V(0).Infoln("failing to connect to volume server", do_err.Error())
  82. writeJsonError(w, r, http.StatusInternalServerError, do_err)
  83. return
  84. }
  85. defer resp.Body.Close()
  86. for k, v := range resp.Header {
  87. w.Header()[k] = v
  88. }
  89. w.WriteHeader(resp.StatusCode)
  90. io.Copy(w, resp.Body)
  91. }