From 6a93f418d54e9a90b262ac7e2d261a31b94146f4 Mon Sep 17 00:00:00 2001 From: cyber-dream Date: Tue, 16 May 2023 13:51:28 +0300 Subject: [PATCH] Create public and private modes for website --- apps/blogviewer/blogviewer.go | 23 ++- apps/finder/finder.go | 31 +++- apps/finder/routes.go | 58 +++++-- apps/img-viewer/imgviewer.go | 28 ++-- apps/personalprops/personalprops.go | 42 ++--- apps/websiteapp.go | 26 ++- libs/cat.go | 12 +- libs/imglib.go | 6 +- main.go | 213 ++++++++++++------------- resources/dev-fs/apps/finder/finder.js | 29 ++-- resources/wde.js | 6 +- routes/private.go | 119 ++++++++++++++ routes/public.go | 101 ++++++++++++ routewde/wde.go | 5 +- templates/finder/app.tmpl | 25 +-- webfilesystem/directory.go | 6 +- webfilesystem/routes.go | 6 +- 17 files changed, 509 insertions(+), 227 deletions(-) create mode 100644 routes/private.go create mode 100644 routes/public.go diff --git a/apps/blogviewer/blogviewer.go b/apps/blogviewer/blogviewer.go index c3e4686..edf39c8 100644 --- a/apps/blogviewer/blogviewer.go +++ b/apps/blogviewer/blogviewer.go @@ -2,7 +2,6 @@ package blogviewer import ( "net/http" - "personalwebsite/apps" "personalwebsite/webfilesystem" "github.com/gin-gonic/gin" @@ -10,28 +9,24 @@ import ( ) type BlogViewerApplication struct { - fs *webfilesystem.WebFileSystem - manifest apps.ApplicationManifest + fs *webfilesystem.WebFileSystem + appID string } func NewBlogViewerApp(webFs *webfilesystem.WebFileSystem) *BlogViewerApplication { return &BlogViewerApplication{ - fs: webFs, - manifest: apps.ApplicationManifest{ - AppId: "blog-viewer", - }, + fs: webFs, + appID: "BlogViewer", } } -func (b *BlogViewerApplication) GetManifest() apps.ApplicationManifest { - return b.manifest +func (b *BlogViewerApplication) GetAppID() string { + return b.appID } - -func (b *BlogViewerApplication) GetId() string { - return b.manifest.AppId +func (b *BlogViewerApplication) PrivateRoutes(route *gin.RouterGroup) { + b.PublicRoutes(route) } - -func (b *BlogViewerApplication) Route(route *gin.RouterGroup) { +func (b *BlogViewerApplication) PublicRoutes(route *gin.RouterGroup) { route.GET("writeMockBlog", func(ctx *gin.Context) { path := ctx.Query("path") if path == "" { diff --git a/apps/finder/finder.go b/apps/finder/finder.go index 8fdd779..617d3b5 100644 --- a/apps/finder/finder.go +++ b/apps/finder/finder.go @@ -10,12 +10,14 @@ import ( type FinderApplication struct { fs *webfilesystem.WebFileSystem + appID string manifest apps.ApplicationManifest } func NewFinderApplication(webFs *webfilesystem.WebFileSystem) *FinderApplication { return &FinderApplication{ - fs: webFs, + fs: webFs, + appID: "Finder", manifest: apps.ApplicationManifest{ AppId: "finder", }, @@ -24,15 +26,15 @@ func NewFinderApplication(webFs *webfilesystem.WebFileSystem) *FinderApplication func (f *FinderApplication) GetManifest() apps.ApplicationManifest { return f.manifest } -func (f *FinderApplication) GetId() string { - return f.manifest.AppId +func (f *FinderApplication) GetAppID() string { + return f.appID } func (f *FinderApplication) Render(isMobile bool) gin.H { return gin.H{} } -func (f *FinderApplication) RenderContextMenu(context string, filePath string, data string) gin.H { +func (f *FinderApplication) RenderPrivateContextMenu(context string, filePath string, data string) gin.H { islands := [][]wde.ContexMenuRow{} islands = append(islands, []wde.ContexMenuRow{}) @@ -68,6 +70,27 @@ func (f *FinderApplication) RenderContextMenu(context string, filePath string, d "Islands": islands, } } +func (f *FinderApplication) RenderPublicContextMenu(context string, filePath string, data string) gin.H { + islands := [][]wde.ContexMenuRow{} + islands = append(islands, []wde.ContexMenuRow{}) + + islands = append(islands, []wde.ContexMenuRow{ + {Label: "Get Info", Action: "getInfo"}, + }) + if context == "FileTileView" { + return gin.H{ + "Islands": islands, + } + } + + switch context { + default: + } + + return gin.H{ + "Islands": islands, + } +} func (f *FinderApplication) RenderProps(filePath string) (gin.H, error) { // file, err := f.fs.NewReadDeprecated(filePath) diff --git a/apps/finder/routes.go b/apps/finder/routes.go index 85461ec..c01008f 100644 --- a/apps/finder/routes.go +++ b/apps/finder/routes.go @@ -6,19 +6,9 @@ import ( "github.com/gin-gonic/gin" ) -func (f *FinderApplication) Routes(routes *gin.RouterGroup) { +func (f *FinderApplication) PublicRoutes(routes *gin.RouterGroup) { routes.GET("render", func(ctx *gin.Context) { - isMobileParam := ctx.Query("isMobile") - isMobile := isMobileParam == "true" - admin := true - if isMobile { - ctx.HTML(http.StatusOK, "finder/mobile-app.tmpl", f.Render(isMobile)) - return - } - if admin { - ctx.HTML(http.StatusOK, "finder/admin-app.tmpl", f.Render(isMobile)) - return - } + isMobile := ctx.Query("isMobile") == "true" ctx.HTML(http.StatusOK, "finder/app.tmpl", f.Render(isMobile)) }) @@ -46,7 +36,49 @@ func (f *FinderApplication) Routes(routes *gin.RouterGroup) { return } data := ctx.Query("data") - ginH := f.RenderContextMenu(context, filePath, data) + // ginH := f.RenderPrivateContextMenu(context, filePath, data) + ginH := f.RenderPublicContextMenu(context, filePath, data) + ctx.HTML(http.StatusOK, "wde-widgets/context-menu.tmpl", ginH) + }) + + routes.GET("renderProps", func(ctx *gin.Context) { + filePath := ctx.Query("path") + ginH, err := f.RenderProps(filePath) + if err != nil { + ctx.Status(http.StatusInternalServerError) + return + } + ctx.HTML(http.StatusOK, "finder/props.tmpl", ginH) + }) +} + +func (f *FinderApplication) PrivateRoutes(routes *gin.RouterGroup) { + routes.GET("render", func(ctx *gin.Context) { + isMobile := ctx.Query("isMobile") == "true" + ctx.HTML(http.StatusOK, "finder/admin-app.tmpl", f.Render(isMobile)) + }) + + routes.GET("renderMobileDesktop", func(ctx *gin.Context) { + ctx.HTML(http.StatusOK, "finder/mobile-desktop.tmpl", gin.H{}) + }) + routes.GET("renderDesktop", func(ctx *gin.Context) { + path := ctx.Query("path") + if path == "" { + ctx.JSON(http.StatusBadRequest, "no path provided") + return + } + ctx.HTML(http.StatusOK, "finder/desktop.tmpl", gin.H{}) + }) + + routes.GET("contextMenu", func(ctx *gin.Context) { + context := ctx.Query("context") + filePath := ctx.Query("path") + if filePath == "" { + ctx.Status(http.StatusBadRequest) + return + } + data := ctx.Query("data") + ginH := f.RenderPrivateContextMenu(context, filePath, data) ctx.HTML(http.StatusOK, "wde-widgets/context-menu.tmpl", ginH) }) diff --git a/apps/img-viewer/imgviewer.go b/apps/img-viewer/imgviewer.go index 580024c..0f717fb 100644 --- a/apps/img-viewer/imgviewer.go +++ b/apps/img-viewer/imgviewer.go @@ -2,28 +2,27 @@ package imgviewer import ( "net/http" - websiteapp "personalwebsite/apps" "personalwebsite/webfilesystem" "github.com/gin-gonic/gin" ) type ImgViewerApp struct { - fs *webfilesystem.WebFileSystem - manifest websiteapp.ApplicationManifest + fs *webfilesystem.WebFileSystem + appID string } -func NewImgViewerApp(webFs *webfilesystem.WebFileSystem) ImgViewerApp { +func NewImgViewerApp(webFs *webfilesystem.WebFileSystem) *ImgViewerApp { newApp := ImgViewerApp{ - fs: webFs, - manifest: websiteapp.ApplicationManifest{ - AppId: "img-viewer", - }, + fs: webFs, + appID: "ImgViewer", } - return newApp + return &newApp } - -func (p *ImgViewerApp) Route(route *gin.RouterGroup) { +func (p *ImgViewerApp) PrivateRoutes(route *gin.RouterGroup) { + p.PublicRoutes(route) +} +func (p *ImgViewerApp) PublicRoutes(route *gin.RouterGroup) { route.GET("render", func(ctx *gin.Context) { isMobileParam := ctx.Query("isMobile") isMobile := isMobileParam == "true" @@ -45,11 +44,8 @@ func (p *ImgViewerApp) Route(route *gin.RouterGroup) { }) } -func (p *ImgViewerApp) GetManifest() websiteapp.ApplicationManifest { - return p.manifest -} -func (p *ImgViewerApp) GetId() string { - return p.manifest.AppId +func (p *ImgViewerApp) GetAppID() string { + return p.appID } func (p *ImgViewerApp) Render(filePath string, isMobile bool) (gin.H, error) { diff --git a/apps/personalprops/personalprops.go b/apps/personalprops/personalprops.go index e28069c..97fba46 100644 --- a/apps/personalprops/personalprops.go +++ b/apps/personalprops/personalprops.go @@ -2,36 +2,27 @@ package personalprops import ( "net/http" - websiteapp "personalwebsite/apps" "personalwebsite/webfilesystem" "github.com/gin-gonic/gin" "go.mongodb.org/mongo-driver/bson/primitive" ) +//TODO Rename to AboutMe type PersonalPropertiesApp struct { - fs *webfilesystem.WebFileSystem - manifest websiteapp.ApplicationManifest + fs *webfilesystem.WebFileSystem + appID string } -func NewPersPropsApp(webFs *webfilesystem.WebFileSystem) PersonalPropertiesApp { +func NewPersPropsApp(webFs *webfilesystem.WebFileSystem) *PersonalPropertiesApp { newApp := PersonalPropertiesApp{ - fs: webFs, - manifest: websiteapp.ApplicationManifest{ - AppId: "personal-properties", - }, + fs: webFs, + appID: "AboutMe", } - return newApp + return &newApp } -func (p *PersonalPropertiesApp) Route(route *gin.RouterGroup) { - route.GET("writeMock", func(ctx *gin.Context) { - err := p.WriteMock() - if err != nil { - ctx.Status(http.StatusInternalServerError) - } - ctx.Status(http.StatusOK) - }) +func (p *PersonalPropertiesApp) PublicRoutes(route *gin.RouterGroup) { route.GET("render", func(ctx *gin.Context) { isMobileParam := ctx.Query("isMobile") isMobile := isMobileParam == "true" @@ -53,11 +44,20 @@ func (p *PersonalPropertiesApp) Route(route *gin.RouterGroup) { }) } -func (p *PersonalPropertiesApp) GetManifest() websiteapp.ApplicationManifest { - return p.manifest +func (p *PersonalPropertiesApp) PrivateRoutes(router *gin.RouterGroup) { + p.PublicRoutes(router) + router.GET("writeMock", func(ctx *gin.Context) { + err := p.WriteMock() + if err != nil { + ctx.Status(http.StatusInternalServerError) + } + ctx.Status(http.StatusOK) + }) + } -func (p *PersonalPropertiesApp) GetId() string { - return p.manifest.AppId + +func (p *PersonalPropertiesApp) GetAppID() string { + return p.appID } func (p *PersonalPropertiesApp) WriteMock() error { diff --git a/apps/websiteapp.go b/apps/websiteapp.go index 5152113..69447f7 100644 --- a/apps/websiteapp.go +++ b/apps/websiteapp.go @@ -12,9 +12,12 @@ import ( //TODO to libs type WebDEApplication interface { // Render() - GetManifest() ApplicationManifest + GetAppID() string + PublicRoutes(*gin.RouterGroup) + PrivateRoutes(*gin.RouterGroup) + // GetManifest() ApplicationManifest //TODO: Delete // GEtHtml() - GetId() string + // GetId() string } type ApplicationManifest struct { @@ -58,12 +61,6 @@ func (as *ApplicationsStorage) createApp(appName string, appId string, appPath s return nil } -// func NewApplicationsStorage() *ApplicationsStorage { -// newStorage := ApplicationsStorage{} - -// return &newStorage -// } - func (aStorage *ApplicationsStorage) Route(route *gin.RouterGroup) { route.GET("/get", func(ctx *gin.Context) { appId := ctx.Query("appid") @@ -71,12 +68,13 @@ func (aStorage *ApplicationsStorage) Route(route *gin.RouterGroup) { ctx.Status(http.StatusBadRequest) return } - app, isExist := aStorage.Apps[appId] - if !isExist { - ctx.Status(http.StatusNoContent) - return - } - ctx.JSON(http.StatusOK, app.GetManifest()) + // app, isExist := aStorage.Apps[appId] + // if !isExist { + // ctx.Status(http.StatusNoContent) + // return + // } + // ctx.JSON(http.StatusOK, app.GetManifest()) + ctx.String(http.StatusMovedPermanently, "Obsolete") }) route.GET("/loadApp", func(ctx *gin.Context) { diff --git a/libs/cat.go b/libs/cat.go index 703ef60..fb98e5f 100644 --- a/libs/cat.go +++ b/libs/cat.go @@ -38,7 +38,7 @@ func (c *Cat) Get(filePath string) (string, error) { return fileData.Data, nil } -func (c *Cat) Route(route *gin.RouterGroup) { +func (c *Cat) PublicRoutes(route *gin.RouterGroup) { route.GET("get", func(ctx *gin.Context) { path := ctx.Query("path") if path == "" { @@ -52,7 +52,13 @@ func (c *Cat) Route(route *gin.RouterGroup) { return } - ctx.String(http.StatusOK, "plaintext", data) - }) + mode := ctx.Query("mode") + switch mode { + case "json": + ctx.JSON(http.StatusOK, data) + default: + ctx.String(http.StatusOK, "plaintext", data) + } + }) } diff --git a/libs/imglib.go b/libs/imglib.go index 6524720..9ec9eb9 100644 --- a/libs/imglib.go +++ b/libs/imglib.go @@ -17,7 +17,7 @@ func NewImgLib(webfs *webfilesystem.WebFileSystem) *ImagLib { } } -func (l *ImagLib) Route(route *gin.RouterGroup) { +func (l *ImagLib) PublicRoutes(route *gin.RouterGroup) { route.GET("get", func(ctx *gin.Context) { path := ctx.Query("path") if path == "" { @@ -31,10 +31,6 @@ func (l *ImagLib) Route(route *gin.RouterGroup) { ctx.Status(http.StatusInternalServerError) return } - // _, err = l.fs.readFSDocs(fileId, &imgData) - // if err != nil { - // ctx.Status(http.StatusInternalServerError) - // } ctx.Data(http.StatusOK, "image/jpeg", imgData.Bin) }) diff --git a/main.go b/main.go index a215592..560d2b0 100644 --- a/main.go +++ b/main.go @@ -4,23 +4,17 @@ import ( "context" "errors" "log" - "net/http" "os" - "time" "personalwebsite/apps" "personalwebsite/apps/blogviewer" "personalwebsite/apps/finder" imgviewer "personalwebsite/apps/img-viewer" "personalwebsite/apps/personalprops" - "personalwebsite/libs" - "personalwebsite/routewde" + "personalwebsite/routes" "personalwebsite/wde" "personalwebsite/webfilesystem" - "github.com/gin-contrib/cors" - "github.com/gin-contrib/location" - "github.com/gin-gonic/gin" "github.com/joho/godotenv" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" @@ -56,122 +50,127 @@ func main() { log.Fatal(err) } - router := gin.New() - router.Use(location.Default()) - router.LoadHTMLGlob("templates/**/*") - router.Static("/res", "resources") - // Set a lower memory limit for multipart forms (default is 32 MiB) - router.MaxMultipartMemory = 8 << 20 // 8 MiB - - router.GET("/", func(ctx *gin.Context) { - ctx.HTML(http.StatusOK, "index.tmpl", gin.H{}) - }) - webfs := webfilesystem.NewWebFileSystem(client, dBName, webFsCollection) + appsStorage := apps.NewApplicationsStorage(map[string]apps.WebDEApplication{}, webfs) + // router := gin.New() + // router.Use(location.Default()) + // router.LoadHTMLGlob("templates/**/*") + // router.Static("/res", "resources") + // // Set a lower memory limit for multipart forms (default is 32 MiB) + // router.MaxMultipartMemory = 8 << 20 // 8 MiB + + // router.GET("/", func(ctx *gin.Context) { + // ctx.HTML(http.StatusOK, "index.tmpl", gin.H{}) + // }) + webde := wde.NewWDE(webfs) persPropsApp := personalprops.NewPersPropsApp(webfs) - // finderApp := finder.FinderApplication{} finderApp := finder.NewFinderApplication(webfs) imgViewerApp := imgviewer.NewImgViewerApp(webfs) blogViewerApp := blogviewer.NewBlogViewerApp(webfs) - appsStorage := apps.NewApplicationsStorage(map[string]apps.WebDEApplication{}, webfs) - appsStorage.Apps["personal-properties"] = &persPropsApp + appsStorage.Apps["personal-properties"] = persPropsApp appsStorage.Apps["finder"] = finderApp - appsStorage.Apps["img-viewer"] = &imgViewerApp + appsStorage.Apps["img-viewer"] = imgViewerApp appsStorage.Apps["blog-viewer"] = blogViewerApp - system := router.Group("system") - { - libsGroup := system.Group("libs") - { - imgLibGroup := libsGroup.Group("img") - { - imgLib := libs.NewImgLib(webfs) - imgLib.Route(imgLibGroup) - } + go routes.PublicRoutes(webfs, webde, appsStorage) + routes.PrivateRoutes(webfs, webde, appsStorage) - catLibGroup := libsGroup.Group("cat") - { - catLib := libs.NewCatLib(webfs) - catLib.Route(catLibGroup) - } + // system := router.Group("system") + // { + // libsGroup := system.Group("libs") + // { + // imgLibGroup := libsGroup.Group("img") + // { + // imgLib := libs.NewImgLib(webfs) + // imgLib.PublicRoutes(imgLibGroup) + // } - appsStorageGroup := libsGroup.Group("apps") - { - appsStorage.Route(appsStorageGroup) - } - } + // catLibGroup := libsGroup.Group("cat") + // { + // catLib := libs.NewCatLib(webfs) + // catLib.PublicRoutes(catLibGroup) + // } - wdeGroup := system.Group("wde") - { - routewde.Route(wdeGroup, webde) - } - apps := system.Group("applications") //TODO to libs - { - apps.GET("/:appid/:method", func(ctx *gin.Context) { - appId := ctx.Param("appid") - method := ctx.Param("method") + // appsStorageGroup := libsGroup.Group("apps") + // { + // appsStorage.Route(appsStorageGroup) + // } + // } - app, isExist := appsStorage.Apps[appId] - if !isExist { - ctx.Status(http.StatusNoContent) - return - } - switch method { - case "getmanifest": - ctx.JSON(http.StatusOK, app.GetManifest()) - case "app.js": - ctx.File("resources/sys/" + appId + "/" + appId + ".js") - case "app.css": - ctx.File("resources/sys/" + appId + "/" + appId + ".css") - default: - ctx.Status(http.StatusBadRequest) - } - }) - } - } + // wdeGroup := system.Group("wde") + // { + // routewde.PublicRoutes(wdeGroup, webde) + // } + // apps := system.Group("applications") //TODO to libs + // { + // apps.GET("/:appid/:method", func(ctx *gin.Context) { + // appId := ctx.Param("appid") + // method := ctx.Param("method") - fs := router.Group("fs") - { - webfs.Route(fs) - } - app := router.Group("app") - { - persPropApp := app.Group("AboutMe") - { - persPropsApp.Route(persPropApp) - } - finderAppRoute := app.Group("Finder") - { - finderApp.Routes(finderAppRoute) - } - imgViewerRoute := app.Group("img-viewer") - { - imgViewerApp.Route(imgViewerRoute) - } - blogViewerRoute := app.Group("blog-viewer") - { - blogViewerApp.Route(blogViewerRoute) - } + // app, isExist := appsStorage.Apps[appId] + // if !isExist { + // ctx.Status(http.StatusNoContent) + // return + // } + // switch method { + // case "getmanifest": + // ctx.JSON(http.StatusOK, app.GetManifest()) + // case "app.js": + // ctx.File("resources/sys/" + appId + "/" + appId + ".js") + // case "app.css": + // ctx.File("resources/sys/" + appId + "/" + appId + ".css") + // default: + // ctx.Status(http.StatusBadRequest) + // } + // }) + // } + // } - router.Use(cors.New(cors.Config{ - AllowAllOrigins: true, - // AllowOrigins: []string{"http://localhost:8080", "http://localhost:9090"}, - // AllowMethods: []string{"PUT", "PATCH"}, - // AllowHeaders: []string{"Origin"}, - ExposeHeaders: []string{"Content-Length"}, - AllowCredentials: true, - // AllowOriginFunc: func(origin string) bool { - // return origin == "https://github.com" - // }, - MaxAge: 12 * time.Hour, - })) - err = router.Run(":8080") - if err != nil { - log.Panicf("error: %s", err) - } - } + // fs := router.Group("fs") + // { + // fsGroup := systemGroup.Group("fs") + // { + // webfs.PublicRoutes(fsGroup) + // } + // } + // app := router.Group("app") + // { + // persPropApp := app.Group("AboutMe") + // { + // persPropsApp.Route(persPropApp) + // } + // finderAppRoute := app.Group("Finder") + // { + // finderApp.Routes(finderAppRoute) + // } + // imgViewerRoute := app.Group("img-viewer") + // { + // imgViewerApp.Route(imgViewerRoute) + // } + // blogViewerRoute := app.Group("blog-viewer") + // { + // blogViewerApp.Route(blogViewerRoute) + // } + + // router.Use(cors.New(cors.Config{ + // AllowAllOrigins: true, + // // AllowOrigins: []string{"http://localhost:8080", "http://localhost:9090"}, + // // AllowMethods: []string{"PUT", "PATCH"}, + // // AllowHeaders: []string{"Origin"}, + // ExposeHeaders: []string{"Content-Length"}, + // AllowCredentials: true, + // // AllowOriginFunc: func(origin string) bool { + // // return origin == "https://github.com" + // // }, + // MaxAge: 12 * time.Hour, + // })) + // err = router.Run(":8080") + // if err != nil { + // log.Panicf("error: %s", err) + // } + // } } // func index(c *gin.Context) { diff --git a/resources/dev-fs/apps/finder/finder.js b/resources/dev-fs/apps/finder/finder.js index 9affb26..c99f8d3 100644 --- a/resources/dev-fs/apps/finder/finder.js +++ b/resources/dev-fs/apps/finder/finder.js @@ -14,21 +14,24 @@ class Finder{ * @returns {boolean} */ static async RenderProperites(path){ + if (path == null || path ==""){ + return + } const params = new URLSearchParams({ path: path }) - const response = await fetch(`/application/${Finder.AppId}/renderProps?` + params) + const response = await fetch(`/app/${Finder.AppId}/renderProps?` + params) if (response.status != 200){ WebDesktopEnvironment.Alert("Error in properties render") //TODO return false } - const html = response.text() + const html = await response.text() let newWindow = WebDesktopEnvironment.CreateNewWindow(Finder.AppId, 350, 500 ) newWindow.innerHTML = html newWindow.querySelector("#closeWindowButton").addEventListener('click', function (params) { WebDesktopEnvironment.CloseWindow(newWindow) }) - } + } } @@ -96,7 +99,6 @@ class FinderWindow{ let scrollBar = new WdeScrollBar(newWindow.querySelector(".ScrollbarPlace"), newWindow.querySelector(".FileTileView")) - this.windowElem = newWindow this.RenderDir(args[0]) } @@ -199,7 +201,7 @@ class FinderWindow{ */ async CreateContextMenu(target, pos){ let context = "" - const fileName = target.getAttribute("name") + const fileName = target.getAttribute("name") //TODO check for null const fileType = target.getAttribute("fileType") if (target.classList.contains("FileTileView")) { @@ -207,7 +209,14 @@ class FinderWindow{ } else { context = fileType } - const params = new URLSearchParams({context: context, path: `${this.curPath}/${fileName}`}) + let path = "" + if (fileName === null){ + console.log(fileName) + path = this.curPath + } else { + path = `${this.curPath}/${fileName}` + } + const params = new URLSearchParams({context: context, path: path}) const response = await fetch(`/app/${Finder.AppId}/contextMenu?` + params) if (response.status != 200){ WebDesktopEnvironment.Alert("ERROR in Context menu TODO"); //TODO @@ -252,11 +261,11 @@ class FinderWindow{ } break case "getInfo": - res = await Finder.RenderProperites(`${this.curPath}/${fileName}`) + Finder.RenderProperites(path) + break + case "openAsDir": + WebDesktopEnvironment.Open(`/Applications/${Finder.AppId}.app`,[`${this.curPath}/${fileName}`]) break - case "openAsDir": - WebDesktopEnvironment.Open(`/Applications/${Finder.AppId}.app`,[`${this.curPath}/${fileName}`]) - break default: break; } diff --git a/resources/wde.js b/resources/wde.js index a3769b7..2a75562 100644 --- a/resources/wde.js +++ b/resources/wde.js @@ -82,11 +82,13 @@ class WebDesktopEnvironment{ * @returns {Object | undefined} //FIXME */ static async fetchApp(path){ - const params = new URLSearchParams({path: path}) - const response = await fetch(`/system/libs/apps/loadApp?` + params) + console.log("path: " + path ) + const params = new URLSearchParams({path: path, mode: "json"}) + const response = await fetch(`/system/loadApp?` + params) if (response.status != 200){ return undefined } + //TODO Validate manifest const appManifest = response.json() return appManifest } diff --git a/routes/private.go b/routes/private.go new file mode 100644 index 0000000..90566bd --- /dev/null +++ b/routes/private.go @@ -0,0 +1,119 @@ +package routes + +import ( + "log" + "net/http" + "path" + "personalwebsite/apps" + "personalwebsite/libs" + "personalwebsite/routewde" + "personalwebsite/wde" + "personalwebsite/webfilesystem" + "time" + + "github.com/gin-contrib/cors" + "github.com/gin-contrib/location" + "github.com/gin-gonic/gin" +) + +func PrivateRoutes(webfs *webfilesystem.WebFileSystem, webde *wde.WDE, appsStorage *apps.ApplicationsStorage) { + router := gin.New() + router.Use(location.Default()) + router.LoadHTMLGlob("templates/**/*") + router.Static("/res", "resources") + // Set a lower memory limit for multipart forms (default is 32 MiB) + router.MaxMultipartMemory = 8 << 20 // 8 MiB + + router.GET("/", func(ctx *gin.Context) { + ctx.HTML(http.StatusOK, "index.tmpl", gin.H{}) + }) + systemGroup := router.Group("system") + { + libsGroup := systemGroup.Group("libs") + { + imgLibGroup := libsGroup.Group("img") + { + imgLib := libs.NewImgLib(webfs) + imgLib.PublicRoutes(imgLibGroup) + } + + catLibGroup := libsGroup.Group("cat") + { + catLib := libs.NewCatLib(webfs) + catLib.PublicRoutes(catLibGroup) + } + + appsStorageGroup := libsGroup.Group("apps") + { + appsStorage.Route(appsStorageGroup) + } + } + + wdeGroup := systemGroup.Group("wde") + { + routewde.PublicRoutes(wdeGroup, webde) + } + fsGroup := systemGroup.Group("fs") + { + webfs.PrivateRoutes(fsGroup) + } + systemGroup.GET("/loadApp", func(ctx *gin.Context) { + appPath := ctx.Query("path") + if appPath == "" { + ctx.Status(http.StatusBadRequest) + return + } + + appBundleData := webfilesystem.DirectoryData{} + appBundle, err := webfs.Read(appPath, &appBundleData) + if err != nil { + ctx.Status(http.StatusInternalServerError) + return + } + if appBundle.Type != "directory" { + ctx.Status(http.StatusBadRequest) + return + } + + appManifestData := apps.ApplicationManifest{} + appManifestHeader, err := webfs.Read(path.Join(appPath, ".appmanifest"), &appManifestData) + if err != nil { + ctx.Status(http.StatusInternalServerError) + return + } + + if appManifestHeader.Type != "application-manifest" { + ctx.Status(http.StatusBadRequest) + return + } + + ctx.JSON(http.StatusOK, appManifestData) + + }) + } + + appsGroup := router.Group("app") + { + for _, app := range appsStorage.Apps { + appsRoutes := appsGroup.Group(app.GetAppID()) + app.PrivateRoutes(appsRoutes) + } + } + + router.Use(cors.New(cors.Config{ + AllowAllOrigins: true, + // AllowOrigins: []string{"http://localhost:8080", "http://localhost:9090"}, + // AllowMethods: []string{"PUT", "PATCH"}, + // AllowHeaders: []string{"Origin"}, + ExposeHeaders: []string{"Content-Length"}, + AllowCredentials: true, + // AllowOriginFunc: func(origin string) bool { + // return origin == "https://github.com" + // }, + MaxAge: 12 * time.Hour, + })) + err := router.Run(":8080") + if err != nil { + log.Panicf("error: %s", err) + } +} diff --git a/routes/public.go b/routes/public.go new file mode 100644 index 0000000..97a0975 --- /dev/null +++ b/routes/public.go @@ -0,0 +1,101 @@ +package routes + +import ( + "log" + "net/http" + "path" + "personalwebsite/apps" + "personalwebsite/libs" + "personalwebsite/routewde" + "personalwebsite/wde" + "personalwebsite/webfilesystem" + + "github.com/gin-contrib/location" + "github.com/gin-gonic/gin" +) + +func PublicRoutes(webfs *webfilesystem.WebFileSystem, webde *wde.WDE, appsStorage *apps.ApplicationsStorage) { + router := gin.New() + router.Use(location.Default()) + router.LoadHTMLGlob("templates/**/*") + router.Static("/res", "resources") + + router.GET("/", func(ctx *gin.Context) { + ctx.HTML(http.StatusOK, "index.tmpl", gin.H{}) + }) + + systemGroup := router.Group("system") + { + libsGroup := systemGroup.Group("libs") + { + imgLibGroup := libsGroup.Group("img") + { + imgLib := libs.NewImgLib(webfs) + imgLib.PublicRoutes(imgLibGroup) + } + + catLibGroup := libsGroup.Group("cat") + { + catLib := libs.NewCatLib(webfs) + catLib.PublicRoutes(catLibGroup) + } + } + + wdeGroup := systemGroup.Group("wde") + { + routewde.PublicRoutes(wdeGroup, webde) + } + + systemGroup.GET("/loadApp", func(ctx *gin.Context) { + appPath := ctx.Query("path") + if appPath == "" { + ctx.Status(http.StatusBadRequest) + return + } + + appBundleData := webfilesystem.DirectoryData{} + appBundle, err := webfs.Read(appPath, &appBundleData) + if err != nil { + ctx.Status(http.StatusInternalServerError) + return + } + if appBundle.Type != "directory" { + ctx.Status(http.StatusBadRequest) + return + } + + appManifestData := apps.ApplicationManifest{} + appManifestHeader, err := webfs.Read(path.Join(appPath, ".appmanifest"), &appManifestData) + if err != nil { + ctx.Status(http.StatusInternalServerError) + return + } + + if appManifestHeader.Type != "application-manifest" { + ctx.Status(http.StatusBadRequest) + return + } + + ctx.JSON(http.StatusOK, appManifestData) + + }) + + // fsGroup := systemGroup.Group("fs") + // { + // webfs.PublicRoutes(fsGroup) + // } + } + + appGroup := router.Group("app") + { + for _, app := range appsStorage.Apps { + appRoutes := appGroup.Group(app.GetAppID()) + app.PublicRoutes(appRoutes) + } + } + + err := router.Run(":7070") + if err != nil { + log.Panicf("error: %s", err) + } +} diff --git a/routewde/wde.go b/routewde/wde.go index 9076988..6f92a2f 100644 --- a/routewde/wde.go +++ b/routewde/wde.go @@ -8,8 +8,8 @@ import ( "github.com/gin-gonic/gin" ) -func Route(route *gin.RouterGroup, wde *wde.WDE) { - route.GET("/getbasicwindow", func(ctx *gin.Context) { +func PublicRoutes(route *gin.RouterGroup, wde *wde.WDE) { + route.GET("/getbasicwindow", func(ctx *gin.Context) { //TODO Rename to renderGenericWindowFrame ctx.HTML(http.StatusOK, "basic-window.html", nil) }) @@ -21,7 +21,6 @@ func Route(route *gin.RouterGroup, wde *wde.WDE) { { widgets.GET("/file-tile-view", func(ctx *gin.Context) { url := location.Get(ctx) - // _ = url path := ctx.Query("path") if path == "" { ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct diff --git a/templates/finder/app.tmpl b/templates/finder/app.tmpl index 3d4107b..becf3ba 100644 --- a/templates/finder/app.tmpl +++ b/templates/finder/app.tmpl @@ -1,24 +1,27 @@ {{ define "finder/app.tmpl" }}
-
+
- Files + Finder
-
+
-
- - -
-
-
+
+
+ +
+
+
+
+
+ {{template "wde-widgets/scrollbar.tmpl" .}} +
- {{template "wde-widgets/scrollbar.tmpl" .}}
{{ end }} - \ No newline at end of file + diff --git a/webfilesystem/directory.go b/webfilesystem/directory.go index b6ad005..b9c2894 100644 --- a/webfilesystem/directory.go +++ b/webfilesystem/directory.go @@ -7,9 +7,9 @@ import ( ) type DirectoryData struct { - MongoId primitive.ObjectID `bson:"_id"` - Parent primitive.ObjectID `bson:"parent_id"` - Children []primitive.ObjectID `bson:"children_id"` + MongoId primitive.ObjectID `bson:"_id" json:"-"` + Parent primitive.ObjectID `bson:"parent_id" json:"parent"` + Children []primitive.ObjectID `bson:"children_id" json:"children"` } func (fs *WebFileSystem) CreateDirectory(dirPath string) (primitive.ObjectID, primitive.ObjectID, error) { diff --git a/webfilesystem/routes.go b/webfilesystem/routes.go index e105d7c..75ad7ed 100644 --- a/webfilesystem/routes.go +++ b/webfilesystem/routes.go @@ -6,7 +6,7 @@ import ( "github.com/gin-gonic/gin" ) -func (fs *WebFileSystem) Route(route *gin.RouterGroup) { +func (fs *WebFileSystem) PrivateRoutes(route *gin.RouterGroup) { route.POST("/upload", func(ctx *gin.Context) { //TODO To PUT request // fileName := ctx.Query("fileName") // if fileName == "" { @@ -189,3 +189,7 @@ func (fs *WebFileSystem) Route(route *gin.RouterGroup) { ctx.Status(http.StatusOK) }) } + +func (fs *WebFileSystem) PublicRoutes(route *gin.RouterGroup) { + +}