File tree Expand file tree Collapse file tree 1 file changed +13
-2
lines changed
Expand file tree Collapse file tree 1 file changed +13
-2
lines changed Original file line number Diff line number Diff line change @@ -31,6 +31,16 @@ function getExtension(filename: string): string {
3131 return dot >= 0 ? filename . slice ( dot + 1 ) . toLowerCase ( ) : ''
3232}
3333
34+ function detectImageMime ( buf : Buffer , claimed : string ) : string {
35+ if ( buf . length < 12 ) return claimed
36+ if ( buf [ 0 ] === 0xff && buf [ 1 ] === 0xd8 && buf [ 2 ] === 0xff ) return 'image/jpeg'
37+ if ( buf [ 0 ] === 0x89 && buf [ 1 ] === 0x50 && buf [ 2 ] === 0x4e && buf [ 3 ] === 0x47 ) return 'image/png'
38+ if ( buf [ 0 ] === 0x47 && buf [ 1 ] === 0x49 && buf [ 2 ] === 0x46 ) return 'image/gif'
39+ if ( buf [ 8 ] === 0x57 && buf [ 9 ] === 0x45 && buf [ 10 ] === 0x42 && buf [ 11 ] === 0x50 )
40+ return 'image/webp'
41+ return claimed
42+ }
43+
3444export interface FileReadResult {
3545 content : string
3646 totalLines : number
@@ -59,14 +69,15 @@ export async function readFileRecord(record: WorkspaceFileRecord): Promise<FileR
5969 }
6070 }
6171 const buffer = await downloadWorkspaceFile ( record )
72+ const mime = detectImageMime ( buffer , record . type )
6273 return {
63- content : `Image: ${ record . name } (${ ( record . size / 1024 ) . toFixed ( 1 ) } KB, ${ record . type } )` ,
74+ content : `Image: ${ record . name } (${ ( record . size / 1024 ) . toFixed ( 1 ) } KB, ${ mime } )` ,
6475 totalLines : 1 ,
6576 attachment : {
6677 type : 'image' ,
6778 source : {
6879 type : 'base64' ,
69- media_type : record . type ,
80+ media_type : mime ,
7081 data : buffer . toString ( 'base64' ) ,
7182 } ,
7283 } ,
You can’t perform that action at this time.
0 commit comments