fixed file checker
This commit is contained in:
+10
-2
@@ -6,6 +6,7 @@ package fileoper
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
|
||||
"mstore/pkg/descr"
|
||||
)
|
||||
@@ -21,9 +22,11 @@ type CheckFilesResult struct {
|
||||
|
||||
func (oper *Operator) CheckFiles(ctx context.Context, operatorID string, params *CheckFilesParams) (int, *CheckFilesResult, error) {
|
||||
var code int
|
||||
res := &CheckFilesResult{}
|
||||
res := &CheckFilesResult{
|
||||
Files: make([]descr.File, 0),
|
||||
}
|
||||
var err error
|
||||
|
||||
code = http.StatusOK
|
||||
// Check existing and size
|
||||
files, err := oper.listFiles(ctx, params.PathType, params.Path)
|
||||
if err != nil {
|
||||
@@ -36,7 +39,9 @@ func (oper *Operator) CheckFiles(ctx context.Context, operatorID string, params
|
||||
code := http.StatusInternalServerError
|
||||
return code, res, err
|
||||
}
|
||||
fullpath := filepath.Join(file.Collection, file.Name)
|
||||
if !exists {
|
||||
oper.logg.Warningf("File not exists: %s", fullpath)
|
||||
res.Files = append(res.Files, file)
|
||||
err = oper.mdb.DeleteFileByCollectionName(ctx, file.Collection, file.Name)
|
||||
if err != nil {
|
||||
@@ -45,6 +50,7 @@ func (oper *Operator) CheckFiles(ctx context.Context, operatorID string, params
|
||||
}
|
||||
}
|
||||
if size != file.Size {
|
||||
oper.logg.Warningf("File has incorrect size: %s", fullpath)
|
||||
res.Files = append(res.Files, file)
|
||||
err = oper.mdb.DeleteFileByCollectionName(ctx, file.Collection, file.Name)
|
||||
if err != nil {
|
||||
@@ -70,7 +76,9 @@ func (oper *Operator) CheckFiles(ctx context.Context, operatorID string, params
|
||||
code := http.StatusInternalServerError
|
||||
return code, res, err
|
||||
}
|
||||
fullpath := filepath.Join(file.Collection, file.Name)
|
||||
if sum != file.Checksum {
|
||||
oper.logg.Warningf("File has incorrect digest: %s", fullpath)
|
||||
res.Files = append(res.Files, file)
|
||||
err = oper.mdb.DeleteFileByCollectionName(ctx, file.Collection, file.Name)
|
||||
if err != nil {
|
||||
|
||||
@@ -60,6 +60,7 @@ func NewServer() (*Server, error) {
|
||||
var err error
|
||||
srv := &Server{}
|
||||
srv.logg = logger.NewLoggerWithSubject("server")
|
||||
srv.ctx, srv.cancel = context.WithCancel(context.Background())
|
||||
return srv, err
|
||||
}
|
||||
|
||||
@@ -425,9 +426,7 @@ func (srv *Server) Run() error {
|
||||
}
|
||||
srv.logg.Infof("Server run with user: %s", usr.Username)
|
||||
|
||||
srv.ctx, srv.cancel = context.WithCancel(context.Background())
|
||||
svcDone := make(chan error, 1)
|
||||
|
||||
// Service run
|
||||
srv.logg.Infof("Start service")
|
||||
startService := func(svc *service.Service, done chan error) {
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
|
||||
*/
|
||||
package filecmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"mstore/pkg/descr"
|
||||
"mstore/pkg/filecli"
|
||||
)
|
||||
|
||||
// CheckFiles
|
||||
type CheckFilesParams struct {
|
||||
Filepath string
|
||||
Detail bool
|
||||
Prefix bool
|
||||
Regexp bool
|
||||
}
|
||||
|
||||
type CheckFilesResult struct {
|
||||
Files []descr.File `json:"files,omitempty"`
|
||||
Filenames []string `json:"filenames,omitempty"`
|
||||
}
|
||||
|
||||
func (util *FileUtil) CheckFiles(cmd *cobra.Command, args []string) {
|
||||
util.checkFilesParams.Filepath = args[0]
|
||||
res, err := util.checkFiles(&util.commonFileParams, &util.checkFilesParams)
|
||||
printResponse(res, err)
|
||||
}
|
||||
|
||||
func (util *FileUtil) checkFiles(common *CommonFileParams, params *CheckFilesParams) (*CheckFilesResult, error) {
|
||||
var err error
|
||||
res := &CheckFilesResult{}
|
||||
timeout := time.Duration(common.Timeout) * time.Second
|
||||
ctx, _ := context.WithTimeout(context.Background(), timeout)
|
||||
ref, err := filecli.ParsePath(params.Filepath)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
ref.SetUserinfo(common.Username, common.Password)
|
||||
ref.PathType(pathType(params.Regexp, params.Prefix))
|
||||
mw := filecli.NewBasicAuthMiddleware(ref.Userinfo())
|
||||
|
||||
cli := filecli.NewClient(nil, mw)
|
||||
list, err := cli.CheckFiles(ctx, ref.Raw())
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
files := descr.NewFiles()
|
||||
err = json.Unmarshal(list, files.ArrayPtr())
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
if !params.Detail {
|
||||
res.Filenames = files.List()
|
||||
} else {
|
||||
res.Files = files.Array()
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
@@ -131,7 +131,7 @@ func (util *FileUtil) MakeFileCmds() *cobra.Command {
|
||||
Use: "check [user:pass@]hostname[:port]/catalog",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Short: "Ckeck files in storage",
|
||||
Run: util.ListFiles,
|
||||
Run: util.CheckFiles,
|
||||
}
|
||||
checkFilesCmd.Flags().BoolVarP(&util.checkFilesParams.Detail, "detail", "D", false, "Show detail file information")
|
||||
checkFilesCmd.Flags().BoolVarP(&util.checkFilesParams.Prefix, "prefix", "P", true, "Use path as collection path prefix")
|
||||
|
||||
@@ -19,7 +19,7 @@ func (cli *Client) CheckFiles(ctx context.Context, rawpath string) ([]byte, erro
|
||||
if err != nil {
|
||||
return list, err
|
||||
}
|
||||
uri := ref.FilesEP()
|
||||
uri := ref.CheckEP()
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user