Change libs routes

This commit is contained in:
cyber-dream 2023-05-04 01:04:29 +03:00
parent 139831365a
commit be13dcd525
3 changed files with 67 additions and 40 deletions

View File

@ -2,8 +2,10 @@ package libs
import (
"errors"
"net/http"
"personalwebsite/webfilesystem"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/bson/primitive"
)
@ -11,6 +13,12 @@ type ImagLib struct {
fs *webfilesystem.WebFileSystem
}
func NewImgLib(webfs *webfilesystem.WebFileSystem) *ImagLib {
return &ImagLib{
fs: webfs,
}
}
func ReadImage(img *webfilesystem.WebFSFile) (*Img, error) {
data, ok := img.Data.(primitive.D).Map()["srcdata"]
if !ok {
@ -30,6 +38,25 @@ func ReadImage(img *webfilesystem.WebFSFile) (*Img, error) {
}, nil
}
func (l *ImagLib) Route(route *gin.RouterGroup) {
route.GET("get", func(ctx *gin.Context) {
path := ctx.Query("path")
if path == "" {
ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct
return
}
file, err := l.fs.Read(path)
if err != nil {
ctx.String(http.StatusInternalServerError, "TODO") //TODO
}
data := file.Data.(primitive.Binary)
ctx.Data(http.StatusOK, "image/jpeg", data.Data)
})
}
func GetBase64Image(img *Base64Img, min string) (string, error) {
imgString := ""
switch min {

15
libs/libs.go Normal file
View File

@ -0,0 +1,15 @@
package libs
import (
"personalwebsite/webfilesystem"
)
type Libs struct {
imglib *ImagLib
}
func NewLibs(webfs *webfilesystem.WebFileSystem) Libs {
return Libs{
imglib: NewImgLib(webfs),
}
}

65
main.go
View File

@ -13,6 +13,7 @@ import (
"personalwebsite/apps/finder"
imgviewer "personalwebsite/apps/img-viewer"
"personalwebsite/apps/personalprops"
"personalwebsite/libs"
"personalwebsite/routewde"
"personalwebsite/wde"
"personalwebsite/webfilesystem"
@ -20,7 +21,6 @@ import (
"github.com/gin-contrib/location"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
@ -119,52 +119,37 @@ func main() {
{
imgLibGroup := libsGroup.Group("img")
{
imgLibGroup.GET("get", func(ctx *gin.Context) {
path := ctx.Query("path")
if path == "" {
ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct
return
}
file, err := webfs.Read(path)
if err != nil {
ctx.String(http.StatusInternalServerError, "TODO") //TODO
}
data := file.Data.(primitive.Binary)
ctx.Data(http.StatusOK, "image/jpeg", data.Data)
})
imgLib := libs.NewImgLib(webfs)
imgLib.Route(imgLibGroup)
}
}
wdeGroup := system.Group("wde")
{
routewde.Route(wdeGroup, webde)
}
// apps := system.Group("applications")
// {
// apps.GET("/:appid/:method", func(ctx *gin.Context) {
// appId := ctx.Param("appid")
// method := ctx.Param("method")
apps := system.Group("applications")
{
apps.GET("/:appid/:method", func(ctx *gin.Context) {
appId := ctx.Param("appid")
method := ctx.Param("method")
// 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)
// }
// })
// }
// apps.Route(apps.Group("/storage"), &appsStorage)
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)
}
})
}
}
fs := router.Group("fs")