From be13dcd5253bf78dc36f75944d39597b450b22bc Mon Sep 17 00:00:00 2001 From: cyber-dream Date: Thu, 4 May 2023 01:04:29 +0300 Subject: [PATCH] Change libs routes --- libs/imglib.go | 27 +++++++++++++++++++++ libs/libs.go | 15 ++++++++++++ main.go | 65 +++++++++++++++++++------------------------------- 3 files changed, 67 insertions(+), 40 deletions(-) create mode 100644 libs/libs.go diff --git a/libs/imglib.go b/libs/imglib.go index 09d1414..570c4ff 100644 --- a/libs/imglib.go +++ b/libs/imglib.go @@ -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 { diff --git a/libs/libs.go b/libs/libs.go new file mode 100644 index 0000000..f916e33 --- /dev/null +++ b/libs/libs.go @@ -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), + } +} diff --git a/main.go b/main.go index 3022afe..768e698 100644 --- a/main.go +++ b/main.go @@ -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")