package wde import ( "errors" "personalwebsite/webfilesystem" "go.mongodb.org/mongo-driver/bson/primitive" ) type ImagLib struct { fs *webfilesystem.WebFileSystem } func ReadImage(img *webfilesystem.WebFSFile) (*Img, error) { // header, ok := img.Data.(primitive.D).Map()["header"].(string) // if !ok { // return nil, errors.New("error in file decoding") // } // base64, ok := img.Data.(primitive.D).Map()["base64"].(string) // if !ok { // return nil, errors.New("error in file decoding") // } // base64min32, ok := img.Data.(primitive.D).Map()["base64min32"].(string) // if !ok { // return nil, errors.New("error in file decoding") // } 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"` }