From 24ced4d8bffb2c4351c4d08b62d77db0a8f6056c Mon Sep 17 00:00:00 2001 From: ayn2op Date: Sun, 6 Sep 2026 23:03:07 -0400 Subject: [PATCH] fix(chat): keep original attachment extension and surface open errors --- internal/ui/chat/messages_list.go | 38 ++++++++++++++++++------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/internal/ui/chat/messages_list.go b/internal/ui/chat/messages_list.go index 6976348..d864815 100644 --- a/internal/ui/chat/messages_list.go +++ b/internal/ui/chat/messages_list.go @@ -1080,36 +1080,43 @@ func (ml *messagesList) confirmAttachment(attachment discord.Attachment, action func openDownloadedAttachment(attachment discord.Attachment) tview.Cmd { return func() tview.Msg { - mediaType, _, _ := mime.ParseMediaType(attachment.ContentType) - extensions, _ := mime.ExtensionsByType(mediaType) - extension := "" - if len(extensions) != 0 { - extension = extensions[0] + extension := filepath.Ext(attachment.Filename) + if extension == "" { + mediaType, _, _ := mime.ParseMediaType(attachment.ContentType) + if extensions, _ := mime.ExtensionsByType(mediaType); len(extensions) != 0 { + extension = extensions[0] + } } dir := filepath.Join(consts.CacheDir(), "attachments") if err := os.MkdirAll(dir, 0o700); err != nil { - slog.Error("failed to create attachments directory", "err", err) - return nil + return attachmentErr("create attachments directory", err) } + file, err := os.CreateTemp(dir, "attachment-*"+extension) if err != nil { - slog.Error("failed to create attachment file", "err", err) - return nil + return attachmentErr("create attachment file", err) } + defer file.Close() + path := file.Name() - file.Close() if err := downloadAttachment(attachment, path); err != nil { os.Remove(path) - slog.Error("failed to download attachment", "err", err) + return attachmentErr("download attachment", err) } else if err := open.Start(path); err != nil { - slog.Error("failed to open attachment file", "err", err, "path", path) + return attachmentErr("open attachment file", err) } + return nil } } +func attachmentErr(what string, err error) tview.Msg { + slog.Error("failed to "+what, "err", err) + return ui.ModalMsg{Text: "Failed to " + what + ": " + err.Error(), Buttons: []ui.ModalButton{{Label: "OK"}}} +} + func saveAttachment(attachment discord.Attachment) tview.Cmd { return func() tview.Msg { destination, err := zenity.SelectFileSave(zenity.Filename(filepath.Base(attachment.Filename)), zenity.ConfirmOverwrite()) @@ -1117,12 +1124,11 @@ func saveAttachment(attachment discord.Attachment) tview.Cmd { return nil } if err != nil { - slog.Error("failed to select attachment destination", "err", err) - return nil + return attachmentErr("select attachment destination", err) } if err := downloadAttachment(attachment, destination); err != nil { - slog.Error("failed to download attachment", "err", err) + return attachmentErr("download attachment", err) } return nil } @@ -1149,7 +1155,7 @@ func downloadAttachment(attachment discord.Attachment, destination string) error func openURL(url string) tview.Cmd { return func() tview.Msg { if err := open.Start(url); err != nil { - slog.Error("failed to open URL", "err", err, "url", url) + return attachmentErr("open URL", err) } return nil }