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.

80 lines
1.9 KiB

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