webmail: add buttons to download a message as eml, and export 1 or more messages as mbox/maildir in zip/tgz/tar, like for entire mailboxes

Download as eml is useful with firefox, because opening the raw message in a
new tab, and then downloading it, causes firefox to request the url without
cookies, causing it to save a "403 - forbidden" response.

Exporting a selection is useful during all kinds of testing. Makes it easy to
an entire thread, or just some messages.

The export popover now has buttons for each combination of mbox/maildir vs
zip/tgz/tar. Before you may have had to select the email format and archive
format first, followed by a click. Now it's just a click.
This commit is contained in:
Mechiel Lukkien
2025-03-29 18:10:23 +01:00
parent d6e55b5f36
commit a5d74eb718
8 changed files with 591 additions and 394 deletions

View File

@ -7,6 +7,7 @@ import (
"fmt"
"mime"
"net/http"
"strconv"
"strings"
"time"
@ -23,7 +24,25 @@ func Export(log mlog.Log, accName string, w http.ResponseWriter, r *http.Request
return
}
// We
mailbox := r.FormValue("mailbox") // Empty means all.
messageIDstr := r.FormValue("messageids")
var messageIDs []int64
if messageIDstr != "" {
for _, s := range strings.Split(messageIDstr, ",") {
id, err := strconv.ParseInt(s, 10, 64)
if err != nil {
http.Error(w, fmt.Sprintf("400 - bad request - bad message id %q: %v", s, err), http.StatusBadRequest)
return
}
messageIDs = append(messageIDs, id)
}
}
if mailbox != "" && len(messageIDs) > 0 {
http.Error(w, "400 - bad request - cannot specify both mailbox and message ids", http.StatusBadRequest)
return
}
format := r.FormValue("format")
archive := r.FormValue("archive")
recursive := r.FormValue("recursive") != ""
@ -43,6 +62,10 @@ func Export(log mlog.Log, accName string, w http.ResponseWriter, r *http.Request
http.Error(w, "400 - bad request - archive none can only be used with non-recursive mbox", http.StatusBadRequest)
return
}
if len(messageIDs) > 0 && recursive {
http.Error(w, "400 - bad request - cannot export message ids recursively", http.StatusBadRequest)
return
}
acc, err := store.OpenAccount(log, accName, false)
if err != nil {
@ -55,11 +78,15 @@ func Export(log mlog.Log, accName string, w http.ResponseWriter, r *http.Request
log.Check(err, "closing account")
}()
name := strings.ReplaceAll(mailbox, "/", "-")
if name == "" {
name = "all"
var name string
if mailbox != "" {
name = "-" + strings.ReplaceAll(mailbox, "/", "-")
} else if len(messageIDs) > 1 {
name = "-selection"
} else if len(messageIDs) == 0 {
name = "-all"
}
filename := fmt.Sprintf("mailexport-%s-%s", name, time.Now().Format("20060102-150405"))
filename := fmt.Sprintf("mailexport%s-%s", name, time.Now().Format("20060102-150405"))
filename += "." + format
var archiver store.Archiver
if archive == "none" {
@ -90,7 +117,7 @@ func Export(log mlog.Log, accName string, w http.ResponseWriter, r *http.Request
log.Check(err, "exporting mail close")
}()
w.Header().Set("Content-Disposition", mime.FormatMediaType("attachment", map[string]string{"filename": filename}))
if err := store.ExportMessages(r.Context(), log, acc.DB, acc.Dir, archiver, format == "maildir", mailbox, recursive); err != nil {
if err := store.ExportMessages(r.Context(), log, acc.DB, acc.Dir, archiver, format == "maildir", mailbox, messageIDs, recursive); err != nil {
log.Errorx("exporting mail", err)
}
}