personal-website/webfilesystem/directory.go

182 lines
4.5 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 {
MongoId primitive.ObjectID `bson:"_id" json:"-"`
2023-05-18 01:57:44 +00:00
Parent primitive.ObjectID `bson:"parent_id" json:"parent"` //TODO: Delete
Children []primitive.ObjectID `bson:"children_id" json:"children"`
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 {
2023-05-10 12:39:49 +00:00
// println(parentId)
2023-05-07 01:00:22 +00:00
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
}
2023-05-10 12:39:49 +00:00
func (fs *WebFileSystem) RemoveChildToDirectory(childId primitive.ObjectID, parentId primitive.ObjectID) error {
parentDirData := DirectoryData{}
parentDir, err := fs.readFSDocs(parentId, &parentDirData)
if err != nil {
return err
}
newChildrenList := []primitive.ObjectID{}
for _, child := range parentDirData.Children {
if child != childId {
newChildrenList = append(newChildrenList, child)
}
}
parentDirData.Children = newChildrenList
err = fs.UpdateFileData(parentDir, parentDirData)
if err != nil {
return err
}
return nil
}
2023-05-07 01:00:22 +00:00
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
2023-05-17 13:09:22 +00:00
children, err := fs.listDirByObjectID(dirId)
// dirData := DirectoryData{}
// _, err = fs.readFSDocs(dirId, &dirData)
// if err != nil {
// return nil, err
// }
// // println(dirId.String())
// // println(dirData.MongoId.String())
// children := []*FileHeader{}
// for _, childID := range dirData.Children {
// childFile, err := fs.readFSDocs(childID, nil)
// if err != nil {
// // println(err.Error())
// continue
// }
// children = append(children, childFile)
// }
return children, nil
}
func (fs *WebFileSystem) listDirByObjectID(dirId primitive.ObjectID) ([]*FileHeader, error) {
2023-05-07 01:00:22 +00:00
dirData := DirectoryData{}
2023-05-17 13:09:22 +00:00
_, 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
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")
}