feat: refactor watchdog
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
package watchdog
|
||||
|
||||
import (
|
||||
"orbits-server/internal/server/bootstrap"
|
||||
"orbits-server/internal/server/database"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func reconcile(fs map[string]struct{}, db map[string]database.File) Result {
|
||||
r := Result{}
|
||||
|
||||
for path := range fs {
|
||||
if _, ok := db[path]; !ok {
|
||||
r.FSOrphans = append(r.FSOrphans, path)
|
||||
}
|
||||
}
|
||||
|
||||
for path, f := range db {
|
||||
if _, ok := fs[path]; !ok {
|
||||
r.DBOrphans = append(r.DBOrphans, f)
|
||||
}
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
func scanFS(env bootstrap.Environment) (map[string]struct{}, error) {
|
||||
fsFiles, err := os.ReadDir(env.ContentDirectory)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res := make(map[string]struct{})
|
||||
for _, f := range fsFiles {
|
||||
full := filepath.Join(env.ContentDirectory, f.Name())
|
||||
res[full] = struct{}{}
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func scanDB(db *gorm.DB) (map[string]database.File, error) {
|
||||
files, err := database.ListFiles(db)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res := make(map[string]database.File)
|
||||
for _, f := range files {
|
||||
res[f.FilePath] = f
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
Reference in New Issue
Block a user