127 lines
2.7 KiB
Go
127 lines
2.7 KiB
Go
package webfilesystem
|
|
|
|
import (
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
)
|
|
|
|
func (fs *WebFileSystem) validate() error {
|
|
filter := primitive.M{
|
|
// "type": "directory",
|
|
}
|
|
cur, err := fs.webfsFilesTable.Find(fs.ctx, filter)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
orfanedDir, err := fs.Read("/orfaned", nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_ = orfanedDir
|
|
for cur.Next(fs.ctx) {
|
|
fileHeader := FileHeader{}
|
|
cur.Decode(&fileHeader)
|
|
if fileHeader.Name == "/" {
|
|
continue
|
|
}
|
|
|
|
res, err := fs.checkFileParent(&fileHeader)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !res {
|
|
println(fileHeader.Name)
|
|
err := fs.moveByID(fileHeader.MongoId, primitive.NilObjectID, orfanedDir.MongoId, true)
|
|
if err != nil {
|
|
println(err.Error())
|
|
println()
|
|
continue
|
|
}
|
|
}
|
|
// println()
|
|
}
|
|
// rootHeader, rootData, err := fs.GetRootDir()
|
|
// if err != nil {
|
|
// return err
|
|
// }
|
|
|
|
// for _, child := range rootData.Children {
|
|
// fs.Chi
|
|
// }
|
|
// println()
|
|
// fs.Read
|
|
// filter := primitive.M{
|
|
// "type": "directory",
|
|
// }
|
|
// cur, err := fs.webfsFilesTable.Find(fs.ctx, filter)
|
|
// if err != nil {
|
|
// return err
|
|
// }
|
|
|
|
// directories := []FileHeader{}
|
|
// for cur.Next(fs.ctx) {
|
|
// fileHeader := FileHeader{}
|
|
// err := cur.Decode(&fileHeader)
|
|
// if err != nil {
|
|
// println(err.Error())
|
|
// continue
|
|
// }
|
|
// directories = append(directories, fileHeader)
|
|
// }
|
|
|
|
// allChilds := map[primitive.ObjectID{}]
|
|
// for _, dir := range directories {
|
|
// println(dir.Name)
|
|
// dirData := DirectoryData{}
|
|
// _, err := fs.ReadByObjectID(dir.MongoId, &dirData)
|
|
// if err != nil {
|
|
// println(err.Error())
|
|
// continue
|
|
// }
|
|
|
|
// //Check for broken links in children
|
|
// for _, dirChild := range dirData.Children {
|
|
// dirChildHeader, err := fs.ReadByObjectID(dirChild, nil)
|
|
// println(" -" + dirChildHeader.Name)
|
|
// if err != nil {
|
|
// println(err.Error())
|
|
// continue
|
|
// }
|
|
// }
|
|
// }
|
|
return nil
|
|
}
|
|
|
|
func (fs *WebFileSystem) checkFileParent(checkFileHeader *FileHeader) (bool, error) {
|
|
filter := primitive.M{
|
|
"type": "directory",
|
|
}
|
|
cur, err := fs.webfsFilesTable.Find(fs.ctx, filter)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
// parentFounded := false
|
|
for cur.Next(fs.ctx) {
|
|
dirHeader := FileHeader{}
|
|
// dirData := DirectoryData{}
|
|
cur.Decode(&dirHeader)
|
|
_, err := fs.readFSDocs(dirHeader.MongoId, nil)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
children, err := fs.listDirByObjectID(dirHeader.MongoId)
|
|
for _, child := range children {
|
|
if child.MongoId == checkFileHeader.MongoId {
|
|
return true, nil
|
|
}
|
|
// println(child.Name)
|
|
}
|
|
}
|
|
return false, nil
|
|
}
|
|
|
|
// func (fs *WebFileSystem) GetChildrenHeaders(directoryID primitive.ObjectID) ([]FileHeader, error) {
|
|
// fs.ReadByObjectID(directoryID, nil)
|
|
// }
|