working commit
This commit is contained in:
+90
-9
@@ -16,6 +16,7 @@ import (
|
||||
"net/http"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -287,7 +288,7 @@ func (oper *Operator) DeleteFile(ctx context.Context, operID string, param *Dele
|
||||
// ListFiles
|
||||
type ListFilesParams struct {
|
||||
Filepath string
|
||||
PathAs terms.PathAs `param:"pathAs"`
|
||||
PathAs terms.PathUsage `param:"pathAs"`
|
||||
}
|
||||
type ListFilesResult struct {
|
||||
Files []descr.File `json:"files,omitempty"`
|
||||
@@ -298,13 +299,20 @@ func (oper *Operator) ListFiles(ctx context.Context, operID string, params *List
|
||||
res := &ListFilesResult{
|
||||
Files: make([]descr.File, 0),
|
||||
}
|
||||
|
||||
//oper.logg.Debugf("FileList path: %s %s", params.Filepath, params.PathAs)
|
||||
params.Filepath, err = cleanFilepath(params.Filepath)
|
||||
if err != nil {
|
||||
code := http.StatusInternalServerError
|
||||
return code, res, err
|
||||
}
|
||||
switch params.PathAs {
|
||||
case terms.AsRegexp:
|
||||
files, err := oper.listFilesWithRegex(ctx, params.Filepath)
|
||||
if err != nil {
|
||||
code := http.StatusInternalServerError
|
||||
return code, res, err
|
||||
}
|
||||
res.Files = files
|
||||
case terms.AsPrefix:
|
||||
files, err := oper.listFilesWithPrefix(ctx, params.Filepath)
|
||||
if err != nil {
|
||||
@@ -312,7 +320,6 @@ func (oper *Operator) ListFiles(ctx context.Context, operID string, params *List
|
||||
return code, res, err
|
||||
}
|
||||
res.Files = files
|
||||
|
||||
default: // Fine
|
||||
files, err := oper.listFilesInOneCollection(ctx, params.Filepath)
|
||||
if err != nil {
|
||||
@@ -353,10 +360,31 @@ func (oper *Operator) listFilesWithPrefix(ctx context.Context, prefix string) ([
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (oper *Operator) listFilesWithRegex(ctx context.Context, regex string) ([]descr.File, error) {
|
||||
var err error
|
||||
res := make([]descr.File, 0)
|
||||
|
||||
re, err := regexp.Compile(regex)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
files, err := oper.mdb.ListAllFiles(ctx)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
for _, file := range files {
|
||||
fullpath := filepath.Join(file.Collection, file.Name)
|
||||
if re.MatchString(fullpath) {
|
||||
res = append(res, file)
|
||||
}
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
|
||||
// ListCollections
|
||||
type ListCollectionsParams struct {
|
||||
Path string
|
||||
PathAS terms.PathAs `param:"pathAs"`
|
||||
PathAS terms.PathUsage `param:"pathAs"`
|
||||
}
|
||||
type ListCollectionsResult struct {
|
||||
Collections []string `json:"collection,omitempty"`
|
||||
@@ -374,6 +402,12 @@ func (oper *Operator) ListCollections(ctx context.Context, operID string, param
|
||||
}
|
||||
collectionList := make([]string, 0)
|
||||
switch param.PathAS {
|
||||
case terms.AsRegexp:
|
||||
collectionList, err = oper.listCollectionsWithRegexp(ctx, param.Path)
|
||||
if err != nil {
|
||||
code := http.StatusInternalServerError
|
||||
return code, res, err
|
||||
}
|
||||
case terms.AsPrefix:
|
||||
collectionList, err = oper.listCollectionsWithPrefix(ctx, param.Path)
|
||||
if err != nil {
|
||||
@@ -408,7 +442,7 @@ func (oper *Operator) listCollectionsWithPrefix(ctx context.Context, prefix stri
|
||||
collMap[item.Collection] = true
|
||||
}
|
||||
}
|
||||
res = make([]string, len(collMap))
|
||||
res = make([]string, 0)
|
||||
for key, _ := range collMap {
|
||||
if strings.HasPrefix(key, prefix) {
|
||||
res = append(res, key)
|
||||
@@ -418,6 +452,36 @@ func (oper *Operator) listCollectionsWithPrefix(ctx context.Context, prefix stri
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (oper *Operator) listCollectionsWithRegexp(ctx context.Context, regex string) ([]string, error) {
|
||||
var err error
|
||||
res := make([]string, 0)
|
||||
|
||||
re, err := regexp.Compile(regex)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
fileDescrs, err := oper.mdb.ListAllFiles(ctx) // TODO
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
collMap := make(map[string]bool)
|
||||
for _, item := range fileDescrs {
|
||||
_, exists := collMap[item.Collection]
|
||||
if !exists {
|
||||
collMap[item.Collection] = true
|
||||
}
|
||||
}
|
||||
res = make([]string, 0)
|
||||
for key, _ := range collMap {
|
||||
if re.MatchString(key) {
|
||||
res = append(res, key)
|
||||
}
|
||||
}
|
||||
slices.Sort(res)
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (oper *Operator) listAllCollections(ctx context.Context) ([]string, error) {
|
||||
var err error
|
||||
res := make([]string, 0)
|
||||
@@ -433,7 +497,7 @@ func (oper *Operator) listAllCollections(ctx context.Context) ([]string, error)
|
||||
collMap[item.Collection] = true
|
||||
}
|
||||
}
|
||||
res = make([]string, len(collMap))
|
||||
res = make([]string, 0)
|
||||
for key, _ := range collMap {
|
||||
res = append(res, key)
|
||||
}
|
||||
@@ -444,8 +508,8 @@ func (oper *Operator) listAllCollections(ctx context.Context) ([]string, error)
|
||||
// DeleteColletion
|
||||
type DeleteColletionParams struct {
|
||||
Path string
|
||||
PathAs string `param:"pathAs"`
|
||||
DryRun bool `param:"dryRun"`
|
||||
PathAs terms.PathUsage `param:"pathAs"`
|
||||
DryRun bool `param:"dryRun"`
|
||||
}
|
||||
type DeleteColletionResult struct {
|
||||
Files []descr.File `json:"files,omitempty"`
|
||||
@@ -462,7 +526,24 @@ func (oper *Operator) DeleteColletion(ctx context.Context, operID string, param
|
||||
return code, res, err
|
||||
}
|
||||
//oper.logg.Debugf("DeleteCollection: Use path as %s", param.PathAs)
|
||||
switch terms.PathAs(param.PathAs) {
|
||||
switch param.PathAs {
|
||||
case terms.AsRegexp:
|
||||
collections, err := oper.listCollectionsWithRegexp(ctx, param.Path)
|
||||
if err != nil {
|
||||
code := http.StatusInternalServerError
|
||||
return code, res, err
|
||||
}
|
||||
allfiles := make([]descr.File, 0)
|
||||
for _, collection := range collections {
|
||||
files, err := oper.deleteFilesInCollection(ctx, collection, param.DryRun)
|
||||
if err != nil {
|
||||
code := http.StatusInternalServerError
|
||||
return code, res, err
|
||||
}
|
||||
allfiles = append(allfiles, files...)
|
||||
}
|
||||
res.Files = allfiles
|
||||
|
||||
case terms.AsPrefix:
|
||||
collections, err := oper.listCollectionsWithPrefix(ctx, param.Path)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user