Skip to content

Commit f0f62e3

Browse files
authored
error warning when attached image is too large (#240316)
add warning if image is too alrge
1 parent 3088b6e commit f0f62e3

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/vs/workbench/contrib/chat/browser/chatDragAndDrop.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { URI } from '../../../../base/common/uri.js';
1515
import { IRange } from '../../../../editor/common/core/range.js';
1616
import { SymbolKinds } from '../../../../editor/common/languages.js';
1717
import { localize } from '../../../../nls.js';
18+
import { IDialogService } from '../../../../platform/dialogs/common/dialogs.js';
1819
import { CodeDataTransfers, containsDragType, DocumentSymbolTransferData, extractEditorsDropData, extractSymbolDropData, IDraggedResourceEditorInput } from '../../../../platform/dnd/browser/dnd.js';
1920
import { FileType, IFileService, IFileSystemProvider } from '../../../../platform/files/common/files.js';
2021
import { IThemeService, Themable } from '../../../../platform/theme/common/themeService.js';
@@ -49,6 +50,7 @@ export class ChatDragAndDrop extends Themable {
4950
@IExtensionService private readonly extensionService: IExtensionService,
5051
@IFileService protected readonly fileService: IFileService,
5152
@IEditorService protected readonly editorService: IEditorService,
53+
@IDialogService protected readonly dialogService: IDialogService
5254
) {
5355
super(themeService);
5456

@@ -233,7 +235,7 @@ export class ChatDragAndDrop extends Themable {
233235

234236
private async resolveAttachContext(editorInput: IDraggedResourceEditorInput): Promise<IChatRequestVariableEntry | undefined> {
235237
// Image
236-
const imageContext = await getImageAttachContext(editorInput, this.fileService);
238+
const imageContext = await getImageAttachContext(editorInput, this.fileService, this.dialogService);
237239
if (imageContext) {
238240
return this.extensionService.extensions.some(ext => isProposedApiEnabled(ext, 'chatReferenceBinaryData')) ? imageContext : undefined;
239241
}
@@ -350,8 +352,9 @@ export class EditsDragAndDrop extends ChatDragAndDrop {
350352
@IExtensionService extensionService: IExtensionService,
351353
@IFileService fileService: IFileService,
352354
@IEditorService editorService: IEditorService,
355+
@IDialogService dialogService: IDialogService
353356
) {
354-
super(attachmentModel, styles, themeService, extensionService, fileService, editorService);
357+
super(attachmentModel, styles, themeService, extensionService, fileService, editorService, dialogService);
355358
}
356359

357360
protected override handleDrop(context: IChatRequestVariableEntry[]): void {
@@ -424,14 +427,18 @@ function getResourceAttachContext(resource: URI, isDirectory: boolean): IChatReq
424427
};
425428
}
426429

427-
async function getImageAttachContext(editor: EditorInput | IDraggedResourceEditorInput, fileService: IFileService): Promise<IChatRequestVariableEntry | undefined> {
430+
async function getImageAttachContext(editor: EditorInput | IDraggedResourceEditorInput, fileService: IFileService, dialogService: IDialogService): Promise<IChatRequestVariableEntry | undefined> {
428431
if (!editor.resource) {
429432
return undefined;
430433
}
431434

432435
if (/\.(png|jpg|jpeg|gif|webp)$/i.test(editor.resource.path)) {
433436
const fileName = basename(editor.resource);
434437
const readFile = await fileService.readFile(editor.resource);
438+
if (readFile.size > 30 * 1024 * 1024) { // 30 MB
439+
dialogService.error(localize('imageTooLarge', 'Image is too large'), localize('imageTooLargeMessage', 'The image {0} is too large to be attached.', fileName));
440+
throw new Error('Image is too large');
441+
}
435442
const resizedImage = await resizeImage(readFile.value.buffer);
436443
return {
437444
id: editor.resource.toString(),

0 commit comments

Comments
 (0)