filer_server_handlers_read_dir.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package weed_server
  2. import (
  3. "context"
  4. "net/http"
  5. "strconv"
  6. "strings"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. ui "github.com/chrislusf/seaweedfs/weed/server/filer_ui"
  9. "github.com/chrislusf/seaweedfs/weed/stats"
  10. "github.com/chrislusf/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("list").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 = 100
  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. if len(entries) > 0 {
  39. lastFileName = entries[len(entries)-1].Name()
  40. }
  41. glog.V(4).Infof("listDirectory %s, last file %s, limit %d: %d items", path, lastFileName, limit, len(entries))
  42. if r.Header.Get("Accept") == "application/json" {
  43. writeJsonQuiet(w, r, http.StatusOK, struct {
  44. Path string
  45. Entries interface{}
  46. Limit int
  47. LastFileName string
  48. ShouldDisplayLoadMore bool
  49. }{
  50. path,
  51. entries,
  52. limit,
  53. lastFileName,
  54. shouldDisplayLoadMore,
  55. })
  56. return
  57. }
  58. ui.StatusTpl.Execute(w, struct {
  59. Path string
  60. Breadcrumbs []ui.Breadcrumb
  61. Entries interface{}
  62. Limit int
  63. LastFileName string
  64. ShouldDisplayLoadMore bool
  65. }{
  66. path,
  67. ui.ToBreadcrumb(path),
  68. entries,
  69. limit,
  70. lastFileName,
  71. shouldDisplayLoadMore,
  72. })
  73. }