diff options
author | Maarten Aertsen <maarten@nlnetlabs.nl> | 2024-04-13 09:09:29 +0200 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2024-04-13 21:47:25 +0200 |
commit | d99c49de2fc12b64a7cd79044a8b042e7e59d02b (patch) | |
tree | 50704e4c311c5b0d9b203df305b0e8a5ec558e0c /commands/msgview/open.go | |
parent | 777bbb77e806cd127d63f0d19cda38a8ab6ec1fb (diff) | |
download | aerc-d99c49de2fc12b64a7cd79044a8b042e7e59d02b.tar.gz |
open: preserve the original filename
Change the name of the temporary file that is :open'ed using the system
handler from `aerc-<randint>.ext` to
`aerc-<randint>/<actual-filename.ext>`. This preserves the original
filename, while retaining collision avoidance and the current base
location (os.TempDir()).
Changelog-changed: `:open` commands now preserve the original filename.
Signed-off-by: Maarten Aertsen <maarten@nlnetlabs.nl>
Tested-by: Bence Ferdinandy <bence@ferdinandy.com>
Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'commands/msgview/open.go')
-rw-r--r-- | commands/msgview/open.go | 27 |
1 files changed, 12 insertions, 15 deletions
diff --git a/commands/msgview/open.go b/commands/msgview/open.go index 85005031..55d085f6 100644 --- a/commands/msgview/open.go +++ b/commands/msgview/open.go @@ -3,7 +3,6 @@ package msgview import ( "errors" "io" - "mime" "os" "path/filepath" @@ -42,23 +41,21 @@ func (o Open) Execute(args []string) error { p := mv.SelectedMessagePart() mv.MessageView().FetchBodyPart(p.Index, func(reader io.Reader) { - extension := "" mimeType := "" - // try to determine the correct extension - if part, err := mv.MessageView().BodyStructure().PartAtIndex(p.Index); err == nil { - mimeType = part.FullMIMEType() - // 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] - } - } + part, err := mv.MessageView().BodyStructure().PartAtIndex(p.Index) + if err != nil { + app.PushError(err.Error()) + return } + mimeType = part.FullMIMEType() - tmpFile, err := os.CreateTemp(os.TempDir(), "aerc-*"+extension) + tmpDir, err := os.MkdirTemp(os.TempDir(), "aerc-*") + if err != nil { + app.PushError(err.Error()) + return + } + tmpFile, err := os.Create(filepath.Join(tmpDir, part.FileName())) if err != nil { app.PushError(err.Error()) return @@ -74,7 +71,7 @@ func (o Open) Execute(args []string) error { go func() { defer log.PanicHandler() if o.Delete { - defer os.Remove(tmpFile.Name()) + defer os.RemoveAll(tmpDir) } err = lib.XDGOpenMime(tmpFile.Name(), mimeType, o.Cmd) if err != nil { |