141 lines
3.5 KiB
Go
141 lines
3.5 KiB
Go
|
package webfilesystem
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
|
||
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||
|
)
|
||
|
|
||
|
type V3DirectoryData struct {
|
||
|
MongoId primitive.ObjectID `bson:"_id"`
|
||
|
Parent primitive.ObjectID `bson:"parent_id"`
|
||
|
Children []primitive.ObjectID `bson:"children_id"`
|
||
|
}
|
||
|
|
||
|
func (fs *WebFileSystem) V3CreateDirectory(dirPath string) (primitive.ObjectID, primitive.ObjectID, error) {
|
||
|
splittedPath := fs.SplitPath(dirPath)
|
||
|
parentPath := fs.GetParentPath(dirPath)
|
||
|
|
||
|
parentDirId, err := fs.V3FindFile(parentPath)
|
||
|
if err != nil {
|
||
|
return primitive.NilObjectID, primitive.NilObjectID, err
|
||
|
}
|
||
|
|
||
|
parentDirHeader, err := fs.V3Read(parentDirId, nil)
|
||
|
if err != nil {
|
||
|
return primitive.NilObjectID, primitive.NilObjectID, err
|
||
|
}
|
||
|
|
||
|
newDir := V3FileHeader{
|
||
|
MongoId: primitive.NewObjectID(),
|
||
|
Name: splittedPath[len(splittedPath)-1],
|
||
|
Type: "directory",
|
||
|
Icon: "",
|
||
|
}
|
||
|
newDirData := V3DirectoryData{
|
||
|
MongoId: primitive.NewObjectID(),
|
||
|
Parent: parentDirHeader.MongoId,
|
||
|
Children: []primitive.ObjectID{},
|
||
|
}
|
||
|
|
||
|
headerId, dataId, err := fs.v3WriteFileToMongo(&newDir, &newDirData)
|
||
|
if err != nil {
|
||
|
return primitive.NilObjectID, primitive.NilObjectID, err
|
||
|
}
|
||
|
|
||
|
err = fs.V3AppendChildToDirectory(headerId, parentDirId)
|
||
|
if err != nil {
|
||
|
return primitive.NilObjectID, primitive.NilObjectID, err
|
||
|
}
|
||
|
return headerId, dataId, nil
|
||
|
}
|
||
|
|
||
|
func (fs *WebFileSystem) V3AppendChildToDirectory(childId primitive.ObjectID, parentId primitive.ObjectID) error {
|
||
|
parentDirData := V3DirectoryData{}
|
||
|
parentDir, err := fs.V3Read(parentId, &parentDirData)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
//TODO Check for duplicates?
|
||
|
parentDirData.Children = append(parentDirData.Children, childId)
|
||
|
|
||
|
err = fs.V3WriteUpdateData(parentDir, parentDirData)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (fs *WebFileSystem) V3List(dirPath string) ([]*V3FileHeader, error) {
|
||
|
dirId, err := fs.V3FindFile(dirPath)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
dirData := V3DirectoryData{}
|
||
|
_, err = fs.V3Read(dirId, &dirData)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
// println(dirId.String())
|
||
|
// println(dirData.MongoId.String())
|
||
|
|
||
|
children := []*V3FileHeader{}
|
||
|
for _, childID := range dirData.Children {
|
||
|
childFile, err := fs.V3Read(childID, nil)
|
||
|
if err != nil {
|
||
|
// println(err.Error())
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
children = append(children, childFile)
|
||
|
|
||
|
}
|
||
|
return children, nil
|
||
|
}
|
||
|
|
||
|
func (fs *WebFileSystem) V3FindFile(filePath string) (primitive.ObjectID, error) {
|
||
|
splittedPath := fs.SplitPath(filePath)
|
||
|
rootDir, _, err := fs.V3GetRootDir()
|
||
|
if err != nil {
|
||
|
return primitive.NilObjectID, err
|
||
|
}
|
||
|
// println(rootDir.Name)
|
||
|
lastFileID := rootDir.MongoId
|
||
|
for _, pathPart := range splittedPath[1:] {
|
||
|
// println(pathPart)
|
||
|
id, err := fs.v3findInChildren(pathPart, lastFileID)
|
||
|
if err != nil {
|
||
|
return primitive.NilObjectID, err
|
||
|
}
|
||
|
lastFileID = id
|
||
|
}
|
||
|
return lastFileID, nil
|
||
|
}
|
||
|
|
||
|
// func (fs *WebFileSystem) v3updateChildrenList(directoryID primitive.ObjectID, childrenList []primitive.ObjectID) (primitive.ObjectID, error) {
|
||
|
|
||
|
// }
|
||
|
|
||
|
func (fs *WebFileSystem) v3findInChildren(fileName string, parentDirId primitive.ObjectID) (primitive.ObjectID, error) {
|
||
|
parentDirData := V3DirectoryData{}
|
||
|
_, err := fs.V3Read(parentDirId, &parentDirData)
|
||
|
if err != nil {
|
||
|
return primitive.NilObjectID, err
|
||
|
}
|
||
|
|
||
|
for _, childId := range parentDirData.Children {
|
||
|
childFileHeader, err := fs.V3Read(childId, nil)
|
||
|
if err != nil {
|
||
|
return primitive.NilObjectID, err
|
||
|
}
|
||
|
if childFileHeader.Name == fileName {
|
||
|
return childId, nil
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return primitive.NilObjectID, errors.New("file not found")
|
||
|
}
|