filer_server_handlers_read_dir.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package weed_server
  2. import (
  3. "context"
  4. "errors"
  5. "net/http"
  6. "strconv"
  7. "strings"
  8. "github.com/seaweedfs/seaweedfs/weed/glog"
  9. ui "github.com/seaweedfs/seaweedfs/weed/server/filer_ui"
  10. "github.com/seaweedfs/seaweedfs/weed/stats"
  11. "github.com/seaweedfs/seaweedfs/weed/util"
  12. )
  13. // listDirectoryHandler lists directories and folders 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 fs.option.ExposeDirectoryData == false {
  19. writeJsonError(w, r, http.StatusForbidden, errors.New("ui is disabled"))
  20. return
  21. }
  22. stats.FilerHandlerCounter.WithLabelValues(stats.DirList).Inc()
  23. path := r.URL.Path
  24. if strings.HasSuffix(path, "/") && len(path) > 1 {
  25. path = path[:len(path)-1]
  26. }
  27. limit, limitErr := strconv.Atoi(r.FormValue("limit"))
  28. if limitErr != nil {
  29. limit = fs.option.DirListingLimit
  30. }
  31. lastFileName := r.FormValue("lastFileName")
  32. namePattern := r.FormValue("namePattern")
  33. namePatternExclude := r.FormValue("namePatternExclude")
  34. entries, shouldDisplayLoadMore, err := fs.filer.ListDirectoryEntries(context.Background(), util.FullPath(path), lastFileName, false, int64(limit), "", namePattern, namePatternExclude)
  35. if err != nil {
  36. glog.V(0).Infof("listDirectory %s %s %d: %s", path, lastFileName, limit, err)
  37. w.WriteHeader(http.StatusNotFound)
  38. return
  39. }
  40. if path == "/" {
  41. path = ""
  42. }
  43. emptyFolder := true
  44. if len(entries) > 0 {
  45. lastFileName = entries[len(entries)-1].Name()
  46. emptyFolder = false
  47. }
  48. glog.V(4).Infof("listDirectory %s, last file %s, limit %d: %d items", path, lastFileName, limit, len(entries))
  49. if r.Header.Get("Accept") == "application/json" {
  50. writeJsonQuiet(w, r, http.StatusOK, struct {
  51. Version string
  52. Path string
  53. Entries interface{}
  54. Limit int
  55. LastFileName string
  56. ShouldDisplayLoadMore bool
  57. EmptyFolder bool
  58. }{
  59. util.Version(),
  60. path,
  61. entries,
  62. limit,
  63. lastFileName,
  64. shouldDisplayLoadMore,
  65. emptyFolder,
  66. })
  67. return
  68. }
  69. err = ui.StatusTpl.Execute(w, struct {
  70. Version string
  71. Path string
  72. Breadcrumbs []ui.Breadcrumb
  73. Entries interface{}
  74. Limit int
  75. LastFileName string
  76. ShouldDisplayLoadMore bool
  77. EmptyFolder bool
  78. ShowDirectoryDelete bool
  79. }{
  80. util.Version(),
  81. path,
  82. ui.ToBreadcrumb(path),
  83. entries,
  84. limit,
  85. lastFileName,
  86. shouldDisplayLoadMore,
  87. emptyFolder,
  88. fs.option.ShowUIDirectoryDelete,
  89. })
  90. if err != nil {
  91. glog.V(0).Infof("Template Execute Error: %v", err)
  92. }
  93. }