14 "github.com/mjl-/mox/mlog"
15 "github.com/mjl-/mox/store"
18// Export is used by webmail and webaccount to export messages of one or
19// multiple mailboxes, in maildir or mbox format, in a tar/tgz/zip archive or
21func Export(log mlog.Log, accName string, w http.ResponseWriter, r *http.Request) {
22 if r.Method != "POST" {
23 http.Error(w, "405 - method not allowed - use post", http.StatusMethodNotAllowed)
28 mailbox := r.FormValue("mailbox") // Empty means all.
29 messageIDstr := r.FormValue("messageids")
30 var messageIDs []int64
31 if messageIDstr != "" {
32 for _, s := range strings.Split(messageIDstr, ",") {
33 id, err := strconv.ParseInt(s, 10, 64)
35 http.Error(w, fmt.Sprintf("400 - bad request - bad message id %q: %v", s, err), http.StatusBadRequest)
38 messageIDs = append(messageIDs, id)
41 if mailbox != "" && len(messageIDs) > 0 {
42 http.Error(w, "400 - bad request - cannot specify both mailbox and message ids", http.StatusBadRequest)
46 format := r.FormValue("format")
47 archive := r.FormValue("archive")
48 recursive := r.FormValue("recursive") != ""
50 case "maildir", "mbox":
52 http.Error(w, "400 - bad request - unknown format", http.StatusBadRequest)
56 case "none", "tar", "tgz", "zip":
58 http.Error(w, "400 - bad request - unknown archive", http.StatusBadRequest)
61 if archive == "none" && (format != "mbox" || recursive) {
62 http.Error(w, "400 - bad request - archive none can only be used with non-recursive mbox", http.StatusBadRequest)
65 if len(messageIDs) > 0 && recursive {
66 http.Error(w, "400 - bad request - cannot export message ids recursively", http.StatusBadRequest)
70 acc, err := store.OpenAccount(log, accName, false)
72 log.Errorx("open account for export", err)
73 http.Error(w, "500 - internal server error", http.StatusInternalServerError)
78 log.Check(err, "closing account")
83 name = "-" + strings.ReplaceAll(mailbox, "/", "-")
84 } else if len(messageIDs) > 1 {
86 } else if len(messageIDs) == 0 {
89 filename := fmt.Sprintf("mailexport%s-%s", name, time.Now().Format("20060102-150405"))
90 filename += "." + format
91 var archiver store.Archiver
92 if archive == "none" {
93 w.Header().Set("Content-Type", "application/mbox")
94 archiver = &store.MboxArchiver{Writer: w}
95 } else if archive == "tar" {
96 // Don't tempt browsers to "helpfully" decompress.
97 w.Header().Set("Content-Type", "application/x-tar")
98 archiver = store.TarArchiver{Writer: tar.NewWriter(w)}
100 } else if archive == "tgz" {
101 // Don't tempt browsers to "helpfully" decompress.
102 w.Header().Set("Content-Type", "application/octet-stream")
104 gzw := gzip.NewWriter(w)
108 archiver = store.TarArchiver{Writer: tar.NewWriter(gzw)}
111 w.Header().Set("Content-Type", "application/zip")
112 archiver = store.ZipArchiver{Writer: zip.NewWriter(w)}
116 err := archiver.Close()
117 log.Check(err, "exporting mail close")
119 w.Header().Set("Content-Disposition", mime.FormatMediaType("attachment", map[string]string{"filename": filename}))
120 if err := store.ExportMessages(r.Context(), log, acc.DB, acc.Dir, archiver, format == "maildir", mailbox, messageIDs, recursive); err != nil {
121 log.Errorx("exporting mail", err)