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)
|
|
|
|
read := readStruct{
|
|
|
|
File: &WebFSFile{},
|
|
|
|
Filter: primitive.M{
|
|
|
|
"name": splittedPath[len(splittedPath)-1],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
fileRaw, err := fs.readMongo(read)
|
|
|
|
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")
|
|
|
|
}
|