working commit
This commit is contained in:
@@ -198,3 +198,65 @@ func (hand *Handler) ListFiles(rctx *router.Context) {
|
||||
}
|
||||
rctx.SendJSON(code, res.Files)
|
||||
}
|
||||
|
||||
func (hand *Handler) ListCollections(rctx *router.Context) {
|
||||
|
||||
cpath, _ := rctx.GetSubpath("path")
|
||||
if cpath == "" {
|
||||
cpath = "/"
|
||||
}
|
||||
params := &operator.ListCollectionsParams{
|
||||
Path: cpath,
|
||||
}
|
||||
// Rigth checking
|
||||
operatorID, _ := rctx.GetString(userTag)
|
||||
opEnable, err := hand.CheckRight(rctx.Ctx, operatorID, descr.RightReadFiles, "")
|
||||
if err != nil {
|
||||
rctx.SetStatus(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if !opEnable {
|
||||
rctx.SetStatus(http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
// Execution of the operation
|
||||
ctx := rctx.GetContext()
|
||||
code, res, err := hand.oper.ListCollections(ctx, operatorID, params)
|
||||
if err != nil {
|
||||
hand.logg.Errorf("ListCollections error: %v", err)
|
||||
rctx.SetStatus(code)
|
||||
return
|
||||
}
|
||||
rctx.SendJSON(code, res.Collections)
|
||||
}
|
||||
|
||||
func (hand *Handler) DeleteCollection(rctx *router.Context) {
|
||||
|
||||
cpath, _ := rctx.GetSubpath("path")
|
||||
if cpath == "" {
|
||||
cpath = "/"
|
||||
}
|
||||
params := &operator.DeleteColletionParams{
|
||||
Path: cpath,
|
||||
}
|
||||
// Rigth checking
|
||||
operatorID, _ := rctx.GetString(userTag)
|
||||
opEnable, err := hand.CheckRight(rctx.Ctx, operatorID, descr.RightReadFiles, "")
|
||||
if err != nil {
|
||||
rctx.SetStatus(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if !opEnable {
|
||||
rctx.SetStatus(http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
// Execution of the operation
|
||||
ctx := rctx.GetContext()
|
||||
code, res, err := hand.oper.DeleteColletion(ctx, operatorID, params)
|
||||
if err != nil {
|
||||
hand.logg.Errorf("DeleteColletion error: %v", err)
|
||||
rctx.SetStatus(code)
|
||||
return
|
||||
}
|
||||
rctx.SendJSON(code, res.Files)
|
||||
}
|
||||
|
||||
+90
-7
@@ -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
|
||||
}
|
||||
|
||||
@@ -80,6 +80,12 @@ func (svc *Service) Build() error {
|
||||
svc.rout.Get(`/v3/api/files/{filepath}`, svc.hand.ListFiles)
|
||||
svc.rout.Get(`/v3/api/files/`, svc.hand.ListFiles)
|
||||
|
||||
svc.rout.Get(`/v3/api/collections/{path}`, svc.hand.ListCollections)
|
||||
svc.rout.Get(`/v3/api/collections/`, svc.hand.ListCollections)
|
||||
|
||||
svc.rout.Delete(`/v3/api/collection/{path}`, svc.hand.DeleteCollection)
|
||||
svc.rout.Delete(`/v3/api/collection/`, svc.hand.DeleteCollection)
|
||||
|
||||
svc.rout.Get(`/v2/`, svc.hand.GetVersion)
|
||||
|
||||
svc.rout.Head(`/v2/{name}/manifests/{reference}`, svc.hand.ManifestExists)
|
||||
|
||||
Reference in New Issue
Block a user