X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/36229ef68a30c5a117599037d79fa1fc708fbfef..add3cf6416e33c24331fd1e90e99284f44d2aae2:/image.go?ds=inline diff --git a/image.go b/image.go index d6a0b20..4b23f7c 100644 --- a/image.go +++ b/image.go @@ -22,6 +22,11 @@ const ( 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. @@ -97,12 +102,12 @@ func (s *ImageStore) Read(name string) ([]byte, error) { 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 { - return "", errors.New("use a JPEG, PNG, or WebP image") + return "", errImageFormat } switch format { @@ -111,7 +116,7 @@ func imageFormat(data []byte) (string, error) { 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 { - return nil, errors.New("image exceeds 20 MB") + return nil, errImageSize } return data, nil @@ -162,13 +167,14 @@ func imageFiles(text string) []string { } func imageText(names []string, caption string) string { - var text strings.Builder - + lines := make([]string, 0, len(names)+1) 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") }