Subject: bug#61394: 30.0.50; [PATCH] Image-dired thumb name based on content

From: Manuel Giraud <[email-masked]>
Date: Sun, 12 Feb 2023 00:06:32 +0100

So, here is another version of the patch.  I'm not quite sure for the
docstring of image-dired-thumb-name.
>From 5cf749b1b321c5f1bd80ae4bb3e1dcf2ba5c02e1 Mon Sep 17 00:00:00 2001
From: Manuel Giraud <[email-masked]>
Date: Thu, 9 Feb 2023 19:56:37 +0100
Subject: [PATCH] Image-dired thumb name based on contents

* lisp/image/image-dired-util.el (image-dired-contents-sha1): New
utility that compute the SHA-1 of the first 4KiB of the contents of a
file.
(image-dired-thumb-name): Fix the docstring and use
image-dired-contents-sha1.
---
 lisp/image/image-dired-util.el | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)

diff --git a/lisp/image/image-dired-util.el b/lisp/image/image-dired-util.el
index c03f9d2e3d3..cde9d0be8d8 100644
--- a/lisp/image/image-dired-util.el
+++ b/lisp/image/image-dired-util.el
@@ -57,18 +57,31 @@ image-dired-dir
       (message "Thumbnail directory created: %s" image-dired-dir))
     image-dired-dir))
 
+(defun image-dired-contents-sha1 (filename)
+  "Compute the SHA-1 of the first 4KiB of FILENAME's contents."
+  (with-temp-buffer
+    (insert-file-contents-literally filename nil 0 4096)
+    (sha1 (current-buffer))))
+
 (defun image-dired-thumb-name (file)
   "Return absolute file name for thumbnail FILE.
 Depending on the value of `image-dired-thumbnail-storage', the
 file name of the thumbnail will vary:
-- For `use-image-dired-dir', make a SHA1-hash of the image file's
-  directory name and add that to make the thumbnail file name
-  unique.
-- For `per-directory' storage, just add a subdirectory.
+
+- For `image-dired', make a SHA1-hash the contents of the image
+  FILE with `image-dired-contents-sha1' and append a \".jpg\"
+  extension.  This thumbnail will be stored in the
+  `image-dired-dir' directory.
+
+- For `per-directory', append a \".thumb.jpg\" extension to the
+  FILE's name.  This thumbnail will be stored in a
+  \".image-dired\" subdirectory into the FILE's directory.
+
 - For `standard' storage, produce the file name according to the
   Thumbnail Managing Standard.  Among other things, an MD5-hash
   of the image file's directory name will be added to the
   filename.
+
 See also `image-dired-thumbnail-storage'."
   (let ((file (expand-file-name file)))
     (cond ((memq image-dired-thumbnail-storage
@@ -85,7 +98,7 @@ image-dired-thumb-name
           ((or (eq 'image-dired image-dired-thumbnail-storage)
                ;; Maintained for backwards compatibility:
                (eq 'use-image-dired-dir image-dired-thumbnail-storage))
-           (expand-file-name (format "%s.jpg" (sha1 file))
+           (expand-file-name (format "%s.jpg" (image-dired-contents-sha1 file))
                              (image-dired-dir)))
           ((eq 'per-directory image-dired-thumbnail-storage)
            (expand-file-name (format "%s.thumb.jpg"
-- 
2.39.1

-- 
Manuel Giraud