-
Notifications
You must be signed in to change notification settings - Fork 1.4k
image attachment support for CLI #1541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import * as vscode from 'vscode'; | ||
| import { IVSCodeExtensionContext } from '../../../platform/extContext/common/extensionContext'; | ||
| import { URI } from '../../../util/vs/base/common/uri'; | ||
|
|
||
| export class ImageStorage { | ||
| private readonly storageDir: URI; | ||
|
|
||
| constructor(private readonly context: IVSCodeExtensionContext) { | ||
| this.storageDir = URI.joinPath(this.context.globalStorageUri, 'copilot-cli-images'); | ||
| this.initialize(); | ||
| } | ||
|
|
||
| private async initialize(): Promise<void> { | ||
| try { | ||
| await vscode.workspace.fs.createDirectory(this.storageDir); | ||
| await this.cleanupOldImages(); | ||
| } catch (error) { | ||
| console.error('ImageStorage: Failed to initialize', error); | ||
| } | ||
| } | ||
|
Comment on lines
+10
to
+25
|
||
|
|
||
| async storeImage(imageData: Uint8Array, mimeType: string): Promise<URI> { | ||
| const timestamp = Date.now(); | ||
| const randomId = Math.random().toString(36).substring(2, 10); | ||
| const extension = this.getExtension(mimeType); | ||
| const filename = `${timestamp}-${randomId}${extension}`; | ||
| const imageUri = URI.joinPath(this.storageDir, filename); | ||
|
|
||
| await vscode.workspace.fs.writeFile(imageUri, imageData); | ||
| return imageUri; | ||
| } | ||
|
|
||
| async getImage(uri: URI): Promise<Uint8Array | undefined> { | ||
| try { | ||
| const data = await vscode.workspace.fs.readFile(uri); | ||
| return data; | ||
| } catch { | ||
| return undefined; | ||
| } | ||
| } | ||
|
|
||
| async deleteImage(uri: URI): Promise<void> { | ||
| try { | ||
| await vscode.workspace.fs.delete(uri); | ||
| } catch { | ||
| // Already deleted | ||
| } | ||
| } | ||
|
|
||
| async cleanupOldImages(maxAgeMs: number = 7 * 24 * 60 * 60 * 1000): Promise<void> { | ||
| try { | ||
| const entries = await vscode.workspace.fs.readDirectory(this.storageDir); | ||
| const now = Date.now(); | ||
| const cutoff = now - maxAgeMs; | ||
|
|
||
| for (const [filename, fileType] of entries) { | ||
| if (fileType === vscode.FileType.File) { | ||
| const fileUri = URI.joinPath(this.storageDir, filename); | ||
| try { | ||
| const stat = await vscode.workspace.fs.stat(fileUri); | ||
| if (stat.mtime < cutoff) { | ||
| await vscode.workspace.fs.delete(fileUri); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be more efficient to do a Promise.all(..) |
||
| } | ||
| } catch { | ||
| // Skip files we can't access | ||
| } | ||
| } | ||
| } | ||
| } catch (error) { | ||
| console.error('ImageStorage: Failed to cleanup old images', error); | ||
|
Comment on lines
+23
to
+75
|
||
| } | ||
| } | ||
|
|
||
| private getExtension(mimeType: string): string { | ||
| const map: Record<string, string> = { | ||
| 'image/png': '.png', | ||
| 'image/jpeg': '.jpg', | ||
| 'image/jpg': '.jpg', | ||
| 'image/gif': '.gif', | ||
| 'image/webp': '.webp', | ||
| 'image/bmp': '.bmp', | ||
| }; | ||
| return map[mimeType.toLowerCase()] || '.bin'; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
initialize()method is called in the constructor but is async and not awaited. This means the storage directory creation and cleanup may not complete beforestoreImage()is called, potentially causing file operation failures. Consider making initialization synchronous or ensuring it completes before allowing image storage operations.