working commit
This commit is contained in:
+12
-7
@@ -17,6 +17,7 @@ import (
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"mstore/app/descr"
|
||||
"mstore/pkg/auxtool"
|
||||
@@ -281,19 +282,17 @@ type ListFilesParams struct {
|
||||
Filepath string
|
||||
}
|
||||
type ListFilesResult struct {
|
||||
Files []descr.File `json:"files,omitempty"`
|
||||
//Catalogs []string `json:"files,omitempty"`
|
||||
Files []descr.File `json:"files,omitempty"`
|
||||
Collections []string `json:"collection,omitempty"`
|
||||
}
|
||||
|
||||
func (oper *Operator) ListFiles(ctx context.Context, operID string, param *ListFilesParams) (int, *ListFilesResult, error) {
|
||||
var err error
|
||||
res := &ListFilesResult{
|
||||
Files: make([]descr.File, 0),
|
||||
//Catalogs: make([]string, 0)
|
||||
Files: make([]descr.File, 0),
|
||||
Collections: make([]string, 0),
|
||||
}
|
||||
|
||||
// TODO: convert file path to a unified and secure state
|
||||
|
||||
_, err = cleanFilepath(param.Filepath)
|
||||
if err != nil {
|
||||
code := http.StatusInternalServerError
|
||||
@@ -305,8 +304,14 @@ func (oper *Operator) ListFiles(ctx context.Context, operID string, param *ListF
|
||||
code := http.StatusInternalServerError
|
||||
return code, res, err
|
||||
}
|
||||
rFilepath := filepath.Join("/", param.Filepath)
|
||||
for _, item := range fileDescrs {
|
||||
res.Files = append(res.Files, item)
|
||||
cFilepath := filepath.Join("/", item.Collection, item.Name)
|
||||
collection := filepath.Join("/", item.Collection)
|
||||
if strings.HasPrefix(cFilepath, rFilepath) {
|
||||
res.Files = append(res.Files, item)
|
||||
res.Collections = append(res.Collections, collection)
|
||||
}
|
||||
}
|
||||
code := http.StatusOK
|
||||
return code, res, err
|
||||
|
||||
+12
-1
@@ -15,6 +15,7 @@ import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type Context struct {
|
||||
@@ -123,18 +124,28 @@ func (rctx *Context) SetStatus(httpStatus int) {
|
||||
}
|
||||
|
||||
func (rctx *Context) SendJSON(statusCode int, payload any) {
|
||||
|
||||
buffer := bytes.NewBuffer(nil)
|
||||
json.NewEncoder(buffer).Encode(payload)
|
||||
rctx.Writer.Header().Set("Content-Type", "application/json")
|
||||
size := strconv.FormatInt(int64(len(buffer.Bytes())), 10)
|
||||
rctx.Writer.Header().Set("Content-Length", size)
|
||||
rctx.Writer.WriteHeader(statusCode)
|
||||
json.NewEncoder(rctx.Writer).Encode(payload)
|
||||
rctx.Writer.Write(buffer.Bytes())
|
||||
}
|
||||
|
||||
func (rctx *Context) SendText(statusCode int, payload string) {
|
||||
size := strconv.FormatInt(int64(len(payload)), 10)
|
||||
rctx.Writer.Header().Set("Content-Type", "text/plain")
|
||||
rctx.Writer.Header().Set("Content-Length", size)
|
||||
rctx.Writer.WriteHeader(statusCode)
|
||||
rctx.Writer.Write([]byte(payload))
|
||||
}
|
||||
|
||||
func (rctx *Context) SendBytes(statusCode int, payload []byte) {
|
||||
size := strconv.FormatInt(int64(len(payload)), 10)
|
||||
rctx.Writer.Header().Set("Content-Type", "application/octet-stream")
|
||||
rctx.Writer.Header().Set("Content-Length", size)
|
||||
rctx.Writer.WriteHeader(statusCode)
|
||||
rctx.Writer.Write(payload)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user