branch: elpa/gptel
commit e3a0d37133b7f0f4fb8a84479235652d8ea83555
Author: Karthik Chikmagalur <[email protected]>
Commit: Karthik Chikmagalur <[email protected]>

    request: Prefer extension for binary file detection of images
    
    * gptel-request.el (gptel--file-binary-p): Images with ICC color
    profile data are categorized by Emacs as text files, at least from
    the first 512 bytes.  Add an exception for common image file
    extensions.  This is a hack until we can find a more robust
    solution. (#1223)
---
 gptel-request.el | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/gptel-request.el b/gptel-request.el
index 5f15bc6194f..041e03aea15 100644
--- a/gptel-request.el
+++ b/gptel-request.el
@@ -854,13 +854,17 @@ Later plists in the sequence take precedence over earlier 
ones."
 
 (defun gptel--file-binary-p (path)
   "Check if file at PATH is readable and binary."
-  (condition-case nil
-      (with-temp-buffer
-        (insert-file-contents path nil 1 512 'replace)
-        (memq buffer-file-coding-system
-              '(no-conversion no-conversion-multibyte)))
-    (file-missing (message "File \"%s\" is not readable." path)
-                  nil)))
+  ;; HACK Image files with ICC color profiles are characterized as ASCII
+  ;; (#1223), so until we find a better solution we just match these files by
+  ;; extension.
+  (or (string-match-p "\\.\\(jpe?g\\|png\\|gif\\|webp\\)\\'" path)
+      (condition-case nil
+          (with-temp-buffer
+            (insert-file-contents path nil 1 512 'replace)
+            (memq buffer-file-coding-system
+                  '(no-conversion no-conversion-multibyte)))
+        (file-missing (message "File \"%s\" is not readable." path)
+                      nil))))
 
 (defun gptel--insert-file-string (path)
   "Insert at point the contents of the file at PATH as context."

Reply via email to