]> git.giacomolli.uk - onechat/blobdiff - image.go
Show stored images to the user
[onechat] / image.go
index d6a0b20c95461f7a9f2f1b8ac83f762b93ec7e71..4b23f7c9b139f2c697ce5fc92968636cb3e38d90 100644 (file)
--- a/image.go
+++ b/image.go
@@ -22,6 +22,11 @@ const (
        imageMaxBytes = 20 << 20
 )
 
        imageMaxBytes = 20 << 20
 )
 
+var (
+       errImageSize   = errors.New("image exceeds 20 MB")
+       errImageFormat = errors.New("use a JPEG, PNG, or WebP image")
+)
+
 // ImageStore holds immutable images shared by the root, children, and
 // subagents. Names are the SHA-256 of the bytes with the decoded format as
 // extension. Publication is atomic and does not replace an existing image.
 // ImageStore holds immutable images shared by the root, children, and
 // subagents. Names are the SHA-256 of the bytes with the decoded format as
 // extension. Publication is atomic and does not replace an existing image.
@@ -97,12 +102,12 @@ func (s *ImageStore) Read(name string) ([]byte, error) {
 
 func imageFormat(data []byte) (string, error) {
        if len(data) > imageMaxBytes {
 
 func imageFormat(data []byte) (string, error) {
        if len(data) > imageMaxBytes {
-               return "", errors.New("image exceeds 20 MB")
+               return "", errImageSize
        }
 
        config, format, err := image.DecodeConfig(bytes.NewReader(data))
        if err != nil || config.Width <= 0 || config.Height <= 0 {
        }
 
        config, format, err := image.DecodeConfig(bytes.NewReader(data))
        if err != nil || config.Width <= 0 || config.Height <= 0 {
-               return "", errors.New("use a JPEG, PNG, or WebP image")
+               return "", errImageFormat
        }
 
        switch format {
        }
 
        switch format {
@@ -111,7 +116,7 @@ func imageFormat(data []byte) (string, error) {
        case "png", "webp":
                return format, nil
        default:
        case "png", "webp":
                return format, nil
        default:
-               return "", errors.New("use a JPEG, PNG, or WebP image")
+               return "", errImageFormat
        }
 }
 
        }
 }
 
@@ -136,7 +141,7 @@ func imageReadFile(path string) (data []byte, err error) {
        }
 
        if len(data) > imageMaxBytes {
        }
 
        if len(data) > imageMaxBytes {
-               return nil, errors.New("image exceeds 20 MB")
+               return nil, errImageSize
        }
 
        return data, nil
        }
 
        return data, nil
@@ -162,13 +167,14 @@ func imageFiles(text string) []string {
 }
 
 func imageText(names []string, caption string) string {
 }
 
 func imageText(names []string, caption string) string {
-       var text strings.Builder
-
+       lines := make([]string, 0, len(names)+1)
        for _, name := range names {
        for _, name := range names {
-               text.WriteString("[image " + name + "]\n")
+               lines = append(lines, "[image "+name+"]")
        }
 
        }
 
-       text.WriteString(caption)
+       if caption != "" {
+               lines = append(lines, caption)
+       }
 
 
-       return strings.TrimSuffix(text.String(), "\n")
+       return strings.Join(lines, "\n")
 }
 }