personal-website/libs/imglib.go

67 lines
1.3 KiB
Go

package libs
import (
"net/http"
"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) Route(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
}
// _, err = l.fs.readFSDocs(fileId, &imgData)
// if err != nil {
// ctx.Status(http.StatusInternalServerError)
// }
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"`
}