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.

98 lines
2.4 KiB

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