filer_server_handlers_read_dir.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. entries, err := fs.filer.ListDirectoryEntries(context.Background(), util.FullPath(path), lastFileName, false, limit)
  28. if err != nil {
  29. glog.V(0).Infof("listDirectory %s %s %d: %s", path, lastFileName, limit, err)
  30. w.WriteHeader(http.StatusNotFound)
  31. return
  32. }
  33. shouldDisplayLoadMore := len(entries) == limit
  34. if path == "/" {
  35. path = ""
  36. }
  37. if len(entries) > 0 {
  38. lastFileName = entries[len(entries)-1].Name()
  39. }
  40. glog.V(4).Infof("listDirectory %s, last file %s, limit %d: %d items", path, lastFileName, limit, len(entries))
  41. if r.Header.Get("Accept") == "application/json" {
  42. writeJsonQuiet(w, r, http.StatusOK, struct {
  43. Path string
  44. Entries interface{}
  45. Limit int
  46. LastFileName string
  47. ShouldDisplayLoadMore bool
  48. }{
  49. path,
  50. entries,
  51. limit,
  52. lastFileName,
  53. shouldDisplayLoadMore,
  54. })
  55. } else {
  56. ui.StatusTpl.Execute(w, struct {
  57. Path string
  58. Breadcrumbs []ui.Breadcrumb
  59. Entries interface{}
  60. Limit int
  61. LastFileName string
  62. ShouldDisplayLoadMore bool
  63. }{
  64. path,
  65. ui.ToBreadcrumb(path),
  66. entries,
  67. limit,
  68. lastFileName,
  69. shouldDisplayLoadMore,
  70. })
  71. }
  72. }