Files
mstore/app/fileoper/check.go
T
2026-03-30 14:06:58 +02:00

97 lines
2.6 KiB
Go

/*
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
*/
package fileoper
import (
"context"
"net/http"
"path/filepath"
"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{
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 {
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
}
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 {
code := http.StatusInternalServerError
return code, res, err
}
}
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 {
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
}
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 {
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
}