filer_server_handlers_read_dir.go 2.8 KB

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