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.

104 lines
2.6 KiB

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