working commit

This commit is contained in:
2026-02-18 23:22:46 +02:00
parent e1ca9b1b4c
commit 4a43a22c19
12 changed files with 500 additions and 92 deletions
+90 -7
View File
@@ -16,6 +16,7 @@ import (
"net/http"
"path"
"path/filepath"
"slices"
"strconv"
"strings"
@@ -282,18 +283,16 @@ type ListFilesParams struct {
Filepath string
}
type ListFilesResult struct {
Files []descr.File `json:"files,omitempty"`
Collections []string `json:"collection,omitempty"`
Files []descr.File `json:"files,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),
Collections: make([]string, 0),
Files: make([]descr.File, 0),
}
_, err = cleanFilepath(param.Filepath)
param.Filepath, err = cleanFilepath(param.Filepath)
if err != nil {
code := http.StatusInternalServerError
return code, res, err
@@ -307,12 +306,96 @@ func (oper *Operator) ListFiles(ctx context.Context, operID string, param *ListF
rFilepath := filepath.Join("/", param.Filepath)
for _, item := range fileDescrs {
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
}
// ListCollections
type ListCollectionsParams struct {
Path string
}
type ListCollectionsResult struct {
Collections []string `json:"collection,omitempty"`
}
func (oper *Operator) ListCollections(ctx context.Context, operID string, param *ListCollectionsParams) (int, *ListCollectionsResult, error) {
var err error
res := &ListCollectionsResult{
Collections: make([]string, 0),
}
param.Path, err = cleanFilepath(param.Path)
if err != nil {
code := http.StatusInternalServerError
return code, res, err
}
fileDescrs, err := oper.mdb.ListAllFiles(ctx)
if err != nil {
code := http.StatusInternalServerError
return code, res, err
}
cmap := make(map[string]bool)
for _, item := range fileDescrs {
cPath := filepath.Join("/", item.Collection)
pattern := filepath.Join("/", param.Path)
if strings.HasPrefix(cPath, pattern) {
_, exists := cmap[cPath]
if !exists {
cmap[cPath] = true
}
}
}
for key, _ := range cmap {
res.Collections = append(res.Collections, key)
}
slices.Sort(res.Collections)
code := http.StatusOK
return code, res, err
}
// DeleteColletion
type DeleteColletionParams struct {
Path string
}
type DeleteColletionResult struct {
Files []descr.File `json:"collection,omitempty"`
}
func (oper *Operator) DeleteColletion(ctx context.Context, operID string, param *DeleteColletionParams) (int, *DeleteColletionResult, error) {
var err error
res := &DeleteColletionResult{
Files: make([]descr.File, 0),
}
param.Path, err = cleanFilepath(param.Path)
if err != nil {
code := http.StatusInternalServerError
return code, res, err
}
oper.logg.Debugf("=== %s", param.Path)
fileDescrs, err := oper.mdb.ListFilesByCollection(ctx, param.Path)
if err != nil {
code := http.StatusInternalServerError
return code, res, err
}
// TODO: transaction
for _, file := range fileDescrs {
err = oper.store.DeleteFile(file.Collection, file.Name)
if err != nil {
oper.logg.Warningf("%v", err)
err = nil
//code := http.StatusInternalServerError
//return code, res, err
}
err = oper.mdb.DeleteFileByCollectionName(ctx, file.Collection, file.Name)
if err != nil {
code := http.StatusInternalServerError
return code, res, err
}
res.Files = append(res.Files, file)
}
code := http.StatusOK
return code, res, err
}