From c9e846e2cf71785f8dc1a2fb7654dfa3ef11910e Mon Sep 17 00:00:00 2001 From: cyber-dream Date: Mon, 8 May 2023 00:20:40 +0300 Subject: [PATCH] Start loading apps by manifest --- apps/blogviewer/blogviewer.go | 3 +- apps/finder/finder.go | 15 +++-- apps/finder/routes.go | 6 +- apps/img-viewer/imgviewer.go | 3 +- apps/personalprops/personalprops.go | 3 +- apps/websiteapp.go | 86 ++++++++++++++++++++++++++++- main.go | 12 ++-- resources/wde.js | 25 +++++++++ templates/finder/props.tmpl | 62 +++++++++++---------- test-img/myphoto.jpg | 3 + webfilesystem/webfilesystem.go | 2 +- 11 files changed, 172 insertions(+), 48 deletions(-) create mode 100644 test-img/myphoto.jpg diff --git a/apps/blogviewer/blogviewer.go b/apps/blogviewer/blogviewer.go index 985d55e..c3e4686 100644 --- a/apps/blogviewer/blogviewer.go +++ b/apps/blogviewer/blogviewer.go @@ -18,8 +18,7 @@ func NewBlogViewerApp(webFs *webfilesystem.WebFileSystem) *BlogViewerApplication return &BlogViewerApplication{ fs: webFs, manifest: apps.ApplicationManifest{ - AppId: "blog-viewer", - WindowName: "", + AppId: "blog-viewer", }, } } diff --git a/apps/finder/finder.go b/apps/finder/finder.go index 908880c..230f413 100644 --- a/apps/finder/finder.go +++ b/apps/finder/finder.go @@ -17,8 +17,7 @@ func NewFinderApplication(webFs *webfilesystem.WebFileSystem) *FinderApplication return &FinderApplication{ fs: webFs, manifest: apps.ApplicationManifest{ - AppId: "finder", - WindowName: "TODO DELETE", //TODO DELETE + AppId: "finder", }, } } @@ -65,15 +64,21 @@ func (f *FinderApplication) RenderContextMenu(context string, data string) gin.H } } -func (f *FinderApplication) RenderProps(filePath string) gin.H { +func (f *FinderApplication) RenderProps(filePath string) (gin.H, error) { // file, err := f.fs.NewReadDeprecated(filePath) // if err != nil { // return nil // } + fileHeader, err := f.fs.Read(filePath, nil) + if err != nil { + return nil, err + } return gin.H{ - // "file": file, - } + // "fileId": fileHeader.MongoId, + // "fileDataId": fileHeader.Data, + "file": fileHeader, + }, nil } // func (f *FinderApplication) Routes(routes *gin.RouterGroup) { diff --git a/apps/finder/routes.go b/apps/finder/routes.go index cb569c5..d0e3909 100644 --- a/apps/finder/routes.go +++ b/apps/finder/routes.go @@ -43,7 +43,11 @@ func (f *FinderApplication) Routes(routes *gin.RouterGroup) { routes.GET("renderProps", func(ctx *gin.Context) { filePath := ctx.Query("path") - ginH := f.RenderProps(filePath) + ginH, err := f.RenderProps(filePath) + if err != nil { + ctx.Status(http.StatusInternalServerError) + return + } ctx.HTML(http.StatusOK, "finder/props.tmpl", ginH) }) } diff --git a/apps/img-viewer/imgviewer.go b/apps/img-viewer/imgviewer.go index f2c0486..580024c 100644 --- a/apps/img-viewer/imgviewer.go +++ b/apps/img-viewer/imgviewer.go @@ -17,8 +17,7 @@ func NewImgViewerApp(webFs *webfilesystem.WebFileSystem) ImgViewerApp { newApp := ImgViewerApp{ fs: webFs, manifest: websiteapp.ApplicationManifest{ - AppId: "img-viewer", - WindowName: "About me", //TODO: delete + AppId: "img-viewer", }, } return newApp diff --git a/apps/personalprops/personalprops.go b/apps/personalprops/personalprops.go index d23518c..e28069c 100644 --- a/apps/personalprops/personalprops.go +++ b/apps/personalprops/personalprops.go @@ -18,8 +18,7 @@ func NewPersPropsApp(webFs *webfilesystem.WebFileSystem) PersonalPropertiesApp { newApp := PersonalPropertiesApp{ fs: webFs, manifest: websiteapp.ApplicationManifest{ - AppId: "personal-properties", - WindowName: "About me", + AppId: "personal-properties", }, } return newApp diff --git a/apps/websiteapp.go b/apps/websiteapp.go index 63a0fa5..57fa63c 100644 --- a/apps/websiteapp.go +++ b/apps/websiteapp.go @@ -2,10 +2,14 @@ package apps import ( "net/http" + "path" + "personalwebsite/webfilesystem" "github.com/gin-gonic/gin" + "go.mongodb.org/mongo-driver/bson/primitive" ) +//TODO to libs type WebDEApplication interface { // Render() GetManifest() ApplicationManifest @@ -14,12 +18,41 @@ type WebDEApplication interface { } type ApplicationManifest struct { - AppId string `json:"appid"` - WindowName string `json:"windowname"` + AppId string `bson:"appid" json:"appid"` + Js []string `bson:"js" json:"js"` + Css []string `bson:"css" json:"css"` +} + +func NewApplicationsStorage(apps map[string]WebDEApplication, webfs *webfilesystem.WebFileSystem) *ApplicationsStorage { + return &ApplicationsStorage{ + Apps: apps, + fs: webfs, + } } type ApplicationsStorage struct { Apps map[string]WebDEApplication + fs *webfilesystem.WebFileSystem +} + +func (as *ApplicationsStorage) createApp(appName string, appId string, appPath string) error { + newAppData := ApplicationManifest{ + AppId: appId, + } + + newAppFile := webfilesystem.FileHeader{ + MongoId: primitive.NewObjectID(), + Name: appName + ".appmanifest", + Type: "application", + Icon: "", + Data: primitive.NewObjectID(), + } + + _, _, err := as.fs.Write(appPath+"/"+newAppFile.Name, &newAppFile, newAppData) + if err != nil { + return err + } + return nil } // func NewApplicationsStorage() *ApplicationsStorage { @@ -28,7 +61,7 @@ type ApplicationsStorage struct { // return &newStorage // } -func Route(route *gin.RouterGroup, aStorage *ApplicationsStorage) { +func (aStorage *ApplicationsStorage) Route(route *gin.RouterGroup) { route.GET("/get", func(ctx *gin.Context) { appId := ctx.Query("appid") if appId == "" { @@ -42,4 +75,51 @@ func Route(route *gin.RouterGroup, aStorage *ApplicationsStorage) { } ctx.JSON(http.StatusOK, app.GetManifest()) }) + + route.GET("/loadApp", func(ctx *gin.Context) { + appPath := ctx.Query("path") + if appPath == "" { + ctx.Status(http.StatusBadRequest) + return + } + + appManifestData := ApplicationManifest{} + fileHeader, err := aStorage.fs.Read(path.Join(appPath, ".appmanifest"), &appManifestData) + if err != nil { + ctx.Status(http.StatusInternalServerError) + return + } + + if fileHeader.Type != "application-manifest" { + ctx.Status(http.StatusBadRequest) + return + } + + ctx.JSON(http.StatusOK, appManifestData) + }) + + route.GET("/createApp", func(ctx *gin.Context) { + appId := ctx.Query("appId") + if appId == "" { + ctx.Status(http.StatusBadRequest) + return + } + appName := ctx.Query("appName") + if appName == "" { + ctx.Status(http.StatusBadRequest) + return + } + appPath := ctx.Query("path") + if appPath == "" { + ctx.Status(http.StatusBadRequest) + return + } + + err := aStorage.createApp(appId, appName, appPath) + if err != nil { + ctx.Status(http.StatusInternalServerError) + return + } + ctx.Status(http.StatusOK) + }) } diff --git a/main.go b/main.go index f93bf6f..d89fd9c 100644 --- a/main.go +++ b/main.go @@ -73,9 +73,7 @@ func main() { finderApp := finder.NewFinderApplication(webfs) imgViewerApp := imgviewer.NewImgViewerApp(webfs) blogViewerApp := blogviewer.NewBlogViewerApp(webfs) - appsStorage := apps.ApplicationsStorage{ - Apps: map[string]apps.WebDEApplication{}, - } + appsStorage := apps.NewApplicationsStorage(map[string]apps.WebDEApplication{}, webfs) appsStorage.Apps["personal-properties"] = &persPropsApp appsStorage.Apps["finder"] = finderApp appsStorage.Apps["img-viewer"] = &imgViewerApp @@ -90,12 +88,18 @@ func main() { imgLib := libs.NewImgLib(webfs) imgLib.Route(imgLibGroup) } + + appsStorageGroup := libsGroup.Group("apps") + { + appsStorage.Route(appsStorageGroup) + } } + wdeGroup := system.Group("wde") { routewde.Route(wdeGroup, webde) } - apps := system.Group("applications") + apps := system.Group("applications") //TODO to libs { apps.GET("/:appid/:method", func(ctx *gin.Context) { appId := ctx.Param("appid") diff --git a/resources/wde.js b/resources/wde.js index b1dec9f..63de8be 100644 --- a/resources/wde.js +++ b/resources/wde.js @@ -5,6 +5,7 @@ document.addEventListener('DOMContentLoaded', function() { wde = new WebDesktopEnvironment if (!WebDesktopEnvironment.isMobile){ WebDesktopEnvironment.Open("finder", ["/home/user"]) + WebDesktopEnvironment.fetchApp("/home/user/AboutMe.app") // WebDesktopEnvironment.Open("blog-viewer", ["/home/user/blog1.blog"]) // WebDesktopEnvironment.Open("personal-properties", ["/home/user/aboutme.props"]) } else { @@ -115,6 +116,30 @@ class WebDesktopEnvironment{ }) } + /** + * @param {string} path + * @param {function} callbackFunc callback after script loading + * @returns {Application | undefined} + */ + static loadApp2(path, callbackFunc){ + //var myObject = window[classNameString]; to load random classes + } + + /** + * @param {string} path + * @returns {Application | undefined} //FIXME + */ + static fetchApp(path){ + fetch(`/system/libs/apps/loadApp?` + new URLSearchParams({ + path: path, + })) .then(async (response) => { + console.log(await response.json()) + }) + .catch((error) => { + WebDesktopEnvironment.Alert(error); + }) + } + /** * @param {string} appId * @param {string[]} args diff --git a/templates/finder/props.tmpl b/templates/finder/props.tmpl index fbd27be..0f73cd7 100644 --- a/templates/finder/props.tmpl +++ b/templates/finder/props.tmpl @@ -10,41 +10,47 @@
- File Icon +
{{ .file.Name }}
-
+
+
+ file id : {{.file.MongoId}} +
+
+ file data id : {{.file.Data}} +
{{ range $propIsland := .allprops }} -
-
- {{$propIsland.Header}}: -
-
- {{range $prop := $propIsland.Props}} -
-
- {{$prop.Key}}: - {{ range $value := $prop.KeyComments }} -
- {{ $value }} -
- {{ end }} -
-
- {{ range $value := $prop.Values }} -
- {{ $value }} -
- {{ end }} -
-
- {{ end }} -
+
+
+ {{$propIsland.Header}}:
- {{ end }} +
+ {{range $prop := $propIsland.Props}} +
+
+ {{$prop.Key}}: + {{ range $value := $prop.KeyComments }} +
+ {{ $value }} +
+ {{ end }} +
+
+ {{ range $value := $prop.Values }} +
+ {{ $value }} +
+ {{ end }} +
+
+ {{ end }} +
+
+ {{ end }}
diff --git a/test-img/myphoto.jpg b/test-img/myphoto.jpg new file mode 100644 index 0000000..dfd3454 --- /dev/null +++ b/test-img/myphoto.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:86e19fd69b67e673a398e550e4b56d9e9a2b56f1390b46b0d0df0f1996bd3c34 +size 80021 diff --git a/webfilesystem/webfilesystem.go b/webfilesystem/webfilesystem.go index a7675b7..e93f8fa 100644 --- a/webfilesystem/webfilesystem.go +++ b/webfilesystem/webfilesystem.go @@ -31,7 +31,7 @@ type FileHeader struct { Name string `bson:"name" json:"name"` Type string `bson:"type" json:"type"` Icon string `bson:"icon" json:"icon"` - Data primitive.ObjectID `bson:"data_id" json:"-"` + Data primitive.ObjectID `bson:"data_id" json:"-"` //TODO rename to DataId } func (fh *FileHeader) GetType() string {