Skip to content

Commit 81377cb

Browse files
committed
Improve image MIME type detection in file editor
Replaced base64 prefix checks with mimetypes.guess_type for determining image MIME type when reading image files. Falls back to 'image/png' if MIME type is not detected or not an image. This simplifies and improves accuracy of MIME type detection.
1 parent eb9373b commit 81377cb

File tree

1 file changed

+4
-11
lines changed
  • openhands-tools/openhands/tools/file_editor

1 file changed

+4
-11
lines changed

openhands-tools/openhands/tools/file_editor/editor.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import base64
2+
import mimetypes
23
import os
34
import re
45
import shutil
@@ -341,17 +342,9 @@ def view(
341342
image_bytes = f.read()
342343
image_base64 = base64.b64encode(image_bytes).decode("utf-8")
343344

344-
mime_type = "image/png" # default
345-
if image_base64.startswith("/9j/"):
346-
mime_type = "image/jpeg"
347-
elif image_base64.startswith("iVBORw0KGgo"):
348-
mime_type = "image/png"
349-
elif image_base64.startswith("R0lGODlh"):
350-
mime_type = "image/gif"
351-
elif image_base64.startswith("UklGR"):
352-
mime_type = "image/webp"
353-
elif image_base64.startswith("Qk"):
354-
mime_type = "image/bmp"
345+
mime_type, _ = mimetypes.guess_type(str(path))
346+
if not mime_type or not mime_type.startswith('image/'):
347+
mime_type = 'image/png'
355348
output_msg = (
356349
f"Image file {path} read successfully. Displaying image content."
357350
)

0 commit comments

Comments
 (0)