personal-website/libs/imglib.go

80 lines
1.6 KiB
Go

package libs
import (
"net/http"
"path"
"personalwebsite/webfilesystem"
"github.com/gin-gonic/gin"
)
type ImagLib struct {
fs *webfilesystem.WebFileSystem
}
func NewImgLib(webfs *webfilesystem.WebFileSystem) *ImagLib {
return &ImagLib{
fs: webfs,
}
}
func (l *ImagLib) PublicRoutes(route *gin.RouterGroup) {
route.GET("get", func(ctx *gin.Context) {
path := ctx.Query("path")
if path == "" {
ctx.String(http.StatusBadRequest, "TODO") //TODO json error struct
return
}
imgData := Img{}
_, err := l.fs.Read(path, &imgData)
if err != nil {
ctx.Status(http.StatusInternalServerError)
return
}
ctx.Data(http.StatusOK, "image/jpeg", imgData.Bin)
})
iconGroup := route.Group("icon")
iconGroup.GET("get", func(ctx *gin.Context) {
iconPath := ctx.Query("path")
iconType := ctx.Query("type")
_ = iconType
iconSize := ctx.Query("size")
imgData := Img{}
_, err := l.fs.Read(path.Join(iconPath, "color", iconSize+".png"), &imgData)
if err != nil {
ctx.Status(http.StatusInternalServerError)
return
}
ctx.Data(http.StatusOK, "image/jpeg", imgData.Bin)
})
}
func GetBase64Image(img *Base64Img, min string) (string, error) {
imgString := ""
switch min {
case "min32":
imgString = "data:" + img.Header + ", " + img.Base64Miniature32
default:
imgString = "data:" + img.Header + ", " + img.Base64
}
return imgString, nil
}
type Base64Img struct {
Header string `bson:"header"`
Base64 string `bson:"base64"`
Base64Miniature32 string `bson:"base64min32"`
}
type Img struct {
Header string `bson:"header"`
Bin []byte `bson:"bin"`
// BinMin32 []byte `bson:"binmin32"`
}