From 435e98dac0e736b7c90be1dd1fdb7b71d1c4f625 Mon Sep 17 00:00:00 2001 From: cyber-dream Date: Thu, 11 May 2023 04:45:57 +0300 Subject: [PATCH] Start work on links support --- webfilesystem/mongo.go | 1 + webfilesystem/routes.go | 22 +++++++++++++++ webfilesystem/webfilesystem.go | 51 +++++++++++++++++++++++++++++++++- 3 files changed, 73 insertions(+), 1 deletion(-) diff --git a/webfilesystem/mongo.go b/webfilesystem/mongo.go index f217ab4..2a1a23d 100644 --- a/webfilesystem/mongo.go +++ b/webfilesystem/mongo.go @@ -6,6 +6,7 @@ import ( "go.mongodb.org/mongo-driver/bson/primitive" ) +//TODO Transparently split to single data and header reading methods func (fs *WebFileSystem) readFSDocs(fileID primitive.ObjectID, fileData interface{}) (*FileHeader, error) { fileHeader := &FileHeader{} filter := primitive.M{ diff --git a/webfilesystem/routes.go b/webfilesystem/routes.go index 7f1fbbb..e105d7c 100644 --- a/webfilesystem/routes.go +++ b/webfilesystem/routes.go @@ -45,6 +45,28 @@ func (fs *WebFileSystem) Route(route *gin.RouterGroup) { ctx.Status(http.StatusCreated) }) + //TODO Support links + route.POST("/createlink", func(ctx *gin.Context) { //TODO To PUT request + sourcePath := ctx.Query("sourcePath") + if sourcePath == "" { + ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct + return + } + targetPath := ctx.Query("targetPath") + if sourcePath == "" { + ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct + return + } + + err := fs.CreateObjectLink(sourcePath, targetPath) + if err != nil { + ctx.String(http.StatusInternalServerError, err.Error()) //TODO + return + } + + ctx.Status(http.StatusCreated) + }) + // route.GET("writeFile", func(ctx *gin.Context) { // parentPath := ctx.Query("parentPath") // if parentPath == "" { diff --git a/webfilesystem/webfilesystem.go b/webfilesystem/webfilesystem.go index 0904f6c..8d399f4 100644 --- a/webfilesystem/webfilesystem.go +++ b/webfilesystem/webfilesystem.go @@ -46,7 +46,12 @@ type BinaryFileData struct { type PlainTextFileData struct { MongoId primitive.ObjectID `bson:"_id" json:"-"` - Data string `bson:"data" json:"data"` + Data string `bson:"data" json:"-"` +} + +type ObjectLinkFileData struct { + MongoId primitive.ObjectID `bson:"_id" json:"-"` + Link_id primitive.ObjectID `bson:"link_id" json:"-"` } // Deprecated @@ -218,6 +223,50 @@ func (fs *WebFileSystem) Remove(filePath string) error { return nil } +func (fs *WebFileSystem) CreateObjectLink(sourcePath string, linkPath string) error { + // sourceParentPath := fs.GetParentPath(sourcePath) + // sourceParentDirId, err := fs.FindFile(sourceParentPath) + // if err != nil { + // return err + // } + + linkSplittedPath := fs.SplitPath(linkPath) + linkFileName := linkSplittedPath[len(linkSplittedPath)-1] + + linkParentDirPath := fs.GetParentPath(linkPath) + linkParentDirPathID, err := fs.FindFile(linkParentDirPath) + if err != nil { + return err + } + sourceFileID, err := fs.FindFile(sourcePath) + if err != nil { + return err + } + newLinkHeader := FileHeader{ + MongoId: primitive.NewObjectID(), + Name: linkFileName, + Type: "objectlink", + Icon: "", + Data: primitive.NewObjectID(), + } + newLinkData := ObjectLinkFileData{ + MongoId: primitive.NewObjectID(), + Link_id: sourceFileID, + } + + linkFileID, _, err := fs.Write(sourcePath, &newLinkHeader, newLinkData) + if err != nil { + return err + } + + err = fs.AppendChildToDirectory(linkFileID, linkParentDirPathID) + if err != nil { + return err + } + + return nil +} + func (fs *WebFileSystem) SplitPath(path string) []string { resPath := []string{} splittedPath := strings.Split(path, "/")