djcb / mu

maildir indexer/searcher + emacs mail client + guile bindings
http://www.djcbsoftware.nl/code/mu
GNU General Public License v3.0
1.62k stars 391 forks source link

[mu4e bug] Attachments with identical name: mu4e-view-save-attachments can only save one #2742

Closed bramadams closed 3 months ago

bramadams commented 3 months ago

Describe the bug

If an email contains multiple attachments with the same name, mu4e-view-save-attachments only offers to save one of these attachments, even though the mu4e email view shows all attachment names correctly.

How to Reproduce

example.txt

Environment

Latest mu4e and vertico on emacs 29.3.

I also encounter the issue without vertico, i.e., using standard emacs completion.

Checklist

djcb commented 3 months ago

Please attach an example message where this happens, thanks.

bramadams commented 3 months ago

OK, done!

djcb commented 3 months ago

It works with mu4e-view-mime-part-action (A), but not with mu4e-view-save-attachments since, indeed, the latter depends on names being different.

Guess worth documenting, but perhaps too much a corner-case to try some (I fear ugly) fix. Let me think about it..

bramadams commented 3 months ago

Thanks for looking into this.

I wonder if, for the time being, mu4e could display a warning when running mu4e-view-save-attachments if two or more attachments have the same name? As such, one would be aware of potentially missing something, allowing users to use other means, like mu4e-view-mime-part-action, to access the attachments.

djcb commented 3 months ago

Perhaps... but one can see the attachments in the view and at least in my experience it's very rare so not really worth adding extra complexity to the code.

So for now I've just documented the behavior. Thanks!

bramadams commented 3 months ago

For anyone bitten by this issue, here's an advice automatically re-routing mu4e-view-save-attachments calls to mu4e-view-mime-part-action if at least one attachment has a duplicate name. (Since mu4e-view-mime-part-action does not have a "save all" option, it is more economical to only use it when needed.)

(defun my-handle-attachments-with-duplicate-names (fn &rest args)
     (let* ((parts (mu4e-view-mime-parts))
               (candidates  (seq-map
                               (lambda (fpart)
                                 (plist-get fpart :filename))
                               (seq-filter
                                (lambda (part) (plist-get part :attachment-like))
                                parts)))
                 (candidates-set (seq-uniq candidates)))
            (if (< (length candidates-set) (length candidates))
                ;; duplicate names, redirect to mu4e-view-mime-part-action
                (mu4e-view-mime-part-action)
              (apply fn args))))

(advice-add #'mu4e-view-save-attachments :around #'my-handle-attachments-with-duplicate-names)