working commit

This commit is contained in:
2026-02-19 18:04:34 +02:00
parent bb0f58f46c
commit 7f7e4706d6
5 changed files with 164 additions and 84 deletions
+1 -1
View File
@@ -216,7 +216,7 @@ func (hand *Handler) GetBlob(rctx *router.Context) {
defer res.ReadCloser.Close() defer res.ReadCloser.Close()
_, err = io.Copy(rctx.Writer, res.ReadCloser) _, err = io.Copy(rctx.Writer, res.ReadCloser)
if err != nil { if err != nil {
hand.logg.Errorf("GetFile error: %v", err) hand.logg.Errorf("GetBlob error: %v", err)
rctx.SetStatus(http.StatusInternalServerError) rctx.SetStatus(http.StatusInternalServerError)
return return
} }
+1 -1
View File
@@ -162,7 +162,7 @@ func (hand *Handler) DeleteFile(rctx *router.Context) {
ctx := rctx.GetContext() ctx := rctx.GetContext()
code, _, err := hand.oper.DeleteFile(ctx, operatorID, params) code, _, err := hand.oper.DeleteFile(ctx, operatorID, params)
if err != nil { if err != nil {
hand.logg.Errorf("GetFile error: %v", err) hand.logg.Errorf("DeleteFIle error: %v", err)
} }
rctx.SetStatus(code) rctx.SetStatus(code)
+151 -79
View File
@@ -259,12 +259,17 @@ func (oper *Operator) DeleteFile(ctx context.Context, operID string, param *Dele
filename := path.Base(xfilepath) filename := path.Base(xfilepath)
collection := path.Dir(xfilepath) collection := path.Dir(xfilepath)
exist, _, err := oper.mdb.GetFileByCollectionName(ctx, collection, filename) descrExists, _, err := oper.mdb.GetFileByCollectionName(ctx, collection, filename)
if err != nil { if err != nil {
code = http.StatusInternalServerError code = http.StatusInternalServerError
return code, res, err return code, res, err
} }
if exist { if !descrExists {
code := http.StatusNotFound
return code, res, err
}
if descrExists {
err = oper.mdb.DeleteFileByCollectionName(ctx, collection, filename) err = oper.mdb.DeleteFileByCollectionName(ctx, collection, filename)
if err != nil { if err != nil {
code = http.StatusInternalServerError code = http.StatusInternalServerError
@@ -282,42 +287,76 @@ func (oper *Operator) DeleteFile(ctx context.Context, operID string, param *Dele
// ListFiles // ListFiles
type ListFilesParams struct { type ListFilesParams struct {
Filepath string Filepath string
PathAs terms.PathAs `param:"pathAs"`
} }
type ListFilesResult struct { type ListFilesResult struct {
Files []descr.File `json:"files,omitempty"` Files []descr.File `json:"files,omitempty"`
} }
func (oper *Operator) ListFiles(ctx context.Context, operID string, param *ListFilesParams) (int, *ListFilesResult, error) { func (oper *Operator) ListFiles(ctx context.Context, operID string, params *ListFilesParams) (int, *ListFilesResult, error) {
var err error var err error
res := &ListFilesResult{ res := &ListFilesResult{
Files: make([]descr.File, 0), Files: make([]descr.File, 0),
} }
param.Filepath, err = cleanFilepath(param.Filepath) params.Filepath, err = cleanFilepath(params.Filepath)
if err != nil { if err != nil {
code := http.StatusInternalServerError code := http.StatusInternalServerError
return code, res, err return code, res, err
} }
switch params.PathAs {
fileDescrs, err := oper.mdb.ListAllFiles(ctx) case terms.AsPrefix:
if err != nil { files, err := oper.listFilesWithPrefix(ctx, params.Filepath)
code := http.StatusInternalServerError if err != nil {
return code, res, err code := http.StatusInternalServerError
} return code, res, err
rFilepath := filepath.Join("/", param.Filepath)
for _, item := range fileDescrs {
cFilepath := filepath.Join("/", item.Collection, item.Name)
if strings.HasPrefix(cFilepath, rFilepath) {
res.Files = append(res.Files, item)
} }
res.Files = files
default: // Fine
files, err := oper.listFilesInOneCollection(ctx, params.Filepath)
if err != nil {
code := http.StatusInternalServerError
return code, res, err
}
res.Files = files
} }
code := http.StatusOK code := http.StatusOK
return code, res, err return code, res, err
} }
func (oper *Operator) listFilesInOneCollection(ctx context.Context, collection string) ([]descr.File, error) {
var err error
res := make([]descr.File, 0)
files, err := oper.mdb.ListFilesByCollection(ctx, collection)
if err != nil {
return res, err
}
res = files
return res, err
}
func (oper *Operator) listFilesWithPrefix(ctx context.Context, prefix string) ([]descr.File, error) {
var err error
res := make([]descr.File, 0)
files, err := oper.mdb.ListAllFiles(ctx)
if err != nil {
return res, err
}
for _, file := range files {
fullpath := filepath.Join(file.Collection, file.Name)
if strings.HasPrefix(fullpath, prefix) {
res = append(res, file)
}
}
return res, err
}
// ListCollections // ListCollections
type ListCollectionsParams struct { type ListCollectionsParams struct {
Path string Path string
PathAS terms.PathAs `param:"pathAs"`
} }
type ListCollectionsResult struct { type ListCollectionsResult struct {
Collections []string `json:"collection,omitempty"` Collections []string `json:"collection,omitempty"`
@@ -333,37 +372,83 @@ func (oper *Operator) ListCollections(ctx context.Context, operID string, param
code := http.StatusInternalServerError code := http.StatusInternalServerError
return code, res, err return code, res, err
} }
fileDescrs, err := oper.mdb.ListAllFiles(ctx) collectionList := make([]string, 0)
if err != nil { switch param.PathAS {
code := http.StatusInternalServerError case terms.AsPrefix:
return code, res, err collectionList, err = oper.listCollectionsWithPrefix(ctx, param.Path)
} if err != nil {
cmap := make(map[string]bool) code := http.StatusInternalServerError
for _, item := range fileDescrs { return code, res, err
cPath := filepath.Join("/", item.Collection)
pattern := filepath.Join("/", param.Path)
if strings.HasPrefix(cPath, pattern) {
_, exists := cmap[cPath]
if !exists {
cmap[cPath] = true
}
} }
default:
collectionList, err = oper.listAllCollections(ctx)
if err != nil {
code := http.StatusInternalServerError
return code, res, err
}
} }
for key, _ := range cmap { res.Collections = collectionList
res.Collections = append(res.Collections, key)
}
slices.Sort(res.Collections)
code := http.StatusOK code := http.StatusOK
return code, res, err return code, res, err
} }
func (oper *Operator) listCollectionsWithPrefix(ctx context.Context, prefix string) ([]string, error) {
var err error
res := make([]string, 0)
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, len(collMap))
for key, _ := range collMap {
if strings.HasPrefix(key, prefix) {
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)
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, len(collMap))
for key, _ := range collMap {
res = append(res, key)
}
slices.Sort(res)
return res, err
}
// DeleteColletion // DeleteColletion
type DeleteColletionParams struct { type DeleteColletionParams struct {
Path string Path string
PathAs string `param:"pathAs"` PathAs string `param:"pathAs"`
DryRun bool `param:"dryRun"`
} }
type DeleteColletionResult struct { type DeleteColletionResult struct {
Files []descr.File `json:"collection,omitempty"` Files []descr.File `json:"files,omitempty"`
} }
func (oper *Operator) DeleteColletion(ctx context.Context, operID string, param *DeleteColletionParams) (int, *DeleteColletionResult, error) { func (oper *Operator) DeleteColletion(ctx context.Context, operID string, param *DeleteColletionParams) (int, *DeleteColletionResult, error) {
@@ -376,59 +461,47 @@ func (oper *Operator) DeleteColletion(ctx context.Context, operID string, param
code := http.StatusInternalServerError code := http.StatusInternalServerError
return code, res, err return code, res, err
} }
oper.logg.Debugf("=== Use path as %s", param.PathAs) //oper.logg.Debugf("DeleteCollection: Use path as %s", param.PathAs)
switch terms.PathAs(param.PathAs) { switch terms.PathAs(param.PathAs) {
case terms.AsPrefix: case terms.AsPrefix:
fileDescrs, err := oper.mdb.ListAllFiles(ctx) collections, err := oper.listCollectionsWithPrefix(ctx, param.Path)
if err != nil { if err != nil {
code := http.StatusInternalServerError code := http.StatusInternalServerError
return code, res, err return code, res, err
} }
allfiles := make([]descr.File, 0)
collMap := make(map[string]bool)
for _, item := range fileDescrs {
_, exists := collMap[item.Collection]
if !exists {
collMap[item.Collection] = true
}
}
collections := make([]string, len(collMap))
for key, _ := range collMap {
collections = append(collections, key)
}
for _, collection := range collections { for _, collection := range collections {
if strings.HasPrefix(collection, param.Path) { files, err := oper.deleteFilesInCollection(ctx, collection, param.DryRun)
if err != nil {
fileDescrs, err := oper.mdb.ListFilesByCollection(ctx, collection) code := http.StatusInternalServerError
if err != nil { return code, res, err
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
}
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)
}
} }
allfiles = append(allfiles, files...)
} }
default: res.Files = allfiles
fileDescrs, err := oper.mdb.ListFilesByCollection(ctx, param.Path) default: // Fine
collection := param.Path
files, err := oper.deleteFilesInCollection(ctx, collection, param.DryRun)
if err != nil { if err != nil {
code := http.StatusInternalServerError code := http.StatusInternalServerError
return code, res, err return code, res, err
} }
res.Files = files
}
code := http.StatusOK
return code, res, err
}
// TODO: transaction func (oper *Operator) deleteFilesInCollection(ctx context.Context, collection string, dryRun bool) ([]descr.File, error) {
for _, file := range fileDescrs { var err error
res := make([]descr.File, 0)
files, err := oper.mdb.ListFilesByCollection(ctx, collection)
if err != nil {
return res, err
}
for _, file := range files {
oper.logg.Debugf("Delete file %s/%s", file.Collection, file.Name)
if !dryRun {
err = oper.store.DeleteFile(file.Collection, file.Name) err = oper.store.DeleteFile(file.Collection, file.Name)
if err != nil { if err != nil {
oper.logg.Warningf("%v", err) oper.logg.Warningf("%v", err)
@@ -436,12 +509,11 @@ func (oper *Operator) DeleteColletion(ctx context.Context, operID string, param
} }
err = oper.mdb.DeleteFileByCollectionName(ctx, file.Collection, file.Name) err = oper.mdb.DeleteFileByCollectionName(ctx, file.Collection, file.Name)
if err != nil { if err != nil {
code := http.StatusInternalServerError return res, err
return code, res, err
} }
res.Files = append(res.Files, file)
} }
res = append(res, file)
} }
code := http.StatusOK return res, err
return code, res, err
} }
+6 -2
View File
@@ -16,6 +16,7 @@ import (
"io/fs" "io/fs"
"net/url" "net/url"
"path/filepath" "path/filepath"
"slices"
"strings" "strings"
"time" "time"
@@ -132,7 +133,8 @@ func (util *FileUtil) CreateCollectionCmds() *cobra.Command {
Run: util.DeleteCollection, Run: util.DeleteCollection,
} }
deleteCollectionCmd.Flags().BoolVarP(&util.deleteCollectionParams.Detail, "detail", "D", false, "Show detail file information") deleteCollectionCmd.Flags().BoolVarP(&util.deleteCollectionParams.Detail, "detail", "D", false, "Show detail file information")
deleteCollectionCmd.Flags().BoolVarP(&util.deleteCollectionParams.AsPrefix, "asPrefix", "P", true, "Use path as collection path prefix") deleteCollectionCmd.Flags().BoolVarP(&util.deleteCollectionParams.AsPrefix, "asprefix", "P", false, "Use path as collection path prefix")
deleteCollectionCmd.Flags().BoolVarP(&util.deleteCollectionParams.DryRun, "dryrun", "Y", false, "Simulate process, don't delete files")
subCmd.AddCommand(deleteCollectionCmd) subCmd.AddCommand(deleteCollectionCmd)
@@ -432,6 +434,7 @@ type DeleteCollectionParams struct {
Path string Path string
Detail bool Detail bool
AsPrefix bool AsPrefix bool
DryRun bool
} }
type DeleteCollectionResult struct { type DeleteCollectionResult struct {
@@ -462,7 +465,7 @@ func (util *FileUtil) deleteCollection(common *CommonFileParams, params *DeleteC
default: default:
pathAs = terms.AsFinePath pathAs = terms.AsFinePath
} }
files, err := client.NewClient().DeleteCollection(ctx, params.Path, pathAs) files, err := client.NewClient().DeleteCollection(ctx, params.Path, pathAs, params.DryRun)
if err != nil { if err != nil {
return res, err return res, err
} }
@@ -472,6 +475,7 @@ func (util *FileUtil) deleteCollection(common *CommonFileParams, params *DeleteC
for _, file := range files { for _, file := range files {
res.Filenames = append(res.Filenames, filepath.Join(file.Collection, file.Name)) res.Filenames = append(res.Filenames, filepath.Join(file.Collection, file.Name))
} }
slices.Sort(res.Filenames)
} }
return res, err return res, err
} }
+5 -1
View File
@@ -325,7 +325,7 @@ func (cli *Client) ListCollections(ctx context.Context, catalogURI string) ([]st
return res, err return res, err
} }
func (cli *Client) DeleteCollection(ctx context.Context, catalogURI string, usePathAs terms.PathAs) ([]descr.File, error) { func (cli *Client) DeleteCollection(ctx context.Context, catalogURI string, usePathAs terms.PathAs, dryRun bool) ([]descr.File, error) {
var err error var err error
res := make([]descr.File, 0) res := make([]descr.File, 0)
@@ -337,10 +337,14 @@ func (cli *Client) DeleteCollection(ctx context.Context, catalogURI string, useP
if err != nil { if err != nil {
return res, err return res, err
} }
// Add values
values := url.Values{} values := url.Values{}
if usePathAs != "" { if usePathAs != "" {
values.Add("pathAs", string(usePathAs)) values.Add("pathAs", string(usePathAs))
} }
if dryRun {
values.Add("dryRun", "true")
}
encodedValues := values.Encode() encodedValues := values.Encode()
if encodedValues != "" { if encodedValues != "" {
catalogURI = catalogURI + "?" + encodedValues catalogURI = catalogURI + "?" + encodedValues