| 1 | import { exec } from "child_process"; |
| 2 | import { readFile, unlink } from "fs/promises"; |
| 3 | import os from "os"; |
| 4 | import path from "path"; |
| 5 | import { promisify } from "util"; |
| 6 | |
| 7 | import { logger } from "./logger.js"; |
| 8 | |
| 9 | const execAsync = promisify(exec); |
| 10 | |
| 11 | /** |
| 12 | * Check if clipboard contains an image |
| 13 | * @returns Promise<boolean> - true if clipboard contains an image |
| 14 | */ |
| 15 | export async function checkClipboardForImage(): Promise<boolean> { |
| 16 | try { |
| 17 | const platform = os.platform(); |
| 18 | |
| 19 | if (platform === "darwin") { |
| 20 | // macOS: Check clipboard using osascript |
| 21 | const { stdout } = await execAsync('osascript -e "clipboard info"'); |
| 22 | return ( |
| 23 | stdout.includes("«class PNGf»") || |
| 24 | stdout.includes("«class TIFF»") || |
| 25 | stdout.includes("picture") |
| 26 | ); |
| 27 | } else if (platform === "win32") { |
| 28 | // Windows: Use PowerShell to check clipboard |
| 29 | const { stdout } = await execAsync( |
| 30 | 'powershell -command "Get-Clipboard -Format Image | Measure-Object | Select-Object -ExpandProperty Count"', |
| 31 | ); |
| 32 | return parseInt(stdout.trim()) > 0; |
| 33 | } else if (platform === "linux") { |
| 34 | // Linux: Check if xclip can get image data |
| 35 | try { |
| 36 | await execAsync( |
| 37 | "xclip -selection clipboard -t image/png -o > /dev/null 2>&1", |
| 38 | ); |
| 39 | return true; |
| 40 | } catch { |
| 41 | return false; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | return false; |
| 46 | } catch (error) { |
| 47 | logger.debug("Error checking clipboard for image:", error); |
| 48 | return false; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Get image from clipboard as a Buffer |
| 54 | * @returns Promise<Buffer | null> - Image buffer if available, null otherwise |
| 55 | */ |
| 56 | export async function getClipboardImage(): Promise<Buffer | null> { |
| 57 | try { |
| 58 | const platform = os.platform(); |
| 59 | const tempDir = os.tmpdir(); |
| 60 | const tempImagePath = path.join( |
| 61 | tempDir, |
| 62 | `continue-clipboard-${Date.now()}.png`, |
| 63 | ); |
| 64 | |
| 65 | if (platform === "darwin") { |
| 66 | // macOS: Save clipboard image using osascript |
| 67 | await execAsync( |
| 68 | `osascript -e 'set the clipboard to (the clipboard as «class PNGf»)' -e 'set png_data to (the clipboard as «class PNGf»)' -e 'set file_ref to open for access "${tempImagePath}" with write permission' -e 'write png_data to file_ref' -e 'close access file_ref'`, |
| 69 | ); |
| 70 | } else if (platform === "win32") { |
| 71 | // Windows: Use PowerShell to save clipboard image |
| 72 | await execAsync( |
| 73 | `powershell -command "$image = Get-Clipboard -Format Image; if ($image) { $image.Save('${tempImagePath}') }"`, |
| 74 | ); |
| 75 | } else if (platform === "linux") { |
| 76 | // Linux: Use xclip to save clipboard image |
| 77 | await execAsync( |
| 78 | `xclip -selection clipboard -t image/png -o > "${tempImagePath}"`, |
| 79 | ); |
| 80 | } else { |
| 81 | return null; |
| 82 | } |
| 83 | |
| 84 | // Read the temporary file |
| 85 | const imageBuffer = await readFile(tempImagePath); |
| 86 | |
| 87 | // Clean up the temporary file |
| 88 | await unlink(tempImagePath).catch(() => { |
| 89 | // Ignore cleanup errors |
| 90 | }); |
| 91 | |
| 92 | return imageBuffer; |
| 93 | } catch (error) { |
| 94 | logger.debug("Error reading image from clipboard:", error); |
| 95 | return null; |
| 96 | } |
| 97 | } |