msgpart: factorize mime type and filename construction

Reduce code duplication.

Signed-off-by: Robin Jarry <robin@jarry.cc>
Acked-by: Moritz Poldrack <moritz@poldrack.dev>
This commit is contained in:
Robin Jarry 2022-10-12 23:52:45 +02:00
parent a4b80bcc8b
commit 9bd2e0c84f
8 changed files with 30 additions and 28 deletions

View file

@ -198,7 +198,7 @@ func (forward) Execute(aerc *widgets.Aerc, args []string) error {
continue
}
store.FetchBodyPart(msg.Uid, p, func(reader io.Reader) {
mime := fmt.Sprintf("%s/%s", bs.MIMEType, bs.MIMESubType)
mime := bs.FullMIMEType()
params := lib.SetUtf8Charset(bs.Params)
name, ok := params["name"]
if !ok {

View file

@ -204,7 +204,7 @@ func (Recall) Execute(aerc *widgets.Aerc, args []string) error {
continue
}
msg.FetchBodyPart(p, func(reader io.Reader) {
mime := fmt.Sprintf("%s/%s", bs.MIMEType, bs.MIMESubType)
mime := bs.FullMIMEType()
params := lib.SetUtf8Charset(bs.Params)
name, ok := params["name"]
if !ok {

View file

@ -326,7 +326,7 @@ func addMimeType(msg *models.MessageInfo, part []int,
if err != nil {
return err
}
orig.MIMEType = fmt.Sprintf("%s/%s", bs.MIMEType, bs.MIMESubType)
orig.MIMEType = bs.FullMIMEType()
return nil
}

View file

@ -1,7 +1,6 @@
package msgview
import (
"fmt"
"io"
"mime"
"os"
@ -52,7 +51,7 @@ func (Open) Execute(aerc *widgets.Aerc, args []string) error {
// try to determine the correct extension based on mimetype
if part, err := mv.MessageView().BodyStructure().PartAtIndex(p.Index); err == nil {
mimeType = fmt.Sprintf("%s/%s", part.MIMEType, part.MIMESubType)
mimeType = part.FullMIMEType()
if exts, _ := mime.ExtensionsByType(mimeType); len(exts) > 0 {
extension = exts[0]
}

View file

@ -215,12 +215,7 @@ func isAbsPath(path string) bool {
// generateFilename tries to get the filename from the given part.
// if that fails it will fallback to a generated one based on the date
func generateFilename(part *models.BodyStructure) string {
var filename string
if fn, ok := part.DispositionParams["filename"]; ok {
filename = fn
} else if fn, ok := part.Params["name"]; ok {
filename = fn
}
filename := part.FileName()
// Some MUAs send attachments with names like /some/stupid/idea/happy.jpeg
// Assuming non hostile intent it does make sense to use just the last
// portion of the pathname as the filename for saving it.