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.

87 lines
2.2 KiB

5 years ago
  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. }