|
1 | 1 | import * as vscode from 'vscode'; |
2 | 2 | import * as path from 'node:path'; |
| 3 | +import * as os from 'node:os'; |
3 | 4 | import WebSocket from 'ws'; |
4 | 5 | import { WebChatEventType, WsCommandType, type WebChatEvent } from 'sbot.commons'; |
5 | 6 | import { SbotClient } from './SbotClient'; |
@@ -180,6 +181,8 @@ export class ChatViewProvider implements vscode.WebviewViewProvider, vscode.Disp |
180 | 181 | return client.fetchThinks(args[0]); |
181 | 182 | case 'listShells': |
182 | 183 | return client.listShells(); |
| 184 | + case 'downloadFile': |
| 185 | + return this.downloadFile(args[0]); |
183 | 186 | default: |
184 | 187 | throw new Error(`Unknown RPC method: ${method}`); |
185 | 188 | } |
@@ -261,6 +264,48 @@ export class ChatViewProvider implements vscode.WebviewViewProvider, vscode.Disp |
261 | 264 | this.ptySockets.clear(); |
262 | 265 | } |
263 | 266 |
|
| 267 | + // ── Download ── |
| 268 | + // The webview can't navigate to the server URL (CSP + server connection lives here), |
| 269 | + // so the host fetches the bytes and saves them via the native save dialog. |
| 270 | + |
| 271 | + private async downloadFile(filePath: string): Promise<void> { |
| 272 | + const name = filePath.split(/[\\/]/).pop() || 'download'; |
| 273 | + const baseDir = this.getWorkspaceFolder() ?? os.homedir(); |
| 274 | + const target = await vscode.window.showSaveDialog({ |
| 275 | + defaultUri: vscode.Uri.file(path.join(baseDir, name)), |
| 276 | + saveLabel: 'Download', |
| 277 | + }); |
| 278 | + if (!target) return; // user cancelled the save dialog |
| 279 | + |
| 280 | + const client = this.ensureClient(); |
| 281 | + let cancelled = false; |
| 282 | + try { |
| 283 | + await vscode.window.withProgress( |
| 284 | + { location: vscode.ProgressLocation.Notification, title: `Downloading ${name}`, cancellable: true }, |
| 285 | + async (progress, token) => { |
| 286 | + const controller = new AbortController(); |
| 287 | + token.onCancellationRequested(() => { cancelled = true; controller.abort(); }); |
| 288 | + let lastPct = 0; |
| 289 | + await client.downloadToFile(filePath, target.fsPath, { |
| 290 | + signal: controller.signal, |
| 291 | + onProgress: (loaded, total) => { |
| 292 | + if (!total) { progress.report({ message: formatBytes(loaded) }); return; } |
| 293 | + const pct = Math.floor((loaded / total) * 100); |
| 294 | + if (pct > lastPct) { |
| 295 | + progress.report({ increment: pct - lastPct, message: `${pct}% · ${formatBytes(loaded)} / ${formatBytes(total)}` }); |
| 296 | + lastPct = pct; |
| 297 | + } |
| 298 | + }, |
| 299 | + }); |
| 300 | + }, |
| 301 | + ); |
| 302 | + void vscode.window.showInformationMessage(`Downloaded: ${target.fsPath}`); |
| 303 | + } catch (e: any) { |
| 304 | + if (cancelled) return; // user aborted — stay quiet |
| 305 | + void vscode.window.showErrorMessage(`Download failed: ${e?.message ?? e}`); |
| 306 | + } |
| 307 | + } |
| 308 | + |
264 | 309 | private onServerEvent = (event: WebChatEvent) => { |
265 | 310 | const chatType = EVENT_TYPE_MAP[event.type]; |
266 | 311 | if (!chatType) return; |
@@ -328,6 +373,14 @@ function getNonce(): string { |
328 | 373 | return text; |
329 | 374 | } |
330 | 375 |
|
| 376 | +function formatBytes(bytes: number): string { |
| 377 | + if (!bytes || bytes < 0) return '0 B'; |
| 378 | + const units = ['B', 'KB', 'MB', 'GB', 'TB']; |
| 379 | + const i = Math.min(units.length - 1, Math.floor(Math.log(bytes) / Math.log(1024))); |
| 380 | + const value = bytes / Math.pow(1024, i); |
| 381 | + return `${i === 0 ? value : value.toFixed(1)} ${units[i]}`; |
| 382 | +} |
| 383 | + |
331 | 384 | function isLocalBaseUrl(baseUrl: string): boolean { |
332 | 385 | try { |
333 | 386 | const { hostname } = new URL(baseUrl); |
|
0 commit comments