package webfilesystem import ( "errors" "go.mongodb.org/mongo-driver/bson/primitive" ) type DirectoryData struct { MongoId primitive.ObjectID `bson:"_id" json:"-"` Parent primitive.ObjectID `bson:"parent_id" json:"parent"` Children []primitive.ObjectID `bson:"children_id" json:"children"` } func (fs *WebFileSystem) CreateDirectory(dirPath string) (primitive.ObjectID, primitive.ObjectID, error) { splittedPath := fs.SplitPath(dirPath) parentPath := fs.GetParentPath(dirPath) parentDirId, err := fs.FindFile(parentPath) if err != nil { 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 { // println(parentId) parentDirData := DirectoryData{} parentDir, err := fs.readFSDocs(parentId, &parentDirData) if err != nil { return err } //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) 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 } func (fs *WebFileSystem) ListDir(dirPath string) ([]*FileHeader, error) { dirId, err := fs.FindFile(dirPath) if err != nil { return nil, err } 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) { dirData := DirectoryData{} _, err := fs.readFSDocs(dirId, &dirData) if err != nil { return nil, err } 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) FindFile(filePath string) (primitive.ObjectID, error) { splittedPath := fs.SplitPath(filePath) rootDir, _, err := fs.GetRootDir() if err != nil { return primitive.NilObjectID, err } // println(rootDir.Name) lastFileID := rootDir.MongoId 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) { parentDirData := DirectoryData{} _, err := fs.readFSDocs(parentDirId, &parentDirData) if err != nil { return primitive.NilObjectID, err } for _, childId := range parentDirData.Children { childFileHeader, err := fs.readFSDocs(childId, nil) if err != nil { return primitive.NilObjectID, err } if childFileHeader.Name == fileName { return childId, nil } } return primitive.NilObjectID, errors.New("file not found") }