File Deleting and new decoding

This commit is contained in:
cyber-dream 2023-05-05 21:32:41 +03:00
parent 2197356dcc
commit 5736b8de31
7 changed files with 217 additions and 259 deletions

View File

@ -49,7 +49,7 @@ func (f *FinderApplication) RenderContextMenu(context string, data string) gin.H
{ {
{ {
Label: "New Directory", Label: "New Directory",
Action: strings.Join([]string{"newDir"}[:], ","), Action: strings.Join([]string{"createDir"}[:], ","),
}, },
}, },
} }

View File

@ -155,17 +155,7 @@ class Finder{
case "blog-page": case "blog-page":
WebDesktopEnvironment.Open("blog-viewer", [`${this.path}/${fileName}`]) WebDesktopEnvironment.Open("blog-viewer", [`${this.path}/${fileName}`])
break break
case "deleteFile":
fetch(`/fs/delete` + new URLSearchParams({
path: `${this.path}/${fileName}` //FIXME
}))
.then((response) => {
console.log(response.status)
})
.catch((error) => {
WebDesktopEnvironment.Alert(error);
})
break
// case "app": // case "app":
// //TODO get real id // //TODO get real id
// WebDesktopEnvironment.Open("personal-properties", []) // WebDesktopEnvironment.Open("personal-properties", [])
@ -213,7 +203,33 @@ class Finder{
overlay.addEventListener('click',(event) => { overlay.addEventListener('click',(event) => {
if (event.target.classList.contains("Row")){ //TODO add uuid id to rows to more accurate checks?? if (event.target.classList.contains("Row")){ //TODO add uuid id to rows to more accurate checks??
let fileType = target.getAttribute("fileType")
switch (event.target.children[0].getAttribute("action")) { switch (event.target.children[0].getAttribute("action")) {
case "createDir":
fetch(`/fs/createDir?` + new URLSearchParams({
path: `${this.path}/New Directory`
}))
.then((response) => {
console.log(response.status)
})
.catch((error) => {
WebDesktopEnvironment.Alert(error);
})
break
case "deleteFile":
let fileName = target.getAttribute("name")
console.log(fileName)
// break
fetch(`/fs/delete?` + new URLSearchParams({
path: `${this.path}/${fileName}` //FIXME
}))
.then((response) => {
console.log(response.status)
})
.catch((error) => {
WebDesktopEnvironment.Alert(error);
})
break
default: default:
break; break;
} }

View File

@ -1,7 +1,11 @@
package webfilesystem package webfilesystem
import ( import (
"context"
"strconv"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
) )
@ -10,22 +14,66 @@ type DirectoryData struct {
Children []primitive.ObjectID `bson:"children"` Children []primitive.ObjectID `bson:"children"`
} }
// type Directory struct { func (fs *WebFileSystem) NewCreateDirectory(dirPath string) (primitive.ObjectID, error) {
// Header Header `bson:"header"` splittedPath := fs.SplitPath(dirPath)
// Data DirectoryData `bson:"data"` parentPath := fs.GetParentPath(dirPath)
// }
// func (d *Directory) Get() interface{} { parentDirRaw, err := fs.NewRead(parentPath)
// return d if err != nil {
// } return primitive.NilObjectID, err
}
newDir := WebFSFile{
MongoId: primitive.NewObjectID(),
Name: splittedPath[len(splittedPath)-1],
Type: "directory",
Data: DirectoryData{
Parent: parentDirRaw.MongoId,
Children: []primitive.ObjectID{},
},
Icon: "",
}
objectId, err := fs.writeMongo(newDir, parentPath)
if err != nil {
return primitive.NilObjectID, err
}
return objectId, nil
}
func (fs *WebFileSystem) validateDir(dir *WebFSFile) error {
kek := dir.Data.(primitive.D).Map()["children"].(primitive.A)
_ = kek
children := []primitive.ObjectID{}
counter := 0
for _, v := range kek {
_, err := fs.ReadByObjectID(v.(primitive.ObjectID))
if err != nil {
counter++
} else {
children = append(children, v.(primitive.ObjectID))
}
}
if counter > 0 {
println(dir.Name + " broken iDs: " + strconv.Itoa(counter))
_, err := fs.webfsCollection.UpdateByID(context.Background(), dir.MongoId, bson.M{"$set": bson.M{"data.children": children}})
if err != nil {
println(err.Error())
return err
}
}
return nil
}
func castToFile(raw *interface{}) *WebFSFile { func castToFile(raw *interface{}) *WebFSFile {
var dirPtr interface{} = *raw var dirPtr interface{} = *raw
return dirPtr.(*WebFSFile) return dirPtr.(*WebFSFile)
} }
func castToDirectory(data interface{}) (*FolderData, error) { func castToDirectoryData(data interface{}) (*DirectoryData, error) {
dirData := FolderData{} dirData := DirectoryData{}
err := mapstructure.Decode(data.(primitive.D).Map(), &dirData) err := mapstructure.Decode(data.(primitive.D).Map(), &dirData)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -29,7 +29,7 @@ func (fs *WebFileSystem) UploadFile(realFilepath string, path string) error {
Data: file, Data: file,
Icon: "", Icon: "",
} }
err := fs.CreateFile(&newFile, path) err := fs.WriteFile(&newFile, path)
return err return err
case "png": case "png":
newFile := WebFSFile{ newFile := WebFSFile{
@ -38,7 +38,7 @@ func (fs *WebFileSystem) UploadFile(realFilepath string, path string) error {
Type: "png", Type: "png",
Data: file, Data: file,
} }
err := fs.CreateFile(&newFile, path) err := fs.WriteFile(&newFile, path)
return err return err
default: default:
return errors.New("this filetype not allowed") return errors.New("this filetype not allowed")
@ -58,7 +58,7 @@ func (fs *WebFileSystem) UploadBinaryFile(file []byte, fileName string, path str
Data: file, Data: file,
Icon: "", Icon: "",
} }
err := fs.CreateFile(&newFile, path) err := fs.WriteFile(&newFile, path)
return err return err
case "png": case "png":
newFile := WebFSFile{ newFile := WebFSFile{
@ -67,7 +67,7 @@ func (fs *WebFileSystem) UploadBinaryFile(file []byte, fileName string, path str
Type: "png", Type: "png",
Data: file, Data: file,
} }
err := fs.CreateFile(&newFile, path) err := fs.WriteFile(&newFile, path)
return err return err
default: default:
return errors.New("this filetype not allowed") return errors.New("this filetype not allowed")

View File

@ -2,6 +2,7 @@ package webfilesystem
import ( import (
"context" "context"
"errors"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
) )
@ -35,3 +36,59 @@ func (fs *WebFileSystem) writeMongo(file WebFSFile, parentPath string) (primitiv
return fileId, nil return fileId, nil
} }
func (fs *WebFileSystem) removeMongo(filePath string) error {
file, err := fs.NewRead(filePath)
if err != nil {
return err
}
if file.MongoId == primitive.NilObjectID {
return errors.New("TODO") //TODO
}
filter := primitive.M{
"_id": file.MongoId,
}
res, err := fs.webfsCollection.DeleteOne(fs.ctx, filter)
if err != nil {
return err
}
if res.DeletedCount < 1 {
return errors.New("no file removed")
}
return nil
}
func (fs *WebFileSystem) Validate() error {
filter := primitive.D{
{
Key: "type",
Value: "directory",
},
}
cur, err := fs.webfsCollection.Find(context.Background(), filter)
if err != nil {
return err
}
defer cur.Close(context.Background())
directories := []*WebFSFile{}
for cur.Next(context.Background()) {
dir := &WebFSFile{}
err = cur.Decode(dir)
if err != nil {
println(err.Error())
return err
}
directories = append(directories, dir)
}
for _, d := range directories {
fs.validateDir(d)
}
return nil
}

View File

@ -24,33 +24,6 @@ func (fs *WebFileSystem) NewRead(filePath string) (*WebFSFile, error) {
return file, nil return file, nil
} }
func (fs *WebFileSystem) NewCreateDirectory(dirPath string) (primitive.ObjectID, error) {
splittedPath := fs.SplitPath(dirPath)
parentPath := fs.GetParentPath(dirPath)
parentDirRaw, err := fs.NewRead(parentPath)
if err != nil {
return primitive.NilObjectID, err
}
newDir := WebFSFile{
MongoId: primitive.NewObjectID(),
Name: splittedPath[len(splittedPath)-1],
Type: "directory",
Data: FolderData{
Parent: parentDirRaw.MongoId,
Children: []primitive.ObjectID{},
},
Icon: "",
}
objectId, err := fs.writeMongo(newDir, parentPath)
if err != nil {
return primitive.NilObjectID, err
}
return objectId, nil
}
func (fs *WebFileSystem) NewList(dirPath string) ([]*WebFSFile, error) { func (fs *WebFileSystem) NewList(dirPath string) ([]*WebFSFile, error) {
dirFile, err := fs.NewRead(dirPath) dirFile, err := fs.NewRead(dirPath)
if err != nil { if err != nil {
@ -60,7 +33,7 @@ func (fs *WebFileSystem) NewList(dirPath string) ([]*WebFSFile, error) {
return nil, errors.New("file is not a directory") return nil, errors.New("file is not a directory")
} }
fileData, err := castToDirectory(dirFile.Data) fileData, err := castToDirectoryData(dirFile.Data)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -77,7 +50,7 @@ func (fs *WebFileSystem) NewList(dirPath string) ([]*WebFSFile, error) {
return files, nil return files, nil
} }
func (fs *WebFileSystem) CreateFile(file *WebFSFile, parentPath string) error { func (fs *WebFileSystem) WriteFile(file *WebFSFile, parentPath string) error {
fs.writeMongo(*file, parentPath) fs.writeMongo(*file, parentPath)
return errors.New("Not ready yet") return errors.New("Not ready yet")
} }

View File

@ -2,8 +2,6 @@ package webfilesystem
import ( import (
"context" "context"
"errors"
"strconv"
"strings" "strings"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
@ -14,8 +12,7 @@ import (
type WebFileSystem struct { type WebFileSystem struct {
webfsCollection *mongo.Collection webfsCollection *mongo.Collection
// folders []*Folder ctx context.Context
ctx context.Context
} }
func NewWebFileSystem(mongoClient *mongo.Client, dBName string, fsCollectionName string) *WebFileSystem { func NewWebFileSystem(mongoClient *mongo.Client, dBName string, fsCollectionName string) *WebFileSystem {
@ -25,18 +22,6 @@ func NewWebFileSystem(mongoClient *mongo.Client, dBName string, fsCollectionName
} }
} }
// func (fs *WebFileSystem) Read(path string) (*WebFSFile, error) {
// splittedPath := fs.SplitPath(path)
// filter := primitive.D{
// {
// Key: "name",
// Value: splittedPath[len(splittedPath)-1],
// },
// }
// file, err := fs.findFileInMongo(filter)
// return file, err
// }
func (fs *WebFileSystem) ReadByObjectID(objectId primitive.ObjectID) (*WebFSFile, error) { func (fs *WebFileSystem) ReadByObjectID(objectId primitive.ObjectID) (*WebFSFile, error) {
filter := primitive.D{ filter := primitive.D{
{ {
@ -44,10 +29,12 @@ func (fs *WebFileSystem) ReadByObjectID(objectId primitive.ObjectID) (*WebFSFile
Value: objectId, Value: objectId,
}, },
} }
//TODO to readMongo()
file, err := fs.findFileInMongo(filter) file, err := fs.findFileInMongo(filter)
return file, err return file, err
} }
// Deprecated
func (fs *WebFileSystem) findFileInMongo(filter primitive.D) (*WebFSFile, error) { func (fs *WebFileSystem) findFileInMongo(filter primitive.D) (*WebFSFile, error) {
res := fs.webfsCollection.FindOne(context.Background(), &filter) res := fs.webfsCollection.FindOne(context.Background(), &filter)
file := WebFSFile{} file := WebFSFile{}
@ -58,94 +45,6 @@ func (fs *WebFileSystem) findFileInMongo(filter primitive.D) (*WebFSFile, error)
return &file, nil return &file, nil
} }
// func (fs *WebFileSystem) List(path string) ([]*WebFSFile, error) {
// // dirFile, err := fs.Read(fs.GetParentPath(path))
// dirFile, err := fs.Read(path)
// if err != nil {
// return nil, err
// }
// if dirFile.Type != "directory" {
// return nil, errors.New("file is not a directory")
// }
// fileData := FolderData{}
// err = mapstructure.Decode(dirFile.Data.(primitive.D).Map(), &fileData)
// 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
// }
// func (fs *WebFileSystem) CreateDirectory(path string) error {
// splittedpath := fs.SplitPath(path)
// parentPath := fs.GetParentPath(path)
// parentDir, err := fs.Read(parentPath)
// if err != nil {
// return err
// }
// directory := WebFSFile{
// MongoId: primitive.NewObjectID(),
// Name: splittedpath[len(splittedpath)-1],
// Type: "directory",
// Data: FolderData{
// Parent: parentDir.MongoId,
// Children: []primitive.ObjectID{},
// },
// }
// fs.CreateFile(&directory, parentPath)
// // res, err := fs.webfsCollection.InsertOne(context.Background(), &directory) //TODO
// // if err != nil {
// // return err
// // }
// // fileId := fs.castInsertId(res)
// // fs.insertFileToDirectory(fileId, parentDir.MongoId)
// return nil
// }
// func (fs *WebFileSystem) UpdateFile(filePath string, update primitive.M) error {
// file, err := fs.Read(filePath)
// if err != nil {
// return err
// }
// if file.MongoId.IsZero() {
// return errors.New("mongo id is zero")
// }
// filter := primitive.M{
// "_id": file.MongoId,
// }
// _, err = fs.webfsCollection.UpdateOne(context.Background(), filter, primitive.M{"$set": update})
// if err != nil {
// return err
// }
// return nil
// }
// func (fs *WebFileSystem) CreateFile(file *WebFSFile, parentPath string) error {
// //TODO Check file existance
// parentDir, err := fs.Read(parentPath)
// if err != nil {
// return err
// }
// res, err := fs.webfsCollection.InsertOne(context.Background(), &file)
// if err != nil {
// return err
// }
// _ = parentDir
// fileId := fs.castInsertId(res)
// fs.insertFileToDirectory(fileId, parentDir.MongoId)
// return nil
// }
func (fs *WebFileSystem) castInsertId(res *mongo.InsertOneResult) primitive.ObjectID { func (fs *WebFileSystem) castInsertId(res *mongo.InsertOneResult) primitive.ObjectID {
return res.InsertedID.(primitive.ObjectID) return res.InsertedID.(primitive.ObjectID)
} }
@ -156,7 +55,7 @@ func (fs *WebFileSystem) insertFileToDirectory(fileId primitive.ObjectID, direct
return err return err
} }
//TODO check if file exist //TODO check if file exist
fileData := FolderData{} fileData := DirectoryData{}
err = mapstructure.Decode(dir.Data.(primitive.D).Map(), &fileData) err = mapstructure.Decode(dir.Data.(primitive.D).Map(), &fileData)
if err != nil { if err != nil {
return err return err
@ -180,7 +79,6 @@ func (fs *WebFileSystem) SplitPath(path string) []string {
} }
func (fs *WebFileSystem) GetExtension(filename string) string { func (fs *WebFileSystem) GetExtension(filename string) string {
// extension := []string{}
splittedName := strings.Split(filename, ".") splittedName := strings.Split(filename, ".")
return splittedName[len(splittedName)-1] return splittedName[len(splittedName)-1]
@ -193,125 +91,91 @@ func (fs *WebFileSystem) GetParentPath(path string) string {
} }
func (fs *WebFileSystem) Delete(filePath string) error { func (fs *WebFileSystem) Delete(filePath string) error {
splittedPath := fs.SplitPath(filePath) // splittedPath := fs.SplitPath(filePath)
parentPath := strings.Join(splittedPath[:len(splittedPath)-1], "/") // parentPath := fs.GetParentPath(filePath)
file, err := fs.NewRead(filePath) _, err := fs.NewRead(filePath)
if err != nil { if err != nil {
return err return err
} }
parentDir, err := fs.NewRead(parentPath) err = fs.removeMongo(filePath)
if err != nil { if err != nil {
return err return err
} }
parentDir.Data.(primitive.D).Map() err = fs.Validate() //FIXME
if err != nil {
return err
}
//Delete from parent folder
// parentDir, err := fs.NewRead(parentPath)
// if err != nil {
// return err
// }
// parentDirData, err := castToDirectoryData(parentDir.Data)
// if err != nil {
// return err
// }
// newChildrenSlice := []primitive.ObjectID{}
// for i := 0; i < len(parentDirData.Children); i++ {
// if parentDirData.Children[i] != file.MongoId {
// newChildrenSlice = append(newChildrenSlice, parentDirData.Children[i])
// }
// }
// parentDirData.Children = newChildrenSlice
// parentDir.Data = parentDirData
// parentParentDir := fs.GetParentPath(parentPath)
// _, err = fs.writeMongo(*parentDir, parentParentDir)
// if err != nil {
// return err
// }
return nil
// for _, childId := range parentDirData.Children {
// // read := readStruct{
// // File: filePath,
// // Filter: primitive.M{
// // "_id": childId,
// // },
// // }
// // childFile, err := fs.ReadByObjectID(childId)
// // if err != nil {
// // println(err.Error())
// // continue
// // }
// // println(childFile.Name + " " + chil)
// if file.MongoId == childId {
// println(file.Name)
// parentDirData.Children.
// }
// }
// update:= primitive.M{ // update:= primitive.M{
// "data.children": // "data.children":
// } // }
// filter := primitive.M{} // filter := primitive.M{}
res, err := fs.webfsCollection.UpdateByID(context.Background(), parentDir.MongoId, primitive.M{"$unset": bson.M{"data.children." + file.MongoId.String(): ""}}) // res, err := fs.webfsCollection.UpdateByID(context.Background(), parentDir.MongoId, primitive.M{"$unset": bson.M{"data.children." + file.MongoId.String(): ""}})
// res, err := fs.webfsCollection.UpdateOne(context.Background(), filter, primitive.M{"$unset": bson.M{"data.children." + file.MongoId.String(): ""}}) // res, err := fs.webfsCollection.UpdateOne(context.Background(), filter, primitive.M{"$unset": bson.M{"data.children." + file.MongoId.String(): ""}})
if err != nil { // if err != nil {
return err // return err
} // }
if res.MatchedCount < 1 { // if res.MatchedCount < 1 {
return errors.New("no documents found") // return errors.New("no documents found")
} // }
return nil // return nil
}
func (fs *WebFileSystem) Validate() error {
filter := primitive.D{
{
Key: "type",
Value: "directory",
},
}
cur, err := fs.webfsCollection.Find(context.Background(), filter)
if err != nil {
return err
}
defer cur.Close(context.Background())
directories := []*WebFSFile{}
for cur.Next(context.Background()) {
dir := &WebFSFile{}
err = cur.Decode(dir)
if err != nil {
println(err.Error())
return err
}
directories = append(directories, dir)
}
for _, d := range directories {
fs.validateDir(d)
}
return nil
}
func (fs *WebFileSystem) validateDir(dir *WebFSFile) error {
kek := dir.Data.(primitive.D).Map()["children"].(primitive.A)
_ = kek
children := []primitive.ObjectID{}
counter := 0
for _, v := range kek {
_, err := fs.ReadByObjectID(v.(primitive.ObjectID))
if err != nil {
counter++
} else {
children = append(children, v.(primitive.ObjectID))
}
}
if counter > 0 {
println(dir.Name + " broken iDs: " + strconv.Itoa(counter))
_, err := fs.webfsCollection.UpdateByID(context.Background(), dir.MongoId, bson.M{"$set": bson.M{"data.children": children}})
if err != nil {
println(err.Error())
return err
}
}
return nil
} }
type WebFSFile struct { type WebFSFile struct {
MongoId primitive.ObjectID `bson:"_id" json:"-"` MongoId primitive.ObjectID `bson:"_id" json:"-"`
Name string `bson:"name" json:"name"` Name string `bson:"name" json:"name"`
Type string `bson:"type" json:"type"` Type string `bson:"type" json:"type"`
Data interface{} `bson:"data" json:"-"`
Icon string `bson:"-" json:"icon"` Icon string `bson:"-" json:"icon"`
Data interface{} `bson:"data" json:"-"`
} }
type FolderData struct {
Parent primitive.ObjectID `bson:"parent"`
Children []primitive.ObjectID `bson:"children"`
}
type File interface {
GetUuid() string
GetFileName() string
}
type Image struct {
}
type Exec struct {
WebFSFile
}
func (e *Exec) GetFileName() string {
return e.Name
}
// type WebFSFile2 interface {
// GetName() string
// GetType() string
// GetData() interface{}
// }