personal-website/webfilesystem/newmethods.go

57 lines
1.1 KiB
Go
Raw Normal View History

2023-05-05 14:44:03 +00:00
package webfilesystem
import (
"errors"
"go.mongodb.org/mongo-driver/bson/primitive"
)
func (fs *WebFileSystem) NewRead(filePath string) (*WebFSFile, error) {
splittedPath := fs.SplitPath(filePath)
2023-05-05 22:50:57 +00:00
read := ReadStruct{
2023-05-05 14:44:03 +00:00
File: &WebFSFile{},
Filter: primitive.M{
"name": splittedPath[len(splittedPath)-1],
},
}
2023-05-05 22:50:57 +00:00
fileRaw, err := fs.ReadMongo(read)
2023-05-05 14:44:03 +00:00
if err != nil {
return nil, err
}
file := castToFile(fileRaw)
return file, nil
}
func (fs *WebFileSystem) NewList(dirPath string) ([]*WebFSFile, error) {
dirFile, err := fs.NewRead(dirPath)
if err != nil {
return nil, err
}
if dirFile.Type != "directory" {
return nil, errors.New("file is not a directory")
}
2023-05-05 18:32:41 +00:00
fileData, err := castToDirectoryData(dirFile.Data)
2023-05-05 14:44:03 +00:00
if err != nil {
return nil, err
}
files := []*WebFSFile{}
for _, child := range fileData.Children {
file, err := fs.ReadByObjectID(child)
if err != nil {
println(err.Error())
continue
}
files = append(files, file)
}
return files, nil
}
2023-05-05 18:32:41 +00:00
func (fs *WebFileSystem) WriteFile(file *WebFSFile, parentPath string) error {
2023-05-05 14:44:03 +00:00
fs.writeMongo(*file, parentPath)
return errors.New("Not ready yet")
}