filer_server_handlers_read_dir.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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, limit_err := strconv.Atoi(r.FormValue("limit"))
  28. if limit_err != 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. Path string
  52. Entries interface{}
  53. Limit int
  54. LastFileName string
  55. ShouldDisplayLoadMore bool
  56. EmptyFolder bool
  57. }{
  58. path,
  59. entries,
  60. limit,
  61. lastFileName,
  62. shouldDisplayLoadMore,
  63. emptyFolder,
  64. })
  65. return
  66. }
  67. err = ui.StatusTpl.Execute(w, struct {
  68. Path string
  69. Breadcrumbs []ui.Breadcrumb
  70. Entries interface{}
  71. Limit int
  72. LastFileName string
  73. ShouldDisplayLoadMore bool
  74. EmptyFolder bool
  75. ShowDirectoryDelete bool
  76. }{
  77. path,
  78. ui.ToBreadcrumb(path),
  79. entries,
  80. limit,
  81. lastFileName,
  82. shouldDisplayLoadMore,
  83. emptyFolder,
  84. fs.option.ShowUIDirectoryDelete,
  85. })
  86. if err != nil {
  87. glog.V(0).Infof("Template Execute Error: %v", err)
  88. }
  89. }