filer_server_handlers_read_dir.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package weed_server
  2. import (
  3. "context"
  4. "net/http"
  5. "strconv"
  6. "strings"
  7. "github.com/seaweedfs/seaweedfs/weed/glog"
  8. ui "github.com/seaweedfs/seaweedfs/weed/server/filer_ui"
  9. "github.com/seaweedfs/seaweedfs/weed/stats"
  10. "github.com/seaweedfs/seaweedfs/weed/util"
  11. )
  12. // listDirectoryHandler lists directories and folers under a directory
  13. // files are sorted by name and paginated via "lastFileName" and "limit".
  14. // sub directories are listed on the first page, when "lastFileName"
  15. // is empty.
  16. func (fs *FilerServer) listDirectoryHandler(w http.ResponseWriter, r *http.Request) {
  17. stats.FilerRequestCounter.WithLabelValues(stats.DirList).Inc()
  18. path := r.URL.Path
  19. if strings.HasSuffix(path, "/") && len(path) > 1 {
  20. path = path[:len(path)-1]
  21. }
  22. limit, limit_err := strconv.Atoi(r.FormValue("limit"))
  23. if limit_err != nil {
  24. limit = fs.option.DirListingLimit
  25. }
  26. lastFileName := r.FormValue("lastFileName")
  27. namePattern := r.FormValue("namePattern")
  28. namePatternExclude := r.FormValue("namePatternExclude")
  29. entries, shouldDisplayLoadMore, err := fs.filer.ListDirectoryEntries(context.Background(), util.FullPath(path), lastFileName, false, int64(limit), "", namePattern, namePatternExclude)
  30. if err != nil {
  31. glog.V(0).Infof("listDirectory %s %s %d: %s", path, lastFileName, limit, err)
  32. w.WriteHeader(http.StatusNotFound)
  33. return
  34. }
  35. if path == "/" {
  36. path = ""
  37. }
  38. emptyFolder := true
  39. if len(entries) > 0 {
  40. lastFileName = entries[len(entries)-1].Name()
  41. emptyFolder = false
  42. }
  43. glog.V(4).Infof("listDirectory %s, last file %s, limit %d: %d items", path, lastFileName, limit, len(entries))
  44. if r.Header.Get("Accept") == "application/json" {
  45. writeJsonQuiet(w, r, http.StatusOK, struct {
  46. Path string
  47. Entries interface{}
  48. Limit int
  49. LastFileName string
  50. ShouldDisplayLoadMore bool
  51. EmptyFolder bool
  52. }{
  53. path,
  54. entries,
  55. limit,
  56. lastFileName,
  57. shouldDisplayLoadMore,
  58. emptyFolder,
  59. })
  60. return
  61. }
  62. err = ui.StatusTpl.Execute(w, struct {
  63. Path string
  64. Breadcrumbs []ui.Breadcrumb
  65. Entries interface{}
  66. Limit int
  67. LastFileName string
  68. ShouldDisplayLoadMore bool
  69. EmptyFolder bool
  70. ShowDirectoryDelete bool
  71. }{
  72. path,
  73. ui.ToBreadcrumb(path),
  74. entries,
  75. limit,
  76. lastFileName,
  77. shouldDisplayLoadMore,
  78. emptyFolder,
  79. fs.option.ShowUIDirectoryDelete,
  80. })
  81. if err != nil {
  82. glog.V(0).Infof("Template Execute Error: %v", err)
  83. }
  84. }