personal-website/libs/imglib.go

67 lines
1.3 KiB
Go
Raw Normal View History

2023-05-02 00:12:43 +00:00
package libs
import (
2023-05-03 22:04:29 +00:00
"net/http"
"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) Route(route *gin.RouterGroup) {
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 01:00:22 +00:00
// _, err = l.fs.readFSDocs(fileId, &imgData)
// if err != nil {
// ctx.Status(http.StatusInternalServerError)
// }
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-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"`
}