personal-website/libs/imglib.go

80 lines
1.6 KiB
Go
Raw Permalink Normal View History

2023-05-02 00:12:43 +00:00
package libs
import (
2023-05-03 22:04:29 +00:00
"net/http"
2023-07-22 15:55:23 +00:00
"path"
"personalwebsite/webfilesystem"
2023-05-03 22:04:29 +00:00
"github.com/gin-gonic/gin"
)
2023-05-01 12:32:41 +00:00
type ImagLib struct {
fs *webfilesystem.WebFileSystem
}
2023-05-03 22:04:29 +00:00
func NewImgLib(webfs *webfilesystem.WebFileSystem) *ImagLib {
return &ImagLib{
fs: webfs,
}
}
func (l *ImagLib) PublicRoutes(route *gin.RouterGroup) {
2023-05-03 22:04:29 +00:00
route.GET("get", func(ctx *gin.Context) {
path := ctx.Query("path")
if path == "" {
2023-05-07 00:14:30 +00:00
ctx.String(http.StatusBadRequest, "TODO") //TODO json error struct
2023-05-03 22:04:29 +00:00
return
}
2023-05-07 00:14:30 +00:00
imgData := Img{}
2023-05-07 01:00:22 +00:00
_, err := l.fs.Read(path, &imgData)
2023-05-03 22:04:29 +00:00
if err != nil {
2023-05-07 00:14:30 +00:00
ctx.Status(http.StatusInternalServerError)
2023-05-07 01:00:22 +00:00
return
2023-05-03 22:04:29 +00:00
}
2023-05-07 00:14:30 +00:00
ctx.Data(http.StatusOK, "image/jpeg", imgData.Bin)
2023-05-03 22:04:29 +00:00
})
2023-07-22 15:55:23 +00:00
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)
})
2023-05-03 22:04:29 +00:00
}
2023-05-01 12:32:41 +00:00
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 {
2023-05-01 12:32:41 +00:00
Header string `bson:"header"`
Base64 string `bson:"base64"`
Base64Miniature32 string `bson:"base64min32"`
}
type Img struct {
2023-05-07 00:14:30 +00:00
Header string `bson:"header"`
Bin []byte `bson:"bin"`
// BinMin32 []byte `bson:"binmin32"`
}