massive commit for safe refactoring

This commit is contained in:
cyber-dream 2023-05-07 03:14:30 +03:00
parent 1beba0f0ee
commit 6c3bc32b59
19 changed files with 710 additions and 97 deletions

2
.env
View File

@ -1,3 +1,3 @@
MONGO_CONNECT=mongodb://localhost:27017
DATABASE=personal-website
COLLECTION_WEBFS=webfs
COLLECTION_WEBFS=webfs2

View File

@ -93,7 +93,7 @@ func (b *BlogViewerApplication) Route(route *gin.RouterGroup) {
// }
func (b *BlogViewerApplication) Render(path string, isMobile bool) (gin.H, error) {
file, err := b.fs.NewRead(path)
file, err := b.fs.NewReadDeprecated(path)
if err != nil {
println(err.Error())
return nil, err

View File

@ -66,7 +66,7 @@ func (f *FinderApplication) RenderContextMenu(context string, data string) gin.H
}
func (f *FinderApplication) RenderProps(filePath string) gin.H {
file, err := f.fs.NewRead(filePath)
file, err := f.fs.NewReadDeprecated(filePath)
if err != nil {
return nil
}

View File

@ -52,7 +52,7 @@ func (p *PersonalPropertiesApp) GetId() string {
}
func (p *PersonalPropertiesApp) Render() (gin.H, error) {
propsFile, err := p.fs.NewRead("/home/user/About Me.props")
propsFile, err := p.fs.NewReadDeprecated("/home/user/About Me.props")
if err != nil {
println(err.Error())
return nil, err
@ -91,8 +91,9 @@ func (p *PersonalPropertiesApp) Render() (gin.H, error) {
}
func castToPersPropsData(fileData interface{}) (*PropertiesFileData, error) {
// kek := fileData.(primitive.D).Map()["name"]
propsData := PropertiesFileData{
Header.Name: fileData.(primitive.D).Map()["name"].(string),
// Header.Name: ,
}
err := mapstructure.Decode(fileData.(primitive.D).Map(), &propsData)
if err != nil {

View File

@ -1,12 +1,10 @@
package libs
import (
"errors"
"net/http"
"personalwebsite/webfilesystem"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type ImagLib struct {
@ -19,40 +17,26 @@ func NewImgLib(webfs *webfilesystem.WebFileSystem) *ImagLib {
}
}
func ReadImage(img *webfilesystem.WebFSFile) (*Img, error) {
data, ok := img.Data.(primitive.D).Map()["srcdata"]
if !ok {
return nil, errors.New("error in file decoding")
}
bin := data.(primitive.Binary).Data
min32Data, ok := img.Data.(primitive.D).Map()["min32data"]
if !ok {
return nil, errors.New("error in file decoding")
}
min32Bin := min32Data.(primitive.Binary).Data
return &Img{
// Header: header,
Data: bin,
Miniature32: min32Bin,
}, nil
}
func (l *ImagLib) Route(route *gin.RouterGroup) {
route.GET("get", func(ctx *gin.Context) {
path := ctx.Query("path")
if path == "" {
ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct
ctx.String(http.StatusBadRequest, "TODO") //TODO json error struct
return
}
file, err := l.fs.NewRead(path)
fileId, err := l.fs.V3FindFile(path)
if err != nil {
ctx.String(http.StatusInternalServerError, "TODO") //TODO
ctx.Status(http.StatusInternalServerError)
return
}
imgData := Img{}
_, err = l.fs.V3Read(fileId, &imgData)
if err != nil {
ctx.Status(http.StatusInternalServerError)
}
data := file.Data.(primitive.Binary)
ctx.Data(http.StatusOK, "image/jpeg", data.Data)
ctx.Data(http.StatusOK, "image/jpeg", imgData.Bin)
})
}
@ -76,7 +60,7 @@ type Base64Img struct {
}
type Img struct {
Header string `bson:"header"`
Data []byte `bson:"srcdata"`
Miniature32 []byte `bson:"min32data"`
Header string `bson:"header"`
Bin []byte `bson:"bin"`
// BinMin32 []byte `bson:"binmin32"`
}

View File

@ -68,16 +68,16 @@ class Finder{
FileUploading(filesList){
let formData = new FormData()
// console.log(filesList)
console.log(this.path)
for (let i = 0; i < filesList.length; i++) {
const element = filesList[i];
formData.append("file", element.getAsFile())
console.log(formData)
// console.log(formData)
}
// console.log(formData)
// formData.append("photo", photo);
fetch('/fs/upload/?' + new URLSearchParams({
path: '/home/user/',
parentPath: this.path,
}),
{
method: "POST",
@ -125,15 +125,16 @@ class Finder{
* @param {string} path
*/
OpenDir(path){
this.path = path
this.fileView.OpenFolder(path)
}
/**
*/
OpenNewDir(){
WebDesktopEnvironment.Open("finder", [this.path])
}
// /**
// */
// OpenNewDir(){
// WebDesktopEnvironment.Open("finder", [this.path])
// }
/**
* @param {MouseEvent} event

View File

@ -4,9 +4,9 @@ document.addEventListener('DOMContentLoaded', function() {
// console.log(window.screen.width)
wde = new WebDesktopEnvironment
if (!WebDesktopEnvironment.isMobile){
// WebDesktopEnvironment.Open("finder", ["/home/user"])
WebDesktopEnvironment.Open("finder", ["/home/user"])
// WebDesktopEnvironment.Open("blog-viewer", ["/home/user/blog/test-1.blog"])
WebDesktopEnvironment.Open("personal-properties", ["kek"])
// WebDesktopEnvironment.Open("personal-properties", ["kek"])
} else {
WebDesktopEnvironment.Open("blog-viewer", ["/home/user/blog/test-1.blog"])
}

BIN
test-img/folder.png (Stored with Git LFS) Normal file

Binary file not shown.

BIN
test-img/ohno.png (Stored with Git LFS) Normal file

Binary file not shown.

View File

@ -22,7 +22,7 @@ type FilesWidget struct {
}
func (w *WDE) Render(path string) (gin.H, error) {
list, err := w.fs.NewList(path)
list, err := w.fs.NewListDeprecated(path)
if err != nil {
return nil, err
}
@ -44,10 +44,11 @@ func (w *WDE) RenderContextMenu() (gin.H, error) {
}
func (w *WDE) RenderFileTileView(directory string, host string) (gin.H, error) {
list, err := w.fs.NewList(directory)
list, err := w.fs.V3List(directory)
if err != nil {
return nil, err
}
for _, file := range list {
switch file.Type {
case "directory":

140
webfilesystem/dir3.go Normal file
View File

@ -0,0 +1,140 @@
package webfilesystem
import (
"errors"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type V3DirectoryData struct {
MongoId primitive.ObjectID `bson:"_id"`
Parent primitive.ObjectID `bson:"parent_id"`
Children []primitive.ObjectID `bson:"children_id"`
}
func (fs *WebFileSystem) V3CreateDirectory(dirPath string) (primitive.ObjectID, primitive.ObjectID, error) {
splittedPath := fs.SplitPath(dirPath)
parentPath := fs.GetParentPath(dirPath)
parentDirId, err := fs.V3FindFile(parentPath)
if err != nil {
return primitive.NilObjectID, primitive.NilObjectID, err
}
parentDirHeader, err := fs.V3Read(parentDirId, nil)
if err != nil {
return primitive.NilObjectID, primitive.NilObjectID, err
}
newDir := V3FileHeader{
MongoId: primitive.NewObjectID(),
Name: splittedPath[len(splittedPath)-1],
Type: "directory",
Icon: "",
}
newDirData := V3DirectoryData{
MongoId: primitive.NewObjectID(),
Parent: parentDirHeader.MongoId,
Children: []primitive.ObjectID{},
}
headerId, dataId, err := fs.v3WriteFileToMongo(&newDir, &newDirData)
if err != nil {
return primitive.NilObjectID, primitive.NilObjectID, err
}
err = fs.V3AppendChildToDirectory(headerId, parentDirId)
if err != nil {
return primitive.NilObjectID, primitive.NilObjectID, err
}
return headerId, dataId, nil
}
func (fs *WebFileSystem) V3AppendChildToDirectory(childId primitive.ObjectID, parentId primitive.ObjectID) error {
parentDirData := V3DirectoryData{}
parentDir, err := fs.V3Read(parentId, &parentDirData)
if err != nil {
return err
}
//TODO Check for duplicates?
parentDirData.Children = append(parentDirData.Children, childId)
err = fs.V3WriteUpdateData(parentDir, parentDirData)
if err != nil {
return err
}
return nil
}
func (fs *WebFileSystem) V3List(dirPath string) ([]*V3FileHeader, error) {
dirId, err := fs.V3FindFile(dirPath)
if err != nil {
return nil, err
}
dirData := V3DirectoryData{}
_, err = fs.V3Read(dirId, &dirData)
if err != nil {
return nil, err
}
// println(dirId.String())
// println(dirData.MongoId.String())
children := []*V3FileHeader{}
for _, childID := range dirData.Children {
childFile, err := fs.V3Read(childID, nil)
if err != nil {
// println(err.Error())
continue
}
children = append(children, childFile)
}
return children, nil
}
func (fs *WebFileSystem) V3FindFile(filePath string) (primitive.ObjectID, error) {
splittedPath := fs.SplitPath(filePath)
rootDir, _, err := fs.V3GetRootDir()
if err != nil {
return primitive.NilObjectID, err
}
// println(rootDir.Name)
lastFileID := rootDir.MongoId
for _, pathPart := range splittedPath[1:] {
// println(pathPart)
id, err := fs.v3findInChildren(pathPart, lastFileID)
if err != nil {
return primitive.NilObjectID, err
}
lastFileID = id
}
return lastFileID, nil
}
// func (fs *WebFileSystem) v3updateChildrenList(directoryID primitive.ObjectID, childrenList []primitive.ObjectID) (primitive.ObjectID, error) {
// }
func (fs *WebFileSystem) v3findInChildren(fileName string, parentDirId primitive.ObjectID) (primitive.ObjectID, error) {
parentDirData := V3DirectoryData{}
_, err := fs.V3Read(parentDirId, &parentDirData)
if err != nil {
return primitive.NilObjectID, err
}
for _, childId := range parentDirData.Children {
childFileHeader, err := fs.V3Read(childId, nil)
if err != nil {
return primitive.NilObjectID, err
}
if childFileHeader.Name == fileName {
return childId, nil
}
}
return primitive.NilObjectID, errors.New("file not found")
}

View File

@ -2,6 +2,7 @@ package webfilesystem
import (
"context"
"errors"
"strconv"
"github.com/mitchellh/mapstructure"
@ -14,11 +15,150 @@ type DirectoryData struct {
Children []primitive.ObjectID `bson:"children"`
}
//Deprecated
func (fs *WebFileSystem) List(dirPath string) ([]*OnlyHeaderFile, error) {
fileId, err := fs.FindFile(dirPath)
if err != nil {
return nil, err
}
read := ReadStruct2{
File: &DirectoryFile{},
Id: fileId,
}
fileRaw, err := fs.Read(read)
if err != nil {
return nil, err
}
var dirPtr interface{} = *fileRaw
dir := dirPtr.(*DirectoryFile)
children := []*OnlyHeaderFile{}
for _, childID := range dir.Data.Children {
read := ReadStruct2{
File: &OnlyHeaderFile{},
Id: childID,
}
childFile, err := fs.Read(read)
if err != nil {
// println(err.Error())
continue
}
var dirPtr interface{} = *childFile
children = append(children, dirPtr.(*OnlyHeaderFile))
}
return children, nil
}
//Deprecated
func (fs *WebFileSystem) FindFile(filePath string) (primitive.ObjectID, error) {
splittedPath := fs.SplitPath(filePath)
_, rootDirID, err := fs.GetRootDir()
if err != nil {
return primitive.NilObjectID, err
}
// println(rootDir.Name)
lastFileID := rootDirID
for _, pathPart := range splittedPath[1:] {
// println(pathPart)
id, err := fs.findInChildren(pathPart, lastFileID)
if err != nil {
return primitive.NilObjectID, err
}
lastFileID = id
}
return lastFileID, nil
}
//Deprecated
func (fs *WebFileSystem) findInChildren(fileName string, parentDirId primitive.ObjectID) (primitive.ObjectID, error) {
read := ReadStruct2{
File: &DirectoryFile{
FileHeader: FileHeader{},
Data: DirectoryData{},
},
Id: parentDirId,
}
parentDirRaw, err := fs.Read(read)
if err != nil {
return primitive.NilObjectID, err
}
var dirPtr interface{} = *parentDirRaw
dir := dirPtr.(*DirectoryFile)
for _, childId := range dir.Data.Children {
read := ReadStruct2{
File: OnlyHeaderFile{},
Id: childId,
}
childRaw, err := fs.Read(read)
if err != nil {
return primitive.NilObjectID, err
}
var dirPtr interface{} = *childRaw
childFileHeader := dirPtr.(*OnlyHeaderFile)
if childFileHeader.Name == fileName {
return childId, nil
}
}
return primitive.NilObjectID, errors.New("file not found")
}
//Deprecated
func (fs *WebFileSystem) IsChild(childID primitive.ObjectID, parent primitive.ObjectID) (bool, error) {
read := ReadStruct{
File: DirectoryFile{}, //TODO May be a possible problem, if parent file would be not a directory type
}
dirRaw, err := fs.ReadMongo(read)
if err != nil {
return false, err
}
var dirPtr interface{} = *dirRaw
dir := dirPtr.(*DirectoryFile)
for _, cID := range dir.Data.Children {
if cID == childID {
return true, nil
}
}
return false, nil
}
//Deprecated
func (fs *WebFileSystem) GetRootDir() (*DirectoryFile, primitive.ObjectID, error) {
filter := primitive.M{
"header.name": "/",
}
res := fs.webfsCollection.FindOne(fs.ctx, filter)
if res == nil {
return nil, primitive.NilObjectID, errors.New("TODO") //TODO
}
rootDirID := FileId{}
rootDir := DirectoryFile{}
err := res.Decode(&rootDir)
if res == nil {
return nil, primitive.NilObjectID, err
}
err = res.Decode(&rootDirID)
if res == nil {
return nil, primitive.NilObjectID, err
}
return &rootDir, rootDirID.Id, err
}
//Deprecated
func (fs *WebFileSystem) NewCreateDirectory(dirPath string) (primitive.ObjectID, error) {
splittedPath := fs.SplitPath(dirPath)
parentPath := fs.GetParentPath(dirPath)
parentDirRaw, err := fs.NewRead(parentPath)
parentDirRaw, err := fs.NewReadDeprecated(parentPath)
if err != nil {
return primitive.NilObjectID, err
}
@ -41,6 +181,50 @@ func (fs *WebFileSystem) NewCreateDirectory(dirPath string) (primitive.ObjectID,
return objectId, nil
}
func (fs *WebFileSystem) CreateDirectory(dirPath string) (primitive.ObjectID, error) {
splittedPath := fs.SplitPath(dirPath)
parentPath := fs.GetParentPath(dirPath)
parentDirID, err := fs.FindFile(parentPath)
if err != nil {
return primitive.NilObjectID, err
}
read := ReadStruct2{
File: nil,
Id: parentDirID,
}
parentDirRaw, err := fs.Read(read)
if err != nil {
return primitive.NilObjectID, err
}
var dirPtr interface{} = *parentDirRaw
parentDir := dirPtr.(*OnlyHeaderFile)
if parentDir.Type != "directory" {
return primitive.NilObjectID, errors.New("path is bad")
}
newDir := DirectoryFile{
FileHeader: FileHeader{
Name: splittedPath[len(splittedPath)-1],
Type: "",
Icon: "",
},
Data: DirectoryData{
Parent: parentDirID,
Children: []primitive.ObjectID{},
},
}
resID, err := fs.Write(newDir, dirPath, false)
if err != nil {
return primitive.NilObjectID, err
}
fs.insertFileToDirectory(resID, parentDirID)
return resID, nil
}
func (fs *WebFileSystem) validateDir(dir *WebFSFile) error {
kek := dir.Data.(primitive.D).Map()["children"].(primitive.A)
_ = kek

View File

@ -5,11 +5,13 @@ package webfilesystem
import (
"errors"
"os"
"path"
"go.mongodb.org/mongo-driver/bson/primitive"
)
func (fs *WebFileSystem) UploadFile(realFilepath string, path string) error {
func (fs *WebFileSystem) UploadFile(realFilepath string, parentPath string) error {
//TODO check is file exists
file, err := os.ReadFile(realFilepath)
if err != nil {
return err
@ -18,33 +20,43 @@ func (fs *WebFileSystem) UploadFile(realFilepath string, path string) error {
fileName := splittedPath[len(splittedPath)-1]
extension := fs.GetExtension(fileName)
newFile2 := V3FileHeader{
MongoId: primitive.NewObjectID(),
Name: fileName,
Type: "",
Icon: "",
Data: primitive.NilObjectID,
}
newFileData := V3FileBinaryData{
MongoId: primitive.NewObjectID(),
Bin: file,
}
filePath := path.Join(parentPath, fileName)
switch extension {
case "jpg":
fallthrough
case "jpeg":
newFile := WebFSFile{
MongoId: primitive.NewObjectID(),
Name: fileName,
Type: "jpeg",
Data: file,
Icon: "",
}
err := fs.WriteFile(&newFile, path)
newFile2.Type = "jpeg"
_, _, err := fs.V3Write(filePath, &newFile2, &newFileData)
return err
case "png":
newFile := WebFSFile{
MongoId: primitive.NewObjectID(),
Name: fileName,
Type: "png",
Data: file,
newFile2.Type = "png"
_, _, err := fs.V3Write(filePath, &newFile2, &newFileData)
if err != nil {
println(err.Error())
return err
}
err := fs.WriteFile(&newFile, path)
return err
return nil
default:
return errors.New("this filetype not allowed")
}
}
// Deprecated
func (fs *WebFileSystem) UploadBinaryFile(file []byte, fileName string, path string) error {
extension := fs.GetExtension(fileName)
switch extension {

View File

@ -12,6 +12,9 @@ type ReadStruct struct {
Filter interface{}
}
//TODO: Read files only by objectId, before reading file, search path by reading all parent folders
// Deprecated
func (fs *WebFileSystem) ReadMongo(read ReadStruct) (*interface{}, error) {
err := fs.webfsCollection.FindOne(fs.ctx, read.Filter).Decode(read.File)
if err != nil {
@ -20,9 +23,10 @@ func (fs *WebFileSystem) ReadMongo(read ReadStruct) (*interface{}, error) {
return &read.File, nil
}
// Deprecated
func (fs *WebFileSystem) writeMongo(file WebFSFile, parentPath string) (primitive.ObjectID, error) {
//TODO Check file existance
parentDir, err := fs.NewRead(parentPath)
parentDir, err := fs.NewReadDeprecated(parentPath)
if err != nil {
return primitive.NilObjectID, err
}
@ -38,7 +42,7 @@ func (fs *WebFileSystem) writeMongo(file WebFSFile, parentPath string) (primitiv
}
func (fs *WebFileSystem) removeMongo(filePath string) error {
file, err := fs.NewRead(filePath)
file, err := fs.NewReadDeprecated(filePath)
if err != nil {
return err
}

View File

@ -6,7 +6,8 @@ import (
"go.mongodb.org/mongo-driver/bson/primitive"
)
func (fs *WebFileSystem) NewRead(filePath string) (*WebFSFile, error) {
// Deprecated
func (fs *WebFileSystem) NewReadDeprecated(filePath string) (*WebFSFile, error) {
splittedPath := fs.SplitPath(filePath)
read := ReadStruct{
File: &WebFSFile{},
@ -24,8 +25,9 @@ func (fs *WebFileSystem) NewRead(filePath string) (*WebFSFile, error) {
return file, nil
}
func (fs *WebFileSystem) NewList(dirPath string) ([]*WebFSFile, error) {
dirFile, err := fs.NewRead(dirPath)
// Deprecated
func (fs *WebFileSystem) NewListDeprecated(dirPath string) ([]*WebFSFile, error) {
dirFile, err := fs.NewReadDeprecated(dirPath)
if err != nil {
return nil, err
}

View File

@ -0,0 +1,82 @@
package webfilesystem
import (
"context"
"errors"
"go.mongodb.org/mongo-driver/bson/primitive"
)
// Deprecated
type ReadStruct2 struct {
File interface{}
Id primitive.ObjectID
}
// Deprecated
func (fs *WebFileSystem) Read(read ReadStruct2) (*interface{}, error) {
//TODO Check nil Id
filter := primitive.M{
"_id": read.Id,
}
err := fs.webfsCollection.FindOne(fs.ctx, filter).Decode(&read.File)
return &read.File, err
}
// Deprecated
func (fs *WebFileSystem) Write(file interface{}, filePath string, force bool) (primitive.ObjectID, error) {
foundedFile, err := fs.FindFile(filePath)
if err == nil { //FIXME
return primitive.NilObjectID, errors.New("file already exists")
}
_ = foundedFile
res, err := fs.webfsCollection.InsertOne(fs.ctx, file)
if err != nil {
return primitive.NilObjectID, err
}
_ = res.InsertedID
return primitive.NilObjectID, nil //FIXME
}
// Deprecated
func (fs *WebFileSystem) InitFS() error {
rootDir := DirectoryFile{
FileHeader: FileHeader{
Name: "/",
Type: "directory",
Icon: "",
},
Data: DirectoryData{
// Parent: id, //FIXME don't work
Children: []primitive.ObjectID{},
},
}
_, err := fs.webfsCollection.InsertOne(context.Background(), &rootDir)
return err
}
// Deprecated
type FileId struct {
Id primitive.ObjectID `bson:"_id"`
}
// Deprecated
type FileHeader struct {
Name string `bson:"name" json:"name"`
Type string `bson:"type" json:"type"`
Icon string `bson:"-" json:"icon"`
}
// Deprecated
type DirectoryFile struct {
FileHeader `bson:"header"`
Data DirectoryData `bson:"data" json:"-"`
}
// Deprecated
type OnlyHeaderFile struct {
FileHeader `bson:"header"`
// Data DirectoryData `bson:"data" json:"-"`
}

View File

@ -13,8 +13,8 @@ func (fs *WebFileSystem) Route(route *gin.RouterGroup) {
// ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct
// return
// }
path := ctx.Query("path")
if path == "" {
parentPath := ctx.Query("parentPath")
if parentPath == "" {
ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct
return
}
@ -32,7 +32,7 @@ func (fs *WebFileSystem) Route(route *gin.RouterGroup) {
ctx.SaveUploadedFile(file, dst)
//TODO: Not Save to disk
err := fs.UploadFile(dst, path)
err := fs.UploadFile(dst, parentPath)
if err != nil {
ctx.String(http.StatusInternalServerError, "TODO") //TODO
return
@ -65,14 +65,14 @@ func (fs *WebFileSystem) Route(route *gin.RouterGroup) {
// ctx.JSON(http.StatusOK, "OK")
// })
// route.GET("init", func(ctx *gin.Context) {
// err := fs.InitFS()
// if err != nil {
// ctx.JSON(http.StatusInternalServerError, err.Error()) //TODO json error struct
// return
// }
// ctx.JSON(http.StatusOK, "OK")
// })
route.GET("init", func(ctx *gin.Context) {
err := fs.V3InitFS()
if err != nil {
ctx.JSON(http.StatusInternalServerError, err.Error()) //TODO json error struct
return
}
ctx.JSON(http.StatusOK, "Inited")
})
route.GET("createDir", func(ctx *gin.Context) {
path := ctx.Query("path")
@ -81,7 +81,7 @@ func (fs *WebFileSystem) Route(route *gin.RouterGroup) {
return
}
_, err := fs.NewCreateDirectory(path)
_, _, err := fs.V3CreateDirectory(path)
if err != nil {
ctx.JSON(http.StatusInternalServerError, err.Error()) //TODO json error struct
return
@ -97,9 +97,9 @@ func (fs *WebFileSystem) Route(route *gin.RouterGroup) {
return
}
files, err := fs.NewList(path)
files, err := fs.V3List(path)
if err != nil {
ctx.JSON(http.StatusInternalServerError, "TODO") //TODO json error struct
ctx.String(http.StatusInternalServerError, err.Error()) //TODO json error struct
return
}
@ -113,7 +113,7 @@ func (fs *WebFileSystem) Route(route *gin.RouterGroup) {
return
}
file, err := fs.NewRead(path)
file, err := fs.NewReadDeprecated(path)
if err != nil {
ctx.JSON(http.StatusInternalServerError, "TODO") //TODO json error struct
return
@ -138,7 +138,7 @@ func (fs *WebFileSystem) Route(route *gin.RouterGroup) {
ctx.Status(http.StatusBadRequest) //TODO
return
}
err := fs.Delete(path)
err := fs.V3Remove(path)
if err != nil {
ctx.Status(http.StatusInternalServerError)
return

View File

@ -2,9 +2,9 @@ package webfilesystem
import (
"context"
"errors"
"strings"
"github.com/mitchellh/mapstructure"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
@ -12,16 +12,21 @@ import (
type WebFileSystem struct {
webfsCollection *mongo.Collection
webfsFilesTable *mongo.Collection
webfsFilesData *mongo.Collection
ctx context.Context
}
func NewWebFileSystem(mongoClient *mongo.Client, dBName string, fsCollectionName string) *WebFileSystem {
return &WebFileSystem{
webfsCollection: mongoClient.Database(dBName).Collection(fsCollectionName), // TODO Check collection is exist
webfsFilesTable: mongoClient.Database(dBName).Collection("webfs-table"), // TODO Check collection is exist, //FIXME
webfsFilesData: mongoClient.Database(dBName).Collection("webfs-data"), // TODO Check collection is exist, //FIXME
ctx: context.Background(),
}
}
// Deprecated
func (fs *WebFileSystem) ReadByObjectID(objectId primitive.ObjectID) (*WebFSFile, error) {
filter := primitive.D{
{
@ -50,19 +55,26 @@ func (fs *WebFileSystem) castInsertId(res *mongo.InsertOneResult) primitive.Obje
}
func (fs *WebFileSystem) insertFileToDirectory(fileId primitive.ObjectID, directoryId primitive.ObjectID) error {
dir, err := fs.ReadByObjectID(directoryId)
read := ReadStruct2{
File: DirectoryFile{},
Id: directoryId,
}
parentDirRaw, err := fs.Read(read)
if err != nil {
return err
}
//TODO check if file exist
fileData := DirectoryData{}
err = mapstructure.Decode(dir.Data.(primitive.D).Map(), &fileData)
if err != nil {
return err
}
fileData.Children = append(fileData.Children, fileId)
var dirPtr interface{} = *parentDirRaw
parentDir := dirPtr.(*DirectoryFile)
fs.webfsCollection.UpdateByID(context.Background(), directoryId, bson.M{"$set": bson.M{"data": fileData}})
parentDir.Data.Children = append(parentDir.Data.Children, fileId)
res, err := fs.webfsCollection.UpdateByID(context.Background(), directoryId, bson.M{"$set": bson.M{"data.children": parentDir.Data.Children}})
if err != nil {
return err
}
if res.ModifiedCount < 1 {
return errors.New("no parent folders edited")
}
return nil
}
@ -86,15 +98,18 @@ func (fs *WebFileSystem) GetExtension(filename string) string {
func (fs *WebFileSystem) GetParentPath(path string) string {
splittedPath := fs.SplitPath(path)
parentPath := strings.Join(splittedPath[:len(splittedPath)-1], "/")
return parentPath
if len(splittedPath) > 1 {
return "/" + strings.Join(splittedPath[1:len(splittedPath)-1], "/")
}
return "/"
}
// Deprecated
func (fs *WebFileSystem) Delete(filePath string) error {
// splittedPath := fs.SplitPath(filePath)
// parentPath := fs.GetParentPath(filePath)
_, err := fs.NewRead(filePath)
_, err := fs.NewReadDeprecated(filePath)
if err != nil {
return err
}

181
webfilesystem/webfs3.go Normal file
View File

@ -0,0 +1,181 @@
package webfilesystem
import (
"errors"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type V3FileHeader struct {
MongoId primitive.ObjectID `bson:"_id" json:"-"`
Name string `bson:"name" json:"name"`
Type string `bson:"type" json:"type"`
Icon string `bson:"-" json:"icon"`
Data primitive.ObjectID `bson:"data_id" json:"-"`
}
type V3FileBinaryData struct {
MongoId primitive.ObjectID `bson:"_id" json:"-"`
Bin []byte `bson:"bin" json:"-"`
}
func (fs *WebFileSystem) V3ReadHeader(fileID primitive.ObjectID) (*V3FileHeader, error) {
file := &V3FileHeader{}
filter := primitive.M{
"_id": fileID,
}
err := fs.webfsFilesTable.FindOne(fs.ctx, filter).Decode(file)
return file, err
}
func (fs *WebFileSystem) V3Read(fileID primitive.ObjectID, fileData interface{}) (*V3FileHeader, error) {
fileHeader := &V3FileHeader{}
filter := primitive.M{
"_id": fileID,
}
err := fs.webfsFilesTable.FindOne(fs.ctx, filter).Decode(fileHeader)
if err != nil {
return nil, err
}
if fileData != nil {
filter := primitive.M{
"_id": fileHeader.Data,
}
err := fs.webfsFilesData.FindOne(fs.ctx, filter).Decode(fileData)
if err != nil {
return nil, err
}
}
return fileHeader, nil
}
func (fs *WebFileSystem) V3Write(filePath string, file *V3FileHeader, data interface{}) (primitive.ObjectID, primitive.ObjectID, error) {
headerId, dataId, err := fs.v3WriteFileToMongo(file, data)
if err != nil {
return primitive.NilObjectID, primitive.NilObjectID, err
}
parentPath := fs.GetParentPath(filePath)
parentDir, err := fs.V3FindFile(parentPath)
if err != nil {
return primitive.NilObjectID, primitive.NilObjectID, err
}
if parentDir.IsZero() {
return primitive.NilObjectID, primitive.NilObjectID, errors.New("parent dir not found")
}
err = fs.V3AppendChildToDirectory(headerId, parentDir)
if err != nil {
return primitive.NilObjectID, primitive.NilObjectID, err
}
return headerId, dataId, nil
}
func (fs *WebFileSystem) v3WriteFileToMongo(file *V3FileHeader, data interface{}) (primitive.ObjectID, primitive.ObjectID, error) {
resData, err := fs.webfsFilesData.InsertOne(fs.ctx, data)
if err != nil {
return primitive.NilObjectID, primitive.NilObjectID, err
}
file.Data = resData.InsertedID.(primitive.ObjectID)
resHeader, err := fs.webfsFilesTable.InsertOne(fs.ctx, file)
if err != nil {
return primitive.NilObjectID, primitive.NilObjectID, err
}
return resHeader.InsertedID.(primitive.ObjectID), resData.InsertedID.(primitive.ObjectID), err
}
func (fs *WebFileSystem) V3WriteUpdateData(file *V3FileHeader, data interface{}) error {
update := bson.M{"$set": data}
// println("updating data file: " + file.MongoId.String())
res, err := fs.webfsFilesData.UpdateByID(fs.ctx, file.Data, update)
if err != nil {
return err
}
if res.ModifiedCount < 1 {
return errors.New("no data updated")
}
return err
}
func (fs *WebFileSystem) V3InitFS() error { //FIXME Can't set parent_id to itself
rootData := V3DirectoryData{
MongoId: primitive.NewObjectID(),
Parent: primitive.NewObjectID(),
Children: []primitive.ObjectID{},
}
rootHeader := V3FileHeader{
MongoId: primitive.NewObjectID(),
Name: "/",
Type: "directory",
Icon: "",
}
_, _, err := fs.v3WriteFileToMongo(&rootHeader, &rootData)
if err != nil {
return err
}
return nil
}
func (fs *WebFileSystem) V3GetRootDir() (*V3FileHeader, *V3DirectoryData, error) {
filter := primitive.M{
"name": "/",
}
res := fs.webfsFilesTable.FindOne(fs.ctx, filter)
if res == nil {
return nil, nil, errors.New("TODO") //TODO
}
rootDir := V3FileHeader{}
err := res.Decode(&rootDir)
if res == nil {
return nil, nil, err
}
filterData := primitive.M{
"_id": rootDir.Data,
}
resData := fs.webfsFilesData.FindOne(fs.ctx, filterData)
if resData.Err() != nil {
return nil, nil, err
}
rootDirData := V3DirectoryData{}
err = resData.Decode(&rootDirData)
if err != nil {
return nil, nil, err
}
return &rootDir, &rootDirData, nil
}
func (fs *WebFileSystem) V3Remove(filePath string) error {
parentPath := fs.GetParentPath(filePath)
parentDirId, err := fs.V3FindFile(parentPath)
if err != nil {
return err
}
//TODO: Check, if parent file is dir
parentDirData := V3DirectoryData{}
parentDir, err := fs.V3Read(parentDirId, &parentDirData)
if err != nil {
return err
}
fileId, err := fs.V3FindFile(filePath)
if err != nil {
return err
}
newChildren := []primitive.ObjectID{}
for _, childId := range parentDirData.Children {
if childId != fileId {
newChildren = append(newChildren, childId)
}
}
parentDirData.Children = newChildren
err = fs.V3WriteUpdateData(parentDir, parentDirData)
if err != nil {
return err
}
return nil
}
func (fs *WebFileSystem) removeFromMongo(fileId primitive.ObjectID) error {
return errors.New("todo not implemented yet")
}