base64 images

This commit is contained in:
cyber-dream 2023-04-29 19:47:12 +03:00
parent 0a8e438d4d
commit 5f07c9a52c
5 changed files with 43 additions and 19 deletions

16
main.go
View File

@ -66,7 +66,7 @@ func main() {
persPropsApp := personalprops.NewPersPropsApp()
// finderApp := finder.FinderApplication{}
finderApp := finder.NewFinderApplication(webfs)
imgViewerApp := imgviewer.NewImgViewerApp()
imgViewerApp := imgviewer.NewImgViewerApp(webfs)
blogViewerApp := blogviewer.NewBlogViewerApp(webfs)
appsStorage := websiteapp.ApplicationsStorage{
Apps: map[string]websiteapp.WebDEApplication{},
@ -214,10 +214,20 @@ func main() {
imgViewerRoute.GET("render", func(ctx *gin.Context) {
isMobileParam := ctx.Query("isMobile")
isMobile := isMobileParam == "true"
path := ctx.Query("path")
if path == "" {
ctx.JSON(http.StatusBadRequest, "no path provided")
return
}
ginH, err := imgViewerApp.Render(path, isMobile)
if err != nil {
ctx.JSON(http.StatusInternalServerError, "TODO")
return
}
if isMobile {
ctx.HTML(http.StatusOK, "img-viewer/mobile-app.tmpl", imgViewerApp.Render(isMobile))
ctx.HTML(http.StatusOK, "img-viewer/mobile-app.tmpl", ginH)
} else {
ctx.HTML(http.StatusOK, "img-viewer/app.tmpl", imgViewerApp.Render(isMobile))
ctx.HTML(http.StatusOK, "img-viewer/app.tmpl", ginH)
}
})

View File

@ -89,9 +89,9 @@ class Finder{
// //TODO get real id
// WebDesktopEnvironment.Open("personal-properties", [])
// break;
// case "img":
// WebDesktopEnvironment.Open("img-viewer", ["pizda"])
// break;
case "base64img":
WebDesktopEnvironment.Open("img-viewer", [this.path + "/" + fileName])
break;
default:
console.log("Unsupported file type")
break;

View File

@ -1,11 +1,12 @@
class ImgViewer{
appId = "img-viewer"
/**
* @param {string} path
* @param {string[]} args
*/
NewWindow(path){
NewWindow(args){
fetch(`${window.location.origin}/application/${this.appId}/render?`+ new URLSearchParams({
isMobile: WebDesktopEnvironment.isMobile,
path: args[0]
}))
.then((response) => response.text())
.then((html) => {
@ -24,7 +25,3 @@ class ImgViewer{
}
}
class test{
}

View File

@ -1,5 +1,5 @@
{{ define "img-viewer/app.tmpl" }}
<div id="TestWindowHeader" class="WindowFrameTopBar">
<div id="TestWindowHeader" class="WindowFrameTopBar DragArea">
<button id="closeWindowButton" class="WindowFrameTopBarButton" title="Close Window"></button>
<div id="Drag" class="WindowDragArea"></div>
<div class="WindowFrameTitle">
@ -12,7 +12,7 @@
Toolbar
</div> -->
<div class="Img-Viewer-Picture-Container">
<div class="Img-Viewer-Picture">
<img src="data:image/jpeg;base64,{{.base64}}">
</div>
</div>
{{ end }}

View File

@ -1,17 +1,22 @@
package imgviewer
import (
"errors"
"personalwebsite/webfilesystem"
"personalwebsite/websiteapp"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type ImgViewerApp struct {
fs *webfilesystem.WebFileSystem
manifest websiteapp.ApplicationManifest
}
func NewImgViewerApp() ImgViewerApp {
func NewImgViewerApp(webFs *webfilesystem.WebFileSystem) ImgViewerApp {
newApp := ImgViewerApp{
fs: webFs,
manifest: websiteapp.ApplicationManifest{
AppId: "img-viewer",
WindowName: "About me", //TODO: delete
@ -27,9 +32,21 @@ func (p *ImgViewerApp) GetId() string {
return p.manifest.AppId
}
func (p *ImgViewerApp) Render(isMobile bool) gin.H {
return gin.H{
"Name": "Greg Brzezinski",
"BasicBio": "Born 27.09.1998 at Saint-Petersburg",
func (p *ImgViewerApp) Render(path string, isMobile bool) (gin.H, error) {
img, err := p.fs.Read(path)
if err != nil {
return nil, err
}
header, ok := img.Data.(primitive.D).Map()["header"]
if !ok {
return nil, errors.New("error in file decoding")
}
base64, ok := img.Data.(primitive.D).Map()["base64"]
if !ok {
return nil, errors.New("error in file decoding")
}
return gin.H{
"header": header,
"base64": base64,
}, nil
}