diff options
| author | Paul Buetow <paul@buetow.org> | 2026-04-19 23:13:06 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-04-19 23:13:06 +0300 |
| commit | d97741966a6c97b5db7df62873bd2ee9d99f0565 (patch) | |
| tree | 9d6bd23b89463bbb98b4965a4c9e0bff438b1b71 /internal/image | |
| parent | b714194839f4245a5a8fb4ec1282453ea7b77e62 (diff) | |
y4: wire ComicForge CLI providers and config
Diffstat (limited to 'internal/image')
| -rw-r--r-- | internal/image/gemini.go | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/internal/image/gemini.go b/internal/image/gemini.go index 57fa61a..85da3a8 100644 --- a/internal/image/gemini.go +++ b/internal/image/gemini.go @@ -12,6 +12,7 @@ import ( "image/png" "io" "net/http" + "os" "strings" "time" @@ -165,6 +166,47 @@ func (c *GeminiProvider) Search(ctx context.Context, opts *SearchOptions) ([]Sea return []SearchResult{result}, nil } +// IsAvailable reports whether the provider was initialized successfully. +func (c *GeminiProvider) IsAvailable() error { + return c.ensureReady() +} + +// GenerateImage renders the first generated image to outputFile. +func (c *GeminiProvider) GenerateImage(ctx context.Context, prompt, outputFile string) error { + if c == nil { + return fmt.Errorf("image provider is nil") + } + if ctx == nil { + ctx = context.Background() + } + if strings.TrimSpace(prompt) == "" { + return fmt.Errorf("prompt is required") + } + if strings.TrimSpace(outputFile) == "" { + return fmt.Errorf("output file is required") + } + results, err := c.Search(ctx, &SearchOptions{Query: prompt}) + if err != nil { + return err + } + if len(results) == 0 { + return fmt.Errorf("no image results returned") + } + rc, err := c.Download(ctx, results[0].URL) + if err != nil { + return err + } + defer rc.Close() + data, err := io.ReadAll(rc) + if err != nil { + return fmt.Errorf("read image data: %w", err) + } + if err := os.WriteFile(outputFile, data, 0o644); err != nil { + return fmt.Errorf("write image: %w", err) + } + return nil +} + // Download returns the image bytes for a data URI or a remote URL. func (c *GeminiProvider) Download(ctx context.Context, url string) (io.ReadCloser, error) { if strings.HasPrefix(url, geminiDataPrefix) { |
