processImageURLPart handles MessagePartTypeImageURL. Only data: URIs are supported; remote http(s):// URLs are rejected. Callers with a remote URL should download the file locally first and pass it as a MessagePartTypeFile instead.
(part MessagePart)
| 161 | // Callers with a remote URL should download the file locally first and |
| 162 | // pass it as a MessagePartTypeFile instead. |
| 163 | func processImageURLPart(part MessagePart) (Document, *ImageResizeResult, error) { |
| 164 | if part.ImageURL == nil { |
| 165 | return Document{}, nil, errors.New("ProcessAttachment: image-url part has nil ImageURL field") |
| 166 | } |
| 167 | rawURL := part.ImageURL.URL |
| 168 | |
| 169 | switch { |
| 170 | case strings.HasPrefix(rawURL, "data:"): |
| 171 | mimeType, data, err := parseDataURI(rawURL) |
| 172 | if err != nil { |
| 173 | return Document{}, nil, fmt.Errorf("ProcessAttachment: parse data URI: %w", err) |
| 174 | } |
| 175 | // When content detection returns an image type, prefer it over the |
| 176 | // declared MIME. (Only image types are trusted from the sniffer.) |
| 177 | if detected := DetectMimeTypeByContent(data); IsImageMimeType(detected) { |
| 178 | mimeType = detected |
| 179 | } |
| 180 | return transcodeImageWithMeta("image", data, mimeType) |
| 181 | |
| 182 | case strings.HasPrefix(rawURL, "http://") || strings.HasPrefix(rawURL, "https://"): |
| 183 | return Document{}, nil, errors.New("attachment: remote URLs are not supported; download the file locally first") |
| 184 | |
| 185 | default: |
| 186 | return Document{}, nil, fmt.Errorf("attachment: unsupported image URL scheme: %q", rawURL) |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | // processDocumentPart handles MessagePartTypeDocument. |
| 191 | // Images with InlineData are transcoded; other already-resolved documents pass through. |
no test coverage detected