12345678910111213141516171819202122232425262728293031323334353637383940 |
- package utils
- import (
- "context"
- "encoding/base64"
- "file_upload/backend/global"
- "file_upload/backend/simple_zap"
- "os"
- "strings"
- )
- func ParseBase64ImageString(input, name string) error {
- parts := strings.Split(input, ",")
- if len(parts) < 2 {
- panic("Invalid Base64 image string")
- }
- contentTypePart := strings.TrimSpace(parts[0])
- contentTypeParts := strings.Split(contentTypePart, ";")
- _ = strings.TrimSpace(contentTypeParts[0])
- data := parts[1]
- decodeString, err := base64.StdEncoding.DecodeString(data)
- if err != nil {
- return err
- }
- optputpath := global.DownloadSetting.Path + "/" + name + ".png"
- create, err := os.Create(optputpath)
- if err != nil {
- simple_zap.WithCtx(context.TODO()).Sugar().Warn(err, "创建文件失败")
- return err
- }
- defer create.Close()
- _, err = create.Write(decodeString)
- if err != nil {
- simple_zap.WithCtx(context.TODO()).Sugar().Warn(err, "写入文件失败")
- return err
- }
- return err
- }
|