loadInputImages reads the given file paths into imagegen.Image values, inferring the MIME/extension from the bytes.
(paths []string)
| 259 | // loadInputImages reads the given file paths into imagegen.Image values, |
| 260 | // inferring the MIME/extension from the bytes. |
| 261 | func loadInputImages(paths []string) ([]imagegen.Image, error) { |
| 262 | out := make([]imagegen.Image, 0, len(paths)) |
| 263 | for _, p := range paths { |
| 264 | data, err := os.ReadFile(p) //#nosec G304 -- user/agent-specified image path for @image edit |
| 265 | if err != nil { |
| 266 | return nil, fmt.Errorf("read %q: %w", p, err) |
| 267 | } |
| 268 | mime, ok := models.DetectImageMediaType(data) |
| 269 | if !ok { |
| 270 | return nil, fmt.Errorf("%q is not a supported image", p) |
| 271 | } |
| 272 | ext := strings.TrimPrefix(filepath.Ext(p), ".") |
| 273 | if ext == "" { |
| 274 | ext = strings.TrimPrefix(mime, "image/") |
| 275 | } |
| 276 | out = append(out, imagegen.Image{Data: data, Mime: mime, Ext: ext}) |
| 277 | } |
| 278 | if len(out) == 0 { |
| 279 | return nil, errors.New("no readable input images") |
| 280 | } |
| 281 | return out, nil |
| 282 | } |
| 283 | |
| 284 | // writeImages saves the images. With one image and an out file path, it writes |
| 285 | // there; otherwise it writes into out (a directory) or temp files. |
no test coverage detected