Skip to content

Commit

Permalink
Add missing implementation for GET /api/v3/cluster/fs/{storage}
Browse files Browse the repository at this point in the history
  • Loading branch information
ioppermann committed Nov 27, 2023
1 parent a817cd3 commit 2c47ab7
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 45 deletions.
2 changes: 1 addition & 1 deletion app/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion cluster/proxy/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cluster/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
66 changes: 24 additions & 42 deletions http/handler/api/cluster_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -20,59 +17,44 @@ 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
// @Success 500 {object} api.Error
// @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 }
}
}

if len(modifiedEnd) != 0 {
if x, err := strconv.ParseInt(modifiedEnd, 10, 64); err != nil {
return api.Err(http.StatusBadRequest, "", "lastmode_end: %s", err.Error())
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 {
t := time.Unix(x+1, 0)
options.ModifiedEnd = &t
sortFunc = func(i, j int) bool { return files[i].LastMod > files[j].LastMod }
}
}

return api.Err(http.StatusNotImplemented, "", "not implemented")
sort.Slice(files, sortFunc)

return c.JSON(http.StatusOK, files)
}

0 comments on commit 2c47ab7

Please sign in to comment.