From d860e26629fd1d59539201a53f4af3eef9f14ccf Mon Sep 17 00:00:00 2001 From: Vitaly Ovchinnikov Date: Fri, 23 Jun 2023 12:46:00 +0300 Subject: open: use the original attachment extension if possible When opening an attachment with :open command we make a temporary file and open it then. The file is named randomly, but the extension is derived from the attachment parameters. This patch checks if the attachment has a file name assigned and takes the extension from there. Otherwise it rolls back to the original mime-based extension generation. Signed-off-by: Vitaly Ovchinnikov Acked-by: Robin Jarry --- commands/msgview/open.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'commands/msgview/open.go') diff --git a/commands/msgview/open.go b/commands/msgview/open.go index 0380b51a..9ceccb5a 100644 --- a/commands/msgview/open.go +++ b/commands/msgview/open.go @@ -5,6 +5,7 @@ import ( "io" "mime" "os" + "path/filepath" "git.sr.ht/~rjarry/aerc/lib" "git.sr.ht/~rjarry/aerc/log" @@ -36,11 +37,16 @@ func (Open) Execute(aerc *widgets.Aerc, args []string) error { extension := "" mimeType := "" - // try to determine the correct extension based on mimetype + // try to determine the correct extension if part, err := mv.MessageView().BodyStructure().PartAtIndex(p.Index); err == nil { mimeType = part.FullMIMEType() - if exts, _ := mime.ExtensionsByType(mimeType); len(exts) > 0 { - extension = exts[0] + // see if we can get extension directly from the attachment name + extension = filepath.Ext(part.FileName()) + // if there is no extension, try using the attachment mime type instead + if extension == "" { + if exts, _ := mime.ExtensionsByType(mimeType); len(exts) > 0 { + extension = exts[0] + } } } -- cgit