Compare commits

..

No commits in common. "c95501dbb738c73d85d3d57d04bb9be77e9f33c1" and "2197356dcc162d5ad250c7f33ae0556ed62efdcf" have entirely different histories.

11 changed files with 334 additions and 268 deletions

View File

@ -4,6 +4,7 @@ import (
"personalwebsite/apps" "personalwebsite/apps"
"personalwebsite/wde" "personalwebsite/wde"
"personalwebsite/webfilesystem" "personalwebsite/webfilesystem"
"strings"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -35,31 +36,71 @@ func (f *FinderApplication) Render(isMobile bool) gin.H {
} }
func (f *FinderApplication) RenderContextMenu(context string, data string) gin.H { func (f *FinderApplication) RenderContextMenu(context string, data string) gin.H {
islands := [][]wde.ContexMenuRow{} islands := [][]wde.ContexMenuRow{} //FIXME
islands = append(islands, []wde.ContexMenuRow{
{Label: "Get Info", Action: "getInfo"},
})
if context == "FileTileView" {
return gin.H{
"Islands": islands,
}
}
islands = append(islands, []wde.ContexMenuRow{
{Label: "Delete File", Action: "deleteFile"},
// {Label: "Get Info", Action: "getInfo"},
})
switch context { switch context {
case "directory": case "FileTileView":
islands = [][]wde.ContexMenuRow{
default: {
islands = append(islands, []wde.ContexMenuRow{ {
{Label: "temp Menu 1", Action: ""}, Label: "Get Info",
{Label: "temp Menu 2", Action: ""}, Action: strings.Join([]string{"getInfo"}[:], ","),
}) },
},
{
{
Label: "New Directory",
Action: strings.Join([]string{"newDir"}[:], ","),
},
},
} }
case "directory":
islands = [][]wde.ContexMenuRow{
{
{
Label: "Test",
Action: strings.Join([]string{""}[:], ","),
},
{
Label: "Delete",
Action: strings.Join([]string{""}[:], ","),
},
},
{
{
Label: "Get Info",
Action: strings.Join([]string{""}[:], ","),
},
},
}
default:
islands = [][]wde.ContexMenuRow{
{
{
Label: "temp Menu 1",
Action: strings.Join([]string{""}[:], ","),
},
{
Label: "temp Menu 2",
Action: strings.Join([]string{""}[:], ","),
},
{
Label: "temp Menu 3",
Action: strings.Join([]string{""}[:], ","),
},
{
Label: "temp Menu 4",
Action: strings.Join([]string{""}[:], ","),
},
},
}
}
islands = append(islands, []wde.ContexMenuRow{
{
Label: "Delete File",
Action: strings.Join([]string{"deleteFile"}[:], ";"),
},
})
return gin.H{ return gin.H{
"Islands": islands, "Islands": islands,

View File

@ -3,6 +3,7 @@ package imgviewer
import ( import (
"net/http" "net/http"
websiteapp "personalwebsite/apps" websiteapp "personalwebsite/apps"
"personalwebsite/libs"
"personalwebsite/webfilesystem" "personalwebsite/webfilesystem"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -53,25 +54,17 @@ func (p *ImgViewerApp) GetId() string {
return p.manifest.AppId return p.manifest.AppId
} }
func (p *ImgViewerApp) Render(filePath string, isMobile bool) (gin.H, error) { func (p *ImgViewerApp) Render(path string, isMobile bool) (gin.H, error) {
// file, err := p.fs.NewRead(filePath) img, err := p.fs.NewRead(path)
// if err != nil { if err != nil {
// return nil, err return nil, err
// } }
// println(file.Data.(primitive.Binary).Data) data, err := libs.ReadImage(img)
if err != nil {
// img, err := p.fs.NewRead(path) return nil, err
// if err != nil { }
// return nil, err
// }
// data, err := libs.ReadImage(img)
// if err != nil {
// return nil, err
// }
// url := location.Get(ctx)
return gin.H{ return gin.H{
"imgUrl": "/system/libs/img/get?path=" + filePath, "header": data.Header,
// "header": data.Header,
// "base64": data.Base64, // "base64": data.Base64,
}, nil }, nil
} }

View File

@ -155,13 +155,22 @@ 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", [])
// break; // break;
case "jpeg": case "base64img":
case "png":
WebDesktopEnvironment.Open("img-viewer", [this.path + "/" + fileName]) WebDesktopEnvironment.Open("img-viewer", [this.path + "/" + fileName])
break; break;
default: default:
@ -204,42 +213,7 @@ 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")
let fileName = target.getAttribute("name")
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)
if (response.status == 200){
this.OpenDir(this.path)
}
})
.catch((error) => {
WebDesktopEnvironment.Alert(error);
})
break
case "deleteFile":
console.log(fileName)
// break
fetch(`/fs/delete?` + new URLSearchParams({
path: `${this.path}/${fileName}` //FIXME
}))
.then((response) => {
console.log(response.status)
if (response.status == 200){
this.OpenDir(this.path)
}
})
.catch((error) => {
WebDesktopEnvironment.Alert(error);
})
break
case "getInfo":
// console.log("Open Properties")
WebDesktopEnvironment.Open("props-viewer", `${this.path}/${fileName}`)
default: default:
break; break;
} }

View File

@ -193,7 +193,7 @@ class WebDesktopEnvironment{
* @param {string} alertText * @param {string} alertText
*/ */
static CreateAlertWindow(alertText){ static CreateAlertWindow(alertText){
// console.log("alertWinfdo") console.log("alertWinfdo")
let newWindow = document.createElement("div") let newWindow = document.createElement("div")
newWindow.setAttribute("class", "WindowFrameless") newWindow.setAttribute("class", "WindowFrameless")
newWindow.setAttribute("windowId", "SuperUniqUUID") //TODO: newWindow.setAttribute("windowId", "SuperUniqUUID") //TODO:

View File

@ -21,7 +21,7 @@ func Route(route *gin.RouterGroup, wde *wde.WDE) {
{ {
widgets.GET("/file-tile-view", func(ctx *gin.Context) { widgets.GET("/file-tile-view", func(ctx *gin.Context) {
url := location.Get(ctx) url := location.Get(ctx)
// _ = url _ = url
path := ctx.Query("path") path := ctx.Query("path")
if path == "" { if path == "" {
ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct

View File

@ -12,7 +12,7 @@
Toolbar Toolbar
</div> --> </div> -->
<img class="Img-Viewer-Picture-Container"src="{{.imgUrl}}"> <img class="Img-Viewer-Picture-Container"src="data:{{.header}},{{.base64}}">
</div> </div>
{{ end }} {{ end }}

View File

@ -1,11 +1,7 @@
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"
) )
@ -14,66 +10,22 @@ type DirectoryData struct {
Children []primitive.ObjectID `bson:"children"` Children []primitive.ObjectID `bson:"children"`
} }
func (fs *WebFileSystem) NewCreateDirectory(dirPath string) (primitive.ObjectID, error) { // type Directory struct {
splittedPath := fs.SplitPath(dirPath) // Header Header `bson:"header"`
parentPath := fs.GetParentPath(dirPath) // Data DirectoryData `bson:"data"`
// }
parentDirRaw, err := fs.NewRead(parentPath) // func (d *Directory) Get() interface{} {
if err != nil { // return d
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 castToDirectoryData(data interface{}) (*DirectoryData, error) { func castToDirectory(data interface{}) (*FolderData, error) {
dirData := DirectoryData{} dirData := FolderData{}
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.WriteFile(&newFile, path) err := fs.CreateFile(&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.WriteFile(&newFile, path) err := fs.CreateFile(&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.WriteFile(&newFile, path) err := fs.CreateFile(&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.WriteFile(&newFile, path) err := fs.CreateFile(&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,7 +2,6 @@ package webfilesystem
import ( import (
"context" "context"
"errors"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
) )
@ -36,59 +35,3 @@ 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,6 +24,33 @@ 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 {
@ -33,7 +60,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 := castToDirectoryData(dirFile.Data) fileData, err := castToDirectory(dirFile.Data)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -50,7 +77,7 @@ func (fs *WebFileSystem) NewList(dirPath string) ([]*WebFSFile, error) {
return files, nil return files, nil
} }
func (fs *WebFileSystem) WriteFile(file *WebFSFile, parentPath string) error { func (fs *WebFileSystem) CreateFile(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,6 +2,8 @@ package webfilesystem
import ( import (
"context" "context"
"errors"
"strconv"
"strings" "strings"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
@ -12,6 +14,7 @@ import (
type WebFileSystem struct { type WebFileSystem struct {
webfsCollection *mongo.Collection webfsCollection *mongo.Collection
// folders []*Folder
ctx context.Context ctx context.Context
} }
@ -22,6 +25,18 @@ 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{
{ {
@ -29,12 +44,10 @@ 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{}
@ -45,6 +58,94 @@ 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)
} }
@ -55,7 +156,7 @@ func (fs *WebFileSystem) insertFileToDirectory(fileId primitive.ObjectID, direct
return err return err
} }
//TODO check if file exist //TODO check if file exist
fileData := DirectoryData{} fileData := FolderData{}
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
@ -79,6 +180,7 @@ 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]
@ -91,91 +193,125 @@ 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 := fs.GetParentPath(filePath) parentPath := strings.Join(splittedPath[:len(splittedPath)-1], "/")
_, err := fs.NewRead(filePath) file, err := fs.NewRead(filePath)
if err != nil { if err != nil {
return err return err
} }
err = fs.removeMongo(filePath) parentDir, err := fs.NewRead(parentPath)
if err != nil { if err != nil {
return err return err
} }
err = fs.Validate() //FIXME parentDir.Data.(primitive.D).Map()
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"`
Icon string `bson:"-" json:"icon"`
Data interface{} `bson:"data" json:"-"` Data interface{} `bson:"data" json:"-"`
Icon string `bson:"-" json:"icon"`
} }
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{}
// }