@@ -2,10 +2,70 @@ import * as vscode from "vscode"
22import fs from "fs/promises"
33import * as path from "path"
44
5- const IMAGE_EXTENSIONS = [ "png" , "jpg" , "jpeg" , "webp" ]
6- const TEXT_FILE_EXTENSIONS = [ "xml" , "json" , "txt" , "log" , "md" , "csv" , "tsv" , "yaml" , "yml" , "ini" , "cfg" , "conf" ]
5+ const IMAGE_EXTENSIONS = [ "png" , "jpg" , "jpeg" , "webp" , "gif" , "bmp" , "svg" , "ico" ]
76const MAX_FILE_SIZE = 20 * 1024 * 1024 // 20MB
87
8+ // Known binary file extensions to skip upfront
9+ const BINARY_EXTENSIONS = [
10+ // Executables & Libraries
11+ "exe" ,
12+ "dll" ,
13+ "so" ,
14+ "dylib" ,
15+ "app" ,
16+ "deb" ,
17+ "rpm" ,
18+ "dmg" ,
19+ "pkg" ,
20+ "msi" ,
21+ // Archives
22+ "zip" ,
23+ "tar" ,
24+ "gz" ,
25+ "bz2" ,
26+ "7z" ,
27+ "rar" ,
28+ "jar" ,
29+ "war" ,
30+ "ear" ,
31+ // Media
32+ "mp3" ,
33+ "mp4" ,
34+ "avi" ,
35+ "mov" ,
36+ "wmv" ,
37+ "flv" ,
38+ "mkv" ,
39+ "webm" ,
40+ "ogg" ,
41+ "wav" ,
42+ "flac" ,
43+ // Documents (binary formats)
44+ "pdf" ,
45+ "doc" ,
46+ "docx" ,
47+ "xls" ,
48+ "xlsx" ,
49+ "ppt" ,
50+ "pptx" ,
51+ "odt" ,
52+ "ods" ,
53+ "odp" ,
54+ // Databases
55+ "db" ,
56+ "sqlite" ,
57+ "mdb" ,
58+ // Other binary formats
59+ "pyc" ,
60+ "pyo" ,
61+ "class" ,
62+ "o" ,
63+ "a" ,
64+ "lib" ,
65+ "node" ,
66+ "wasm" ,
67+ ]
68+
969export async function selectFiles ( ) : Promise < {
1070 images : string [ ]
1171 files : Array < { path : string ; content : string ; type : string } >
@@ -15,9 +75,7 @@ export async function selectFiles(): Promise<{
1575 openLabel : "Select" ,
1676 filters : {
1777 "All Files" : [ "*" ] ,
18- "Supported Files" : [ ...IMAGE_EXTENSIONS , ...TEXT_FILE_EXTENSIONS ] ,
1978 Images : IMAGE_EXTENSIONS ,
20- "Text Files" : TEXT_FILE_EXTENSIONS ,
2179 } ,
2280 }
2381
@@ -44,24 +102,32 @@ export async function selectFiles(): Promise<{
44102 }
45103
46104 if ( IMAGE_EXTENSIONS . includes ( ext ) ) {
47- // Process as image (existing logic)
105+ // Process as image
48106 const buffer = await fs . readFile ( filePath )
49107 const base64 = buffer . toString ( "base64" )
50108 const mimeType = getMimeType ( filePath )
51109 const dataUrl = `data:${ mimeType } ;base64,${ base64 } `
52110 images . push ( dataUrl )
111+ } else if ( BINARY_EXTENSIONS . includes ( ext ) ) {
112+ // Skip known binary files
113+ vscode . window . showWarningMessage ( `Cannot attach binary file: ${ fileName } ` )
53114 } else {
54- // Process as text file
115+ // Try to read as text file
55116 try {
56117 const content = await fs . readFile ( filePath , "utf-8" )
57- files . push ( {
58- path : fileName ,
59- content : content ,
60- type : ext ,
61- } )
118+ // Additional check: if the content has null bytes, it's likely binary
119+ if ( content . includes ( "\0" ) ) {
120+ vscode . window . showWarningMessage ( `File appears to be binary: ${ fileName } ` )
121+ } else {
122+ files . push ( {
123+ path : fileName ,
124+ content : content ,
125+ type : ext || "txt" , // Default to 'txt' if no extension
126+ } )
127+ }
62128 } catch ( error ) {
63- // Binary file or read error
64- vscode . window . showWarningMessage ( `Could not read file: ${ fileName } ` )
129+ // File couldn't be read as UTF-8, likely binary
130+ vscode . window . showWarningMessage ( `Cannot read file as text : ${ fileName } ` )
65131 }
66132 }
67133 } ) ,
0 commit comments