Skip to content

Commit ba397ce

Browse files
committed
refactor: consolidate MIME type mapping for image formats
1 parent 7ec9ae6 commit ba397ce

File tree

1 file changed

+17
-33
lines changed

1 file changed

+17
-33
lines changed

src/core/tools/readFileTool.ts

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ const SUPPORTED_IMAGE_FORMATS = [
4444
".avif",
4545
] as const
4646

47+
const IMAGE_MIME_TYPES: Record<string, string> = {
48+
".png": "image/png",
49+
".jpg": "image/jpeg",
50+
".jpeg": "image/jpeg",
51+
".gif": "image/gif",
52+
".webp": "image/webp",
53+
".svg": "image/svg+xml",
54+
".bmp": "image/bmp",
55+
".ico": "image/x-icon",
56+
".tiff": "image/tiff",
57+
".tif": "image/tiff",
58+
".avif": "image/avif",
59+
}
60+
4761
/**
4862
* Reads an image file and returns both the data URL and buffer
4963
*/
@@ -52,24 +66,9 @@ async function readImageAsDataUrlWithBuffer(filePath: string): Promise<{ dataUrl
5266
const base64 = fileBuffer.toString("base64")
5367
const ext = path.extname(filePath).toLowerCase()
5468

55-
// Map extensions to MIME types
56-
const mimeTypes: Record<string, string> = {
57-
".png": "image/png",
58-
".jpg": "image/jpeg",
59-
".jpeg": "image/jpeg",
60-
".gif": "image/gif",
61-
".webp": "image/webp",
62-
".svg": "image/svg+xml",
63-
".bmp": "image/bmp",
64-
".ico": "image/x-icon",
65-
".tiff": "image/tiff",
66-
".tif": "image/tiff",
67-
".avif": "image/avif",
68-
}
69-
70-
const mimeType = mimeTypes[ext] || "image/png"
69+
const mimeType = IMAGE_MIME_TYPES[ext] || "image/png"
7170
const dataUrl = `data:${mimeType};base64,${base64}`
72-
71+
7372
return { dataUrl, buffer: fileBuffer }
7473
}
7574

@@ -559,26 +558,11 @@ export async function readFileTool(
559558
const { dataUrl: imageDataUrl, buffer } = await readImageAsDataUrlWithBuffer(fullPath)
560559
const imageSizeInKB = Math.round(imageStats.size / 1024)
561560

562-
// For images, get dimensions if possible
563-
let dimensionsInfo = ""
564-
if (fileExtension === ".png") {
565-
// Simple PNG dimension extraction (first 24 bytes contain width/height)
566-
if (buffer.length >= 24) {
567-
const width = buffer.readUInt32BE(16)
568-
const height = buffer.readUInt32BE(20)
569-
if (width && height) {
570-
dimensionsInfo = `${width}x${height} pixels`
571-
}
572-
}
573-
}
574-
575561
// Track file read
576562
await cline.fileContextTracker.trackFileContext(relPath, "read_tool" as RecordSource)
577563

578564
// Store image data URL separately - NOT in XML
579-
const noticeText = dimensionsInfo
580-
? t("tools:readFile.imageWithDimensions", { dimensions: dimensionsInfo, size: imageSizeInKB })
581-
: t("tools:readFile.imageWithSize", { size: imageSizeInKB })
565+
const noticeText = t("tools:readFile.imageWithSize", { size: imageSizeInKB })
582566

583567
updateFileResult(relPath, {
584568
xmlContent: `<file><path>${relPath}</path>\n<notice>${noticeText}</notice>\n</file>`,

0 commit comments

Comments
 (0)