personal-website/libs/imglib.go

56 lines
1.2 KiB
Go

package libs
import (
"errors"
"personalwebsite/webfilesystem"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type ImagLib struct {
fs *webfilesystem.WebFileSystem
}
func ReadImage(img *webfilesystem.WebFSFile) (*Img, error) {
data, ok := img.Data.(primitive.D).Map()["srcdata"]
if !ok {
return nil, errors.New("error in file decoding")
}
bin := data.(primitive.Binary).Data
min32Data, ok := img.Data.(primitive.D).Map()["min32data"]
if !ok {
return nil, errors.New("error in file decoding")
}
min32Bin := min32Data.(primitive.Binary).Data
return &Img{
// Header: header,
Data: bin,
Miniature32: min32Bin,
}, nil
}
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"`
Data []byte `bson:"srcdata"`
Miniature32 []byte `bson:"min32data"`
}