personal-website/websiteapp/img-viewer/imgviewer.go

53 lines
1.1 KiB
Go
Raw Normal View History

2023-04-12 21:05:23 +00:00
package imgviewer
import (
2023-04-29 16:47:12 +00:00
"errors"
"personalwebsite/webfilesystem"
2023-04-12 21:05:23 +00:00
"personalwebsite/websiteapp"
"github.com/gin-gonic/gin"
2023-04-29 16:47:12 +00:00
"go.mongodb.org/mongo-driver/bson/primitive"
2023-04-12 21:05:23 +00:00
)
type ImgViewerApp struct {
2023-04-29 16:47:12 +00:00
fs *webfilesystem.WebFileSystem
2023-04-12 21:05:23 +00:00
manifest websiteapp.ApplicationManifest
}
2023-04-29 16:47:12 +00:00
func NewImgViewerApp(webFs *webfilesystem.WebFileSystem) ImgViewerApp {
2023-04-12 21:05:23 +00:00
newApp := ImgViewerApp{
2023-04-29 16:47:12 +00:00
fs: webFs,
2023-04-12 21:05:23 +00:00
manifest: websiteapp.ApplicationManifest{
AppId: "img-viewer",
WindowName: "About me", //TODO: delete
},
}
return newApp
}
func (p *ImgViewerApp) GetManifest() websiteapp.ApplicationManifest {
return p.manifest
}
func (p *ImgViewerApp) GetId() string {
return p.manifest.AppId
}
2023-04-29 16:47:12 +00:00
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")
2023-04-12 21:05:23 +00:00
}
2023-04-29 16:47:12 +00:00
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
2023-04-12 21:05:23 +00:00
}