Add missing implementation for GET /api/v3/cluster/fs/{storage}

This commit is contained in:
Ingo Oppermann
2023-11-27 12:51:14 +01:00
parent a817cd37b4
commit 2c47ab77d2
4 changed files with 37 additions and 46 deletions

View File

@@ -30,7 +30,7 @@ func (v versionInfo) MinorString() string {
var Version = versionInfo{
Major: 16,
Minor: 18,
Patch: 1,
Patch: 2,
}
// Commit is the git commit the app is build from. It should be filled in during compilation

View File

@@ -738,7 +738,16 @@ func (n *node) FileList(storage, pattern string) ([]clientapi.FileInfo, error) {
return nil, ErrNoPeer
}
return n.peer.FilesystemList(storage, pattern, "", "")
files, err := n.peer.FilesystemList(storage, pattern, "", "")
if err != nil {
return nil, err
}
for i := range files {
files[i].CoreID = n.id
}
return files, nil
}
func cloneURL(src *url.URL) *url.URL {

View File

@@ -44,7 +44,7 @@ type ProxyReader interface {
ProbeProcess(nodeid string, id app.ProcessID) (clientapi.Probe, error)
ProbeProcessConfig(nodeid string, config *app.Config) (clientapi.Probe, error)
ListFiles(storage, patter string) []clientapi.FileInfo
ListFiles(storage, pattern string) []clientapi.FileInfo
GetURL(prefix, path string) (*url.URL, error)
GetFile(prefix, path string, offset int64) (io.ReadCloser, error)

View File

@@ -2,12 +2,9 @@ package api
import (
"net/http"
"strconv"
"time"
"sort"
"github.com/datarhei/core/v16/http/api"
"github.com/datarhei/core/v16/http/handler/util"
"github.com/datarhei/core/v16/io/fs"
"github.com/labstack/echo/v4"
)
@@ -20,10 +17,6 @@ import (
// @Produce json
// @Param storage path string true "Name of the filesystem"
// @Param glob query string false "glob pattern for file names"
// @Param size_min query int64 false "minimal size of files"
// @Param size_max query int64 false "maximal size of files"
// @Param lastmod_start query int64 false "minimal last modification time"
// @Param lastmod_end query int64 false "maximal last modification time"
// @Param sort query string false "none, name, size, lastmod"
// @Param order query string false "asc, desc"
// @Success 200 {array} api.FileInfo
@@ -31,48 +24,37 @@ import (
// @Security ApiKeyAuth
// @Router /api/v3/cluster/fs/{storage} [get]
func (h *ClusterHandler) ListFiles(c echo.Context) error {
//name := util.PathParam(c, "storage")
name := util.PathParam(c, "storage")
pattern := util.DefaultQuery(c, "glob", "")
sizeMin := util.DefaultQuery(c, "size_min", "0")
sizeMax := util.DefaultQuery(c, "size_max", "0")
modifiedStart := util.DefaultQuery(c, "lastmod_start", "")
modifiedEnd := util.DefaultQuery(c, "lastmod_end", "")
//sortby := util.DefaultQuery(c, "sort", "none")
//order := util.DefaultQuery(c, "order", "asc")
sortby := util.DefaultQuery(c, "sort", "none")
order := util.DefaultQuery(c, "order", "asc")
options := fs.ListOptions{
Pattern: pattern,
}
files := h.proxy.ListFiles(name, pattern)
if x, err := strconv.ParseInt(sizeMin, 10, 64); err != nil {
return api.Err(http.StatusBadRequest, "", "size_min: %s", err.Error())
} else {
options.SizeMin = x
}
var sortFunc func(i, j int) bool
if x, err := strconv.ParseInt(sizeMax, 10, 64); err != nil {
return api.Err(http.StatusBadRequest, "", "size_max: %s", err.Error())
} else {
options.SizeMax = x
}
if len(modifiedStart) != 0 {
if x, err := strconv.ParseInt(modifiedStart, 10, 64); err != nil {
return api.Err(http.StatusBadRequest, "", "lastmod_start: %s", err.Error())
switch sortby {
case "name":
if order == "desc" {
sortFunc = func(i, j int) bool { return files[i].Name > files[j].Name }
} else {
t := time.Unix(x, 0)
options.ModifiedStart = &t
sortFunc = func(i, j int) bool { return files[i].Name < files[j].Name }
}
case "size":
if order == "desc" {
sortFunc = func(i, j int) bool { return files[i].Size > files[j].Size }
} else {
sortFunc = func(i, j int) bool { return files[i].Size < files[j].Size }
}
default:
if order == "asc" {
sortFunc = func(i, j int) bool { return files[i].LastMod < files[j].LastMod }
} else {
sortFunc = func(i, j int) bool { return files[i].LastMod > files[j].LastMod }
}
}
if len(modifiedEnd) != 0 {
if x, err := strconv.ParseInt(modifiedEnd, 10, 64); err != nil {
return api.Err(http.StatusBadRequest, "", "lastmode_end: %s", err.Error())
} else {
t := time.Unix(x+1, 0)
options.ModifiedEnd = &t
}
}
sort.Slice(files, sortFunc)
return api.Err(http.StatusNotImplemented, "", "not implemented")
return c.JSON(http.StatusOK, files)
}