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.

81 lines
2.0 KiB

  1. package weed_server
  2. import (
  3. "context"
  4. "net/http"
  5. "strconv"
  6. "strings"
  7. "github.com/chrislusf/seaweedfs/weed/filer2"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. ui "github.com/chrislusf/seaweedfs/weed/server/filer_ui"
  10. )
  11. // listDirectoryHandler lists directories and folers under a directory
  12. // files are sorted by name and paginated via "lastFileName" and "limit".
  13. // sub directories are listed on the first page, when "lastFileName"
  14. // is empty.
  15. func (fs *FilerServer) listDirectoryHandler(w http.ResponseWriter, r *http.Request) {
  16. path := r.URL.Path
  17. if strings.HasSuffix(path, "/") && len(path) > 1 {
  18. path = path[:len(path)-1]
  19. }
  20. limit, limit_err := strconv.Atoi(r.FormValue("limit"))
  21. if limit_err != nil {
  22. limit = 100
  23. }
  24. lastFileName := r.FormValue("lastFileName")
  25. entries, err := fs.filer.ListDirectoryEntries(context.Background(), filer2.FullPath(path), lastFileName, false, limit)
  26. if err != nil {
  27. glog.V(0).Infof("listDirectory %s %s %d: %s", path, lastFileName, limit, err)
  28. w.WriteHeader(http.StatusNotFound)
  29. return
  30. }
  31. shouldDisplayLoadMore := len(entries) == limit
  32. if path == "/" {
  33. path = ""
  34. }
  35. if len(entries) > 0 {
  36. lastFileName = entries[len(entries)-1].Name()
  37. }
  38. glog.V(4).Infof("listDirectory %s, last file %s, limit %d: %d items", path, lastFileName, limit, len(entries))
  39. if r.Header.Get("Accept") == "application/json" {
  40. writeJsonQuiet(w, r, http.StatusOK, struct {
  41. Path string
  42. Entries interface{}
  43. Limit int
  44. LastFileName string
  45. ShouldDisplayLoadMore bool
  46. }{
  47. path,
  48. entries,
  49. limit,
  50. lastFileName,
  51. shouldDisplayLoadMore,
  52. })
  53. } else {
  54. ui.StatusTpl.Execute(w, struct {
  55. Path string
  56. Breadcrumbs []ui.Breadcrumb
  57. Entries interface{}
  58. Limit int
  59. LastFileName string
  60. ShouldDisplayLoadMore bool
  61. }{
  62. path,
  63. ui.ToBreadcrumb(path),
  64. entries,
  65. limit,
  66. lastFileName,
  67. shouldDisplayLoadMore,
  68. })
  69. }
  70. }