Files

112 lines
2.8 KiB
Go

/*
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
*/
package fileoper
import (
"context"
"net/http"
"mstore/pkg/descr"
"mstore/pkg/filecli"
)
// DeleteColletion
type DeleteColletionParams struct {
Path string
PathType string `param:"pathType"`
DryRun bool `param:"dryRun"`
}
type DeleteColletionResult struct {
Files []descr.File `json:"files,omitempty"`
}
func (oper *Operator) DeleteColletion(ctx context.Context, operatorID string, param *DeleteColletionParams) (int, *DeleteColletionResult, error) {
var err error
res := &DeleteColletionResult{
Files: make([]descr.File, 0),
}
switch param.PathType {
case filecli.PathTypeRegexp:
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 filecli.PathTypePrefix:
param.Path, err = cleanFilepath(param.Path)
if err != nil {
code := http.StatusInternalServerError
return code, res, err
}
collections, err := oper.listCollectionsWithPrefix(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
default:
param.Path, err = cleanFilepath(param.Path)
if err != nil {
code := http.StatusInternalServerError
return code, res, err
}
collection := param.Path
files, err := oper.deleteFilesInCollection(ctx, collection, param.DryRun)
if err != nil {
code := http.StatusInternalServerError
return code, res, err
}
res.Files = files
}
code := http.StatusOK
return code, res, err
}
func (oper *Operator) deleteFilesInCollection(ctx context.Context, collection string, dryRun bool) ([]descr.File, error) {
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 {
if !dryRun {
oper.logg.Debugf("Delete file %s/%s", file.Collection, file.Name)
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 {
return res, err
}
}
res = append(res, file)
}
return res, err
}