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.

97 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, shouldDisplayLoadMore, err := fs.filer.ListDirectoryEntries(context.Background(), util.FullPath(path), lastFileName, false, int64(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. if path == "/" {
  38. path = ""
  39. }
  40. if len(entries) > 0 {
  41. lastFileName = entries[len(entries)-1].Name()
  42. }
  43. glog.V(4).Infof("listDirectory %s, last file %s, limit %d: %d items", path, lastFileName, limit, len(entries))
  44. if r.Header.Get("Accept") == "application/json" {
  45. writeJsonQuiet(w, r, http.StatusOK, struct {
  46. Path string
  47. Entries interface{}
  48. Limit int
  49. LastFileName string
  50. ShouldDisplayLoadMore bool
  51. }{
  52. path,
  53. entries,
  54. limit,
  55. lastFileName,
  56. shouldDisplayLoadMore,
  57. })
  58. return
  59. }
  60. var qrImageString string
  61. img, err := qrcode.Encode(fmt.Sprintf("http://%s:%d%s", fs.option.Host, fs.option.Port, r.URL.Path), qrcode.Medium, 128)
  62. if err == nil {
  63. qrImageString = base64.StdEncoding.EncodeToString(img)
  64. }
  65. ui.StatusTpl.Execute(w, struct {
  66. Path string
  67. Breadcrumbs []ui.Breadcrumb
  68. Entries interface{}
  69. Limit int
  70. LastFileName string
  71. ShouldDisplayLoadMore bool
  72. QrImage string
  73. }{
  74. path,
  75. ui.ToBreadcrumb(path),
  76. entries,
  77. limit,
  78. lastFileName,
  79. shouldDisplayLoadMore,
  80. qrImageString,
  81. })
  82. }