added minimal image checker

This commit is contained in:
2026-03-30 23:12:02 +02:00
parent 856ea529a7
commit 1c894e190d
12 changed files with 341 additions and 16 deletions
+58 -2
View File
@@ -7,8 +7,11 @@ import (
"context"
"net/http"
"path/filepath"
"regexp"
"strings"
"mstore/pkg/descr"
"mstore/pkg/filecli"
)
// Check files
@@ -50,7 +53,7 @@ func (oper *Operator) CheckFiles(ctx context.Context, operatorID string, params
}
}
if size != file.Size {
oper.logg.Warningf("File has incorrect size: %s", fullpath)
oper.logg.Warningf("Delete file with incorrect size: %s", fullpath)
res.Files = append(res.Files, file)
err = oper.mdb.DeleteFileByCollectionName(ctx, file.Collection, file.Name)
if err != nil {
@@ -78,7 +81,7 @@ func (oper *Operator) CheckFiles(ctx context.Context, operatorID string, params
}
fullpath := filepath.Join(file.Collection, file.Name)
if sum != file.Checksum {
oper.logg.Warningf("File has incorrect digest: %s", fullpath)
oper.logg.Warningf("Delete file with incorrect digest: %s", fullpath)
res.Files = append(res.Files, file)
err = oper.mdb.DeleteFileByCollectionName(ctx, file.Collection, file.Name)
if err != nil {
@@ -92,5 +95,58 @@ func (oper *Operator) CheckFiles(ctx context.Context, operatorID string, params
}
}
}
// Find orphans
filelist, err := oper.store.ListAllFiles()
if err != nil {
code = http.StatusInternalServerError
return code, res, err
}
for _, fullpath := range filelist {
switch params.PathType {
case filecli.PathTypeRegexp:
re, err := regexp.Compile(params.Path)
if err != nil {
code = http.StatusInternalServerError
return code, res, err
}
if !re.MatchString(fullpath) {
continue
}
case filecli.PathTypePrefix:
prefix, err := cleanFilepath(params.Path)
if err != nil {
code = http.StatusInternalServerError
return code, res, err
}
if !strings.HasPrefix(fullpath, prefix) {
continue
}
default:
collection, err := cleanFilepath(params.Path)
if err != nil {
code = http.StatusInternalServerError
return code, res, err
}
if filepath.Dir(fullpath) != collection {
continue
}
}
filename := filepath.Base(fullpath)
collection := filepath.Dir(fullpath)
exists, _, err := oper.mdb.GetFileByCollectionName(ctx, collection, filename)
if err != nil {
code = http.StatusInternalServerError
return code, res, err
}
if !exists {
oper.logg.Warningf("Delete orphan file: %s", fullpath)
err = oper.store.DeleteFile(collection, filename)
if err != nil {
code := http.StatusInternalServerError
return code, res, err
}
}
}
return code, res, err
}
-4
View File
@@ -110,10 +110,6 @@ func (oper *Operator) listFiles(ctx context.Context, pathType, filepath string)
}
res = files
default:
filepath, err = cleanFilepath(filepath)
if err != nil {
return res, err
}
filepath, err = cleanFilepath(filepath)
if err != nil {
return res, err