File types

This commit is contained in:
Siddharth Ganesan
2026-04-04 17:04:14 -07:00
parent 0d09d11c33
commit 0b3f3ed728

View File

@@ -31,6 +31,16 @@ function getExtension(filename: string): string {
return dot >= 0 ? filename.slice(dot + 1).toLowerCase() : ''
}
function detectImageMime(buf: Buffer, claimed: string): string {
if (buf.length < 12) return claimed
if (buf[0] === 0xff && buf[1] === 0xd8 && buf[2] === 0xff) return 'image/jpeg'
if (buf[0] === 0x89 && buf[1] === 0x50 && buf[2] === 0x4e && buf[3] === 0x47) return 'image/png'
if (buf[0] === 0x47 && buf[1] === 0x49 && buf[2] === 0x46) return 'image/gif'
if (buf[8] === 0x57 && buf[9] === 0x45 && buf[10] === 0x42 && buf[11] === 0x50)
return 'image/webp'
return claimed
}
export interface FileReadResult {
content: string
totalLines: number
@@ -59,14 +69,15 @@ export async function readFileRecord(record: WorkspaceFileRecord): Promise<FileR
}
}
const buffer = await downloadWorkspaceFile(record)
const mime = detectImageMime(buffer, record.type)
return {
content: `Image: ${record.name} (${(record.size / 1024).toFixed(1)}KB, ${record.type})`,
content: `Image: ${record.name} (${(record.size / 1024).toFixed(1)}KB, ${mime})`,
totalLines: 1,
attachment: {
type: 'image',
source: {
type: 'base64',
media_type: record.type,
media_type: mime,
data: buffer.toString('base64'),
},
},