Try to open attachments with correct extension

The temporary file created when opening an attachment is currently saved
without an extension, which prevents matching on file ending with
xdg-open.
This commit is contained in:
Galen Abell 2020-04-02 09:54:45 -04:00 committed by Reto Brunner
parent 35402e21d9
commit 95fb35b701
1 changed files with 13 additions and 1 deletions

View File

@ -2,8 +2,10 @@ package msgview
import ( import (
"errors" "errors"
"fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"mime"
"os" "os"
"time" "time"
@ -35,7 +37,17 @@ func (Open) Execute(aerc *widgets.Aerc, args []string) error {
store := mv.Store() store := mv.Store()
store.FetchBodyPart(p.Msg.Uid, p.Msg.BodyStructure, p.Index, func(reader io.Reader) { store.FetchBodyPart(p.Msg.Uid, p.Msg.BodyStructure, p.Index, func(reader io.Reader) {
tmpFile, err := ioutil.TempFile(os.TempDir(), "aerc-") extension := ""
// try to determine the correct extension based on mimetype
if part, err := p.Msg.BodyStructure.PartAtIndex(p.Index); err == nil {
mimeType := fmt.Sprintf("%s/%s", part.MIMEType, part.MIMESubType)
if exts, _ := mime.ExtensionsByType(mimeType); exts != nil && len(exts) > 0 {
extension = exts[0]
}
}
tmpFile, err := ioutil.TempFile(os.TempDir(), "aerc-*"+extension)
if err != nil { if err != nil {
aerc.PushError(" " + err.Error()) aerc.PushError(" " + err.Error())
return return