Compare commits

...

2 Commits

Author SHA1 Message Date
c95501dbb7 Working img viewer 2023-05-05 23:31:42 +03:00
5736b8de31 File Deleting and new decoding 2023-05-05 21:32:41 +03:00
11 changed files with 268 additions and 334 deletions

View File

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

View File

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

View File

@ -155,22 +155,13 @@ class Finder{
case "blog-page":
WebDesktopEnvironment.Open("blog-viewer", [`${this.path}/${fileName}`])
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":
// //TODO get real id
// WebDesktopEnvironment.Open("personal-properties", [])
// break;
case "base64img":
case "jpeg":
case "png":
WebDesktopEnvironment.Open("img-viewer", [this.path + "/" + fileName])
break;
default:
@ -213,7 +204,42 @@ class Finder{
overlay.addEventListener('click',(event) => {
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")) {
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:
break;
}

View File

@ -193,7 +193,7 @@ class WebDesktopEnvironment{
* @param {string} alertText
*/
static CreateAlertWindow(alertText){
console.log("alertWinfdo")
// console.log("alertWinfdo")
let newWindow = document.createElement("div")
newWindow.setAttribute("class", "WindowFrameless")
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) {
url := location.Get(ctx)
_ = url
// _ = url
path := ctx.Query("path")
if path == "" {
ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct

View File

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

View File

@ -1,7 +1,11 @@
package webfilesystem
import (
"context"
"strconv"
"github.com/mitchellh/mapstructure"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
)
@ -10,22 +14,66 @@ type DirectoryData struct {
Children []primitive.ObjectID `bson:"children"`
}
// type Directory struct {
// Header Header `bson:"header"`
// Data DirectoryData `bson:"data"`
// }
func (fs *WebFileSystem) NewCreateDirectory(dirPath string) (primitive.ObjectID, error) {
splittedPath := fs.SplitPath(dirPath)
parentPath := fs.GetParentPath(dirPath)
// func (d *Directory) Get() interface{} {
// return d
// }
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: 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 {
var dirPtr interface{} = *raw
return dirPtr.(*WebFSFile)
}
func castToDirectory(data interface{}) (*FolderData, error) {
dirData := FolderData{}
func castToDirectoryData(data interface{}) (*DirectoryData, error) {
dirData := DirectoryData{}
err := mapstructure.Decode(data.(primitive.D).Map(), &dirData)
if err != nil {
return nil, err

View File

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

View File

@ -2,6 +2,7 @@ package webfilesystem
import (
"context"
"errors"
"go.mongodb.org/mongo-driver/bson/primitive"
)
@ -35,3 +36,59 @@ func (fs *WebFileSystem) writeMongo(file WebFSFile, parentPath string) (primitiv
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
}
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) {
dirFile, err := fs.NewRead(dirPath)
if err != nil {
@ -60,7 +33,7 @@ func (fs *WebFileSystem) NewList(dirPath string) ([]*WebFSFile, error) {
return nil, errors.New("file is not a directory")
}
fileData, err := castToDirectory(dirFile.Data)
fileData, err := castToDirectoryData(dirFile.Data)
if err != nil {
return nil, err
}
@ -77,7 +50,7 @@ func (fs *WebFileSystem) NewList(dirPath string) ([]*WebFSFile, error) {
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)
return errors.New("Not ready yet")
}

View File

@ -2,8 +2,6 @@ package webfilesystem
import (
"context"
"errors"
"strconv"
"strings"
"github.com/mitchellh/mapstructure"
@ -14,8 +12,7 @@ import (
type WebFileSystem struct {
webfsCollection *mongo.Collection
// folders []*Folder
ctx context.Context
ctx context.Context
}
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) {
filter := primitive.D{
{
@ -44,10 +29,12 @@ func (fs *WebFileSystem) ReadByObjectID(objectId primitive.ObjectID) (*WebFSFile
Value: objectId,
},
}
//TODO to readMongo()
file, err := fs.findFileInMongo(filter)
return file, err
}
// Deprecated
func (fs *WebFileSystem) findFileInMongo(filter primitive.D) (*WebFSFile, error) {
res := fs.webfsCollection.FindOne(context.Background(), &filter)
file := WebFSFile{}
@ -58,94 +45,6 @@ func (fs *WebFileSystem) findFileInMongo(filter primitive.D) (*WebFSFile, error)
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 {
return res.InsertedID.(primitive.ObjectID)
}
@ -156,7 +55,7 @@ func (fs *WebFileSystem) insertFileToDirectory(fileId primitive.ObjectID, direct
return err
}
//TODO check if file exist
fileData := FolderData{}
fileData := DirectoryData{}
err = mapstructure.Decode(dir.Data.(primitive.D).Map(), &fileData)
if err != nil {
return err
@ -180,7 +79,6 @@ func (fs *WebFileSystem) SplitPath(path string) []string {
}
func (fs *WebFileSystem) GetExtension(filename string) string {
// extension := []string{}
splittedName := strings.Split(filename, ".")
return splittedName[len(splittedName)-1]
@ -193,125 +91,91 @@ func (fs *WebFileSystem) GetParentPath(path string) string {
}
func (fs *WebFileSystem) Delete(filePath string) error {
splittedPath := fs.SplitPath(filePath)
parentPath := strings.Join(splittedPath[:len(splittedPath)-1], "/")
// splittedPath := fs.SplitPath(filePath)
// parentPath := fs.GetParentPath(filePath)
file, err := fs.NewRead(filePath)
_, err := fs.NewRead(filePath)
if err != nil {
return err
}
parentDir, err := fs.NewRead(parentPath)
err = fs.removeMongo(filePath)
if err != nil {
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{
// "data.children":
// }
// 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(): ""}})
if err != nil {
return err
}
if res.MatchedCount < 1 {
return errors.New("no documents found")
}
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
// if err != nil {
// return err
// }
// if res.MatchedCount < 1 {
// return errors.New("no documents found")
// }
// return nil
}
type WebFSFile struct {
MongoId primitive.ObjectID `bson:"_id" json:"-"`
Name string `bson:"name" json:"name"`
Type string `bson:"type" json:"type"`
Data interface{} `bson:"data" json:"-"`
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{}
// }