* Download and save images from a `generate()` response to disk. * * Handles both `"url"` (CDN download) and `"base64"` response formats * and creates intermediate directories as needed. Returns the absolute * paths of all saved files.
(response: ImageResponse, options: ImageSaveOptions = {})
| 42 | * paths of all saved files. |
| 43 | */ |
| 44 | async save(response: ImageResponse, options: ImageSaveOptions = {}): Promise<string[]> { |
| 45 | const fmt = options.responseFormat || 'url'; |
| 46 | const quiet = options.quiet ?? true; |
| 47 | const saved: string[] = []; |
| 48 | |
| 49 | if (options.out) { |
| 50 | // Single-image, exact path |
| 51 | const count = (fmt === 'base64' |
| 52 | ? (response.data.image_base64 || []).length |
| 53 | : (response.data.image_urls || []).length); |
| 54 | |
| 55 | if (count > 1) { |
| 56 | throw new SDKError( |
| 57 | 'Cannot use `out` with multiple images. Use `outDir` instead.', |
| 58 | ExitCode.USAGE, |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | const destPath = resolve(options.out); |
| 63 | const dir = dirname(destPath); |
| 64 | if (!existsSync(dir)) mkdirSync(dir, { recursive: true }); |
| 65 | |
| 66 | if (fmt === 'base64') { |
| 67 | const image = (response.data.image_base64 || [])[0]; |
| 68 | if (image) writeFileSync(destPath, image, 'base64'); |
| 69 | } else { |
| 70 | const imageUrl = (response.data.image_urls || [])[0]; |
| 71 | if (imageUrl) await downloadFile(imageUrl, destPath, { quiet }); |
| 72 | } |
| 73 | saved.push(destPath); |
| 74 | } else { |
| 75 | // Multi-image, numbered filenames in a directory |
| 76 | const outDir = resolve(options.outDir || '.'); |
| 77 | if (!existsSync(outDir)) mkdirSync(outDir, { recursive: true }); |
| 78 | const prefix = options.prefix || 'image'; |
| 79 | |
| 80 | if (fmt === 'base64') { |
| 81 | const images = response.data.image_base64 || []; |
| 82 | for (let i = 0; i < images.length; i++) { |
| 83 | const destPath = join(outDir, `${prefix}_${String(i + 1).padStart(3, '0')}.jpg`); |
| 84 | writeFileSync(destPath, images[i]!, 'base64'); |
| 85 | saved.push(destPath); |
| 86 | } |
| 87 | } else { |
| 88 | const imageUrls = response.data.image_urls || []; |
| 89 | for (let i = 0; i < imageUrls.length; i++) { |
| 90 | const destPath = join(outDir, `${prefix}_${String(i + 1).padStart(3, '0')}.jpg`); |
| 91 | await downloadFile(imageUrls[i]!, destPath, { quiet }); |
| 92 | saved.push(destPath); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return saved; |
| 98 | } |
| 99 | |
| 100 | private validateParams(params: Partial<ImageRequest>): ImageRequest { |
| 101 | const { width, height, aspect_ratio } = params; |
no test coverage detected