added file checker, completation/size/digest

This commit is contained in:
2026-03-30 13:20:13 +02:00
parent 8afe71d925
commit f5227bcac9
8 changed files with 291 additions and 43 deletions
+88
View File
@@ -0,0 +1,88 @@
/*
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
*/
package fileoper
import (
"context"
"net/http"
"mstore/pkg/descr"
)
// Check files
type CheckFilesParams struct {
Path string
PathType string `param:"pathType"`
}
type CheckFilesResult struct {
Files []descr.File `json:"files,omitempty"`
}
func (oper *Operator) CheckFiles(ctx context.Context, operatorID string, params *CheckFilesParams) (int, *CheckFilesResult, error) {
var code int
res := &CheckFilesResult{}
var err error
// Check existing and size
files, err := oper.listFiles(ctx, params.PathType, params.Path)
if err != nil {
code = http.StatusInternalServerError
return code, res, err
}
for _, file := range files {
exists, size, err := oper.store.FileExists(file.Collection, file.Name)
if err != nil {
code := http.StatusInternalServerError
return code, res, err
}
if !exists {
res.Files = append(res.Files, file)
err = oper.mdb.DeleteFileByCollectionName(ctx, file.Collection, file.Name)
if err != nil {
code := http.StatusInternalServerError
return code, res, err
}
}
if size != file.Size {
res.Files = append(res.Files, file)
err = oper.mdb.DeleteFileByCollectionName(ctx, file.Collection, file.Name)
if err != nil {
code := http.StatusInternalServerError
return code, res, err
}
err = oper.store.DeleteFile(file.Collection, file.Name)
if err != nil {
code := http.StatusInternalServerError
return code, res, err
}
}
}
// Check hashs
files, err = oper.listFiles(ctx, params.PathType, params.Path)
if err != nil {
code = http.StatusInternalServerError
return code, res, err
}
for _, file := range files {
sum, err := oper.store.GetFileCheksum(file.Collection, file.Name)
if err != nil {
code := http.StatusInternalServerError
return code, res, err
}
if sum != file.Checksum {
res.Files = append(res.Files, file)
err = oper.mdb.DeleteFileByCollectionName(ctx, file.Collection, file.Name)
if err != nil {
code := http.StatusInternalServerError
return code, res, err
}
err = oper.store.DeleteFile(file.Collection, file.Name)
if err != nil {
code := http.StatusInternalServerError
return code, res, err
}
}
}
return code, res, err
}