personal-website/webfilesystem/directory.go

137 lines
3.3 KiB
Go
Raw Permalink Normal View History

2023-05-05 14:44:03 +00:00
package webfilesystem
import (
2023-05-07 00:14:30 +00:00
"errors"
2023-05-05 18:32:41 +00:00
2023-05-05 14:44:03 +00:00
"go.mongodb.org/mongo-driver/bson/primitive"
)
type DirectoryData struct {
2023-05-07 01:00:22 +00:00
MongoId primitive.ObjectID `bson:"_id"`
Parent primitive.ObjectID `bson:"parent_id"`
Children []primitive.ObjectID `bson:"children_id"`
2023-05-05 14:44:03 +00:00
}
2023-05-07 01:00:22 +00:00
func (fs *WebFileSystem) CreateDirectory(dirPath string) (primitive.ObjectID, primitive.ObjectID, error) {
splittedPath := fs.SplitPath(dirPath)
parentPath := fs.GetParentPath(dirPath)
parentDirId, err := fs.FindFile(parentPath)
2023-05-07 00:14:30 +00:00
if err != nil {
2023-05-07 01:00:22 +00:00
return primitive.NilObjectID, primitive.NilObjectID, err
}
parentDirHeader, err := fs.readFSDocs(parentDirId, nil)
if err != nil {
return primitive.NilObjectID, primitive.NilObjectID, err
}
newDir := FileHeader{
MongoId: primitive.NewObjectID(),
Name: splittedPath[len(splittedPath)-1],
Type: "directory",
Icon: "",
}
newDirData := DirectoryData{
MongoId: primitive.NewObjectID(),
Parent: parentDirHeader.MongoId,
Children: []primitive.ObjectID{},
}
headerId, dataId, err := fs.writeFileToMongo(&newDir, &newDirData)
if err != nil {
return primitive.NilObjectID, primitive.NilObjectID, err
}
err = fs.AppendChildToDirectory(headerId, parentDirId)
if err != nil {
return primitive.NilObjectID, primitive.NilObjectID, err
}
return headerId, dataId, nil
}
func (fs *WebFileSystem) AppendChildToDirectory(childId primitive.ObjectID, parentId primitive.ObjectID) error {
parentDirData := DirectoryData{}
parentDir, err := fs.readFSDocs(parentId, &parentDirData)
if err != nil {
return err
2023-05-07 00:14:30 +00:00
}
2023-05-07 01:00:22 +00:00
//TODO Check for duplicates?
parentDirData.Children = append(parentDirData.Children, childId)
err = fs.UpdateFileData(parentDir, parentDirData)
if err != nil {
return err
}
return nil
}
func (fs *WebFileSystem) ListDir(dirPath string) ([]*FileHeader, error) {
dirId, err := fs.FindFile(dirPath)
if err != nil {
return nil, err
2023-05-07 00:14:30 +00:00
}
2023-05-07 01:00:22 +00:00
dirData := DirectoryData{}
_, err = fs.readFSDocs(dirId, &dirData)
2023-05-07 00:14:30 +00:00
if err != nil {
return nil, err
}
2023-05-07 01:00:22 +00:00
// println(dirId.String())
// println(dirData.MongoId.String())
2023-05-07 00:14:30 +00:00
2023-05-07 01:00:22 +00:00
children := []*FileHeader{}
for _, childID := range dirData.Children {
childFile, err := fs.readFSDocs(childID, nil)
2023-05-07 00:14:30 +00:00
if err != nil {
// println(err.Error())
continue
}
2023-05-07 01:00:22 +00:00
children = append(children, childFile)
2023-05-07 00:14:30 +00:00
}
return children, nil
}
func (fs *WebFileSystem) FindFile(filePath string) (primitive.ObjectID, error) {
splittedPath := fs.SplitPath(filePath)
2023-05-07 01:00:22 +00:00
rootDir, _, err := fs.GetRootDir()
2023-05-07 00:14:30 +00:00
if err != nil {
return primitive.NilObjectID, err
}
// println(rootDir.Name)
2023-05-07 01:00:22 +00:00
lastFileID := rootDir.MongoId
2023-05-07 00:14:30 +00:00
for _, pathPart := range splittedPath[1:] {
// println(pathPart)
id, err := fs.findInChildren(pathPart, lastFileID)
if err != nil {
return primitive.NilObjectID, err
}
lastFileID = id
}
return lastFileID, nil
}
func (fs *WebFileSystem) findInChildren(fileName string, parentDirId primitive.ObjectID) (primitive.ObjectID, error) {
2023-05-07 01:00:22 +00:00
parentDirData := DirectoryData{}
_, err := fs.readFSDocs(parentDirId, &parentDirData)
2023-05-07 00:14:30 +00:00
if err != nil {
return primitive.NilObjectID, err
}
2023-05-07 01:00:22 +00:00
for _, childId := range parentDirData.Children {
childFileHeader, err := fs.readFSDocs(childId, nil)
2023-05-07 00:14:30 +00:00
if err != nil {
return primitive.NilObjectID, err
}
if childFileHeader.Name == fileName {
return childId, nil
}
}
return primitive.NilObjectID, errors.New("file not found")
}