Skip to content

Commit b2b4bdf

Browse files
committed
Revert "Optimize file extension detection performance"
This reverts commit 824962c.
1 parent 824962c commit b2b4bdf

1 file changed

Lines changed: 31 additions & 29 deletions

File tree

packages/opencode/src/file/index.ts

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -204,34 +204,37 @@ export namespace File {
204204
"x3f",
205205
])
206206

207-
const imageMimeTypes: Record<string, string> = {
208-
png: "image/png",
209-
jpg: "image/jpeg",
210-
jpeg: "image/jpeg",
211-
gif: "image/gif",
212-
bmp: "image/bmp",
213-
webp: "image/webp",
214-
ico: "image/x-icon",
215-
tif: "image/tiff",
216-
tiff: "image/tiff",
217-
svg: "image/svg+xml",
218-
svgz: "image/svg+xml",
219-
avif: "image/avif",
220-
apng: "image/apng",
221-
jxl: "image/jxl",
222-
heic: "image/heic",
223-
heif: "image/heif",
207+
function isImageByExtension(filepath: string): boolean {
208+
const ext = path.extname(filepath).toLowerCase().slice(1)
209+
return imageExtensions.has(ext)
224210
}
225211

226-
function getFileTypeByExtension(filepath: string): { isImage: boolean; isBinary: boolean; mimeType?: string } {
212+
function getImageMimeType(filepath: string): string {
227213
const ext = path.extname(filepath).toLowerCase().slice(1)
228-
if (imageExtensions.has(ext)) {
229-
return { isImage: true, isBinary: false, mimeType: imageMimeTypes[ext] || "image/" + ext }
230-
}
231-
if (binaryExtensions.has(ext)) {
232-
return { isImage: false, isBinary: true }
214+
const mimeTypes: Record<string, string> = {
215+
png: "image/png",
216+
jpg: "image/jpeg",
217+
jpeg: "image/jpeg",
218+
gif: "image/gif",
219+
bmp: "image/bmp",
220+
webp: "image/webp",
221+
ico: "image/x-icon",
222+
tif: "image/tiff",
223+
tiff: "image/tiff",
224+
svg: "image/svg+xml",
225+
svgz: "image/svg+xml",
226+
avif: "image/avif",
227+
apng: "image/apng",
228+
jxl: "image/jxl",
229+
heic: "image/heic",
230+
heif: "image/heif",
233231
}
234-
return { isImage: false, isBinary: false }
232+
return mimeTypes[ext] || "image/" + ext
233+
}
234+
235+
function isBinaryByExtension(filepath: string): boolean {
236+
const ext = path.extname(filepath).toLowerCase().slice(1)
237+
return binaryExtensions.has(ext)
235238
}
236239

237240
function isImage(mimeType: string): boolean {
@@ -433,19 +436,18 @@ export namespace File {
433436
}
434437

435438
// Fast path: check extension before any filesystem operations
436-
const fileType = getFileTypeByExtension(file)
437-
438-
if (fileType.isImage) {
439+
if (isImageByExtension(file)) {
439440
const bunFile = Bun.file(full)
440441
if (await bunFile.exists()) {
441442
const buffer = await bunFile.arrayBuffer().catch(() => new ArrayBuffer(0))
442443
const content = Buffer.from(buffer).toString("base64")
443-
return { type: "text", content, mimeType: fileType.mimeType, encoding: "base64" }
444+
const mimeType = getImageMimeType(file)
445+
return { type: "text", content, mimeType, encoding: "base64" }
444446
}
445447
return { type: "text", content: "" }
446448
}
447449

448-
if (fileType.isBinary) {
450+
if (isBinaryByExtension(file)) {
449451
return { type: "binary", content: "" }
450452
}
451453

0 commit comments

Comments
 (0)