| 36 | } |
| 37 | |
| 38 | func (c *SlidesInsertImageCmd) Run(ctx context.Context, flags *RootFlags) error { |
| 39 | u := ui.FromContext(ctx) |
| 40 | |
| 41 | presentationID := strings.TrimSpace(c.PresentationID) |
| 42 | if presentationID == "" { |
| 43 | return usage("empty presentationId") |
| 44 | } |
| 45 | slideID := strings.TrimSpace(c.SlideID) |
| 46 | if slideID == "" { |
| 47 | return usage("empty slideId") |
| 48 | } |
| 49 | if c.Width <= 0 { |
| 50 | return usage("--width must be greater than 0") |
| 51 | } |
| 52 | if c.Height < 0 { |
| 53 | return usage("--height cannot be negative") |
| 54 | } |
| 55 | |
| 56 | source, err := resolveSlidesImageSource(c.Image, c.URL) |
| 57 | if err != nil { |
| 58 | return err |
| 59 | } |
| 60 | |
| 61 | // Resolve height from the image's aspect ratio when not supplied. |
| 62 | height := c.Height |
| 63 | if height == 0 { |
| 64 | if source.imageURL != "" { |
| 65 | return usage("--height is required with --url") |
| 66 | } |
| 67 | ar, aspectErr := imageAspectRatio(source.localPath) |
| 68 | if aspectErr != nil { |
| 69 | return fmt.Errorf("determine image aspect ratio (pass --height to skip): %w", aspectErr) |
| 70 | } |
| 71 | height = c.Width * ar |
| 72 | } |
| 73 | |
| 74 | dryRunPayload := map[string]any{ |
| 75 | "presentation_id": presentationID, |
| 76 | "slide_id": slideID, |
| 77 | "x": c.X, |
| 78 | "y": c.Y, |
| 79 | "width": c.Width, |
| 80 | "height": height, |
| 81 | "unit": c.Unit, |
| 82 | } |
| 83 | if source.imageURL != "" { |
| 84 | dryRunPayload["url"] = source.imageURL |
| 85 | } else { |
| 86 | dryRunPayload["image"] = source.localPath |
| 87 | dryRunPayload["mime_type"] = source.mimeType |
| 88 | } |
| 89 | if dryRunErr := dryRunExit(ctx, flags, "slides.insert-image", dryRunPayload); dryRunErr != nil { |
| 90 | return dryRunErr |
| 91 | } |
| 92 | |
| 93 | account, err := requireAccount(flags) |
| 94 | if err != nil { |
| 95 | return err |