|
1 | 1 | import * as path from "path" |
2 | 2 | import * as os from "os" |
3 | 3 | import * as vscode from "vscode" |
| 4 | +import * as fs from "fs/promises" |
4 | 5 | import { getWorkspacePath } from "../../utils/path" |
5 | 6 | import { t } from "../../i18n" |
6 | 7 |
|
@@ -42,7 +43,6 @@ export async function openImage(dataUriOrPath: string, options?: { values?: { ac |
42 | 43 | return |
43 | 44 | } |
44 | 45 |
|
45 | | - // Handle data URI (existing logic) |
46 | 46 | const matches = dataUriOrPath.match(/^data:image\/([a-zA-Z]+);base64,(.+)$/) |
47 | 47 | if (!matches) { |
48 | 48 | vscode.window.showErrorMessage(t("common:errors.invalid_data_uri")) |
@@ -90,6 +90,108 @@ export async function openImage(dataUriOrPath: string, options?: { values?: { ac |
90 | 90 | } |
91 | 91 | } |
92 | 92 |
|
| 93 | +/** |
| 94 | + * Save a pasted/dropped image to global storage and return its path and webview URI |
| 95 | + * This uses VSCode's global storage for persistence across sessions |
| 96 | + */ |
| 97 | +export async function importImageToGlobalStorage( |
| 98 | + imagePath: string, |
| 99 | + provider?: any, |
| 100 | +): Promise<{ imagePath: string; imageUri: string } | null> { |
| 101 | + try { |
| 102 | + // Determine storage directory (global storage preferred, fallback to temp) |
| 103 | + let imagesDir: string |
| 104 | + if (provider?.contextProxy?.globalStorageUri) { |
| 105 | + const globalStoragePath = provider.contextProxy.globalStorageUri.fsPath |
| 106 | + const taskId = provider.getCurrentTask?.()?.taskId |
| 107 | + imagesDir = taskId |
| 108 | + ? path.join(globalStoragePath, "user-images", `task-${taskId}`) |
| 109 | + : path.join(globalStoragePath, "user-images", "general") |
| 110 | + } else { |
| 111 | + console.warn("Provider context not available, falling back to temp directory") |
| 112 | + imagesDir = path.join(os.tmpdir(), "roo-user-images") |
| 113 | + } |
| 114 | + |
| 115 | + await fs.mkdir(imagesDir, { recursive: true }) |
| 116 | + |
| 117 | + // Preserve original extension if possible |
| 118 | + const ext = path.extname(imagePath) || ".png" |
| 119 | + const timestamp = Date.now() |
| 120 | + const randomId = Math.random().toString(36).substring(2, 8) |
| 121 | + const destFileName = `imported_image_${timestamp}_${randomId}${ext}` |
| 122 | + const destPath = path.join(imagesDir, destFileName) |
| 123 | + |
| 124 | + // Copy the original image into global storage |
| 125 | + await fs.copyFile(imagePath, destPath) |
| 126 | + |
| 127 | + // Convert to webview URI |
| 128 | + let webviewUri = provider?.convertToWebviewUri?.(destPath) ?? vscode.Uri.file(destPath).toString() |
| 129 | + |
| 130 | + return { imagePath: destPath, imageUri: webviewUri } |
| 131 | + } catch (error) { |
| 132 | + console.error("Failed to import image into global storage:", error) |
| 133 | + return null |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +export async function savePastedImageToTemp( |
| 138 | + dataUri: string, |
| 139 | + provider?: any, |
| 140 | +): Promise<{ imagePath: string; imageUri: string } | null> { |
| 141 | + const matches = dataUri.match(/^data:image\/([a-zA-Z]+);base64,(.+)$/) |
| 142 | + if (!matches) { |
| 143 | + return null |
| 144 | + } |
| 145 | + |
| 146 | + const [, format, base64Data] = matches |
| 147 | + const imageBuffer = Buffer.from(base64Data, "base64") |
| 148 | + |
| 149 | + // Determine storage directory |
| 150 | + let imagesDir: string |
| 151 | + |
| 152 | + // Use global storage if provider context is available |
| 153 | + if (provider?.contextProxy?.globalStorageUri) { |
| 154 | + const globalStoragePath = provider.contextProxy.globalStorageUri.fsPath |
| 155 | + |
| 156 | + // Organize by task ID if available |
| 157 | + const taskId = provider.getCurrentTask?.()?.taskId |
| 158 | + if (taskId) { |
| 159 | + imagesDir = path.join(globalStoragePath, "pasted-images", `task-${taskId}`) |
| 160 | + } else { |
| 161 | + // Fallback to general pasted-images directory |
| 162 | + imagesDir = path.join(globalStoragePath, "pasted-images", "general") |
| 163 | + } |
| 164 | + } else { |
| 165 | + // Fallback to temp directory if provider context is not available |
| 166 | + console.warn("Provider context not available, falling back to temp directory") |
| 167 | + imagesDir = path.join(os.tmpdir(), "roo-pasted-images") |
| 168 | + } |
| 169 | + |
| 170 | + // Create directory if it doesn't exist |
| 171 | + await fs.mkdir(imagesDir, { recursive: true }) |
| 172 | + |
| 173 | + // Generate a unique filename |
| 174 | + const timestamp = Date.now() |
| 175 | + const randomId = Math.random().toString(36).substring(2, 8) |
| 176 | + const fileName = `pasted_image_${timestamp}_${randomId}.${format}` |
| 177 | + const imagePath = path.join(imagesDir, fileName) |
| 178 | + |
| 179 | + try { |
| 180 | + // Write the image to the file |
| 181 | + await fs.writeFile(imagePath, imageBuffer) |
| 182 | + |
| 183 | + // Convert to webview URI if provider is available |
| 184 | + let imageUri = provider?.convertToWebviewUri?.(imagePath) ?? vscode.Uri.file(imagePath).toString() |
| 185 | + |
| 186 | + // Do not append custom query params to VS Code webview URIs (can break auth token and cause 401) |
| 187 | + |
| 188 | + return { imagePath, imageUri } |
| 189 | + } catch (error) { |
| 190 | + console.error("Failed to save pasted image:", error) |
| 191 | + return null |
| 192 | + } |
| 193 | +} |
| 194 | + |
93 | 195 | export async function saveImage(dataUri: string) { |
94 | 196 | const matches = dataUri.match(/^data:image\/([a-zA-Z]+);base64,(.+)$/) |
95 | 197 | if (!matches) { |
|
0 commit comments