2018-05-28 02:24:14 -07:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
2024-03-18 13:08:16 -05:00
|
|
|
"errors"
|
2025-06-03 22:46:07 -07:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util/version"
|
2018-05-28 02:24:14 -07:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2022-07-29 00:17:28 -07:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
ui "github.com/seaweedfs/seaweedfs/weed/server/filer_ui"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/stats"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
2018-05-28 02:24:14 -07:00
|
|
|
)
|
|
|
|
|
2024-03-18 13:08:16 -05:00
|
|
|
// listDirectoryHandler lists directories and folders under a directory
|
2018-05-28 02:24:14 -07:00
|
|
|
// files are sorted by name and paginated via "lastFileName" and "limit".
|
|
|
|
// sub directories are listed on the first page, when "lastFileName"
|
|
|
|
// is empty.
|
|
|
|
func (fs *FilerServer) listDirectoryHandler(w http.ResponseWriter, r *http.Request) {
|
2025-05-28 21:34:02 +03:00
|
|
|
ctx := r.Context()
|
2024-02-27 10:38:55 -06:00
|
|
|
if fs.option.ExposeDirectoryData == false {
|
2024-03-18 13:08:16 -05:00
|
|
|
writeJsonError(w, r, http.StatusForbidden, errors.New("ui is disabled"))
|
2024-02-27 10:38:55 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-10 23:05:27 +05:00
|
|
|
stats.FilerHandlerCounter.WithLabelValues(stats.DirList).Inc()
|
2019-06-22 22:53:52 -07:00
|
|
|
|
2018-05-28 02:24:14 -07:00
|
|
|
path := r.URL.Path
|
|
|
|
if strings.HasSuffix(path, "/") && len(path) > 1 {
|
|
|
|
path = path[:len(path)-1]
|
|
|
|
}
|
|
|
|
|
2024-06-23 11:47:26 -07:00
|
|
|
limit, limitErr := strconv.Atoi(r.FormValue("limit"))
|
|
|
|
if limitErr != nil {
|
2023-02-28 08:31:26 -08:00
|
|
|
limit = fs.option.DirListingLimit
|
2018-05-28 02:24:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
lastFileName := r.FormValue("lastFileName")
|
2020-12-26 15:05:31 -08:00
|
|
|
namePattern := r.FormValue("namePattern")
|
2021-04-24 11:49:03 -07:00
|
|
|
namePatternExclude := r.FormValue("namePatternExclude")
|
2018-05-28 02:24:14 -07:00
|
|
|
|
2025-05-28 21:34:02 +03:00
|
|
|
entries, shouldDisplayLoadMore, err := fs.filer.ListDirectoryEntries(ctx, util.FullPath(path), lastFileName, false, int64(limit), "", namePattern, namePatternExclude)
|
2018-05-28 02:24:14 -07:00
|
|
|
|
|
|
|
if err != nil {
|
2025-06-24 18:44:06 +03:00
|
|
|
glog.V(0).InfofCtx(ctx, "listDirectory %s %s %d: %s", path, lastFileName, limit, err)
|
2018-05-28 02:24:14 -07:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if path == "/" {
|
|
|
|
path = ""
|
|
|
|
}
|
|
|
|
|
2022-04-07 15:11:05 +08:00
|
|
|
emptyFolder := true
|
2018-05-28 02:24:14 -07:00
|
|
|
if len(entries) > 0 {
|
|
|
|
lastFileName = entries[len(entries)-1].Name()
|
2022-04-07 15:11:05 +08:00
|
|
|
emptyFolder = false
|
2018-05-28 02:24:14 -07:00
|
|
|
}
|
|
|
|
|
2025-06-24 18:44:06 +03:00
|
|
|
glog.V(4).InfofCtx(ctx, "listDirectory %s, last file %s, limit %d: %d items", path, lastFileName, limit, len(entries))
|
2018-05-28 02:24:14 -07:00
|
|
|
|
|
|
|
if r.Header.Get("Accept") == "application/json" {
|
2018-06-04 19:28:59 -07:00
|
|
|
writeJsonQuiet(w, r, http.StatusOK, struct {
|
2024-07-30 00:07:12 +03:00
|
|
|
Version string
|
2018-06-04 19:28:59 -07:00
|
|
|
Path string
|
|
|
|
Entries interface{}
|
|
|
|
Limit int
|
|
|
|
LastFileName string
|
|
|
|
ShouldDisplayLoadMore bool
|
2022-04-07 15:11:05 +08:00
|
|
|
EmptyFolder bool
|
2018-06-04 19:28:59 -07:00
|
|
|
}{
|
2025-06-03 22:46:07 -07:00
|
|
|
version.Version(),
|
2018-06-04 19:28:59 -07:00
|
|
|
path,
|
|
|
|
entries,
|
|
|
|
limit,
|
|
|
|
lastFileName,
|
|
|
|
shouldDisplayLoadMore,
|
2022-04-07 15:11:05 +08:00
|
|
|
emptyFolder,
|
2018-06-04 19:28:59 -07:00
|
|
|
})
|
2020-10-10 18:00:21 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-15 00:30:49 +08:00
|
|
|
err = ui.StatusTpl.Execute(w, struct {
|
2024-07-30 00:07:12 +03:00
|
|
|
Version string
|
2020-10-10 18:00:21 -07:00
|
|
|
Path string
|
|
|
|
Breadcrumbs []ui.Breadcrumb
|
|
|
|
Entries interface{}
|
|
|
|
Limit int
|
|
|
|
LastFileName string
|
|
|
|
ShouldDisplayLoadMore bool
|
2022-04-07 15:11:05 +08:00
|
|
|
EmptyFolder bool
|
2022-06-15 00:30:49 +08:00
|
|
|
ShowDirectoryDelete bool
|
2020-10-10 18:00:21 -07:00
|
|
|
}{
|
2025-06-03 22:46:07 -07:00
|
|
|
version.Version(),
|
2020-10-10 18:00:21 -07:00
|
|
|
path,
|
|
|
|
ui.ToBreadcrumb(path),
|
|
|
|
entries,
|
|
|
|
limit,
|
|
|
|
lastFileName,
|
|
|
|
shouldDisplayLoadMore,
|
2022-04-07 15:11:05 +08:00
|
|
|
emptyFolder,
|
2022-06-15 00:30:49 +08:00
|
|
|
fs.option.ShowUIDirectoryDelete,
|
2020-10-10 18:00:21 -07:00
|
|
|
})
|
2022-06-15 00:30:49 +08:00
|
|
|
if err != nil {
|
2025-06-24 18:44:06 +03:00
|
|
|
glog.V(0).InfofCtx(ctx, "Template Execute Error: %v", err)
|
2022-06-15 00:30:49 +08:00
|
|
|
}
|
2024-02-27 10:38:55 -06:00
|
|
|
|
2018-05-28 02:24:14 -07:00
|
|
|
}
|