add csv export

This commit is contained in:
2026-09-03 11:59:57 -04:00
parent 7c886a7927
commit 30442d8ece
3 changed files with 150 additions and 8 deletions

View File

@@ -1,6 +1,7 @@
package app
import (
"bytes"
"encoding/json"
"maxwarden/entries"
"maxwarden/middleware"
@@ -22,8 +23,12 @@ func ExportBitwardenHandler(w http.ResponseWriter, r *http.Request) {
renderView := func(errorMessage string) {
AppLayout("Export Vault", *identity, session,
Card(
Heading("Bitwarden JSON export"),
P(Text("Download your MaxWarden vault as a plaintext JSON file that can be imported into Bitwarden.")),
Heading("Bitwarden export"),
P(Text("Download your MaxWarden vault in one of Bitwarden's native import formats.")),
P(
InlineStyle("$me { margin-top: $3; }"),
Text("In Bitwarden, choose the File format that exactly matches the download: Bitwarden (csv) or Bitwarden (json). CSV is recommended if Bitwarden reports a generic JSON import error."),
),
P(
InlineStyle("$me { margin-top: $3; color: $color(red-700); font-weight: var(--font-weight-semibold); }"),
Text("Warning: the downloaded file contains all usernames and passwords in plaintext. Store it securely and delete it when you are finished."),
@@ -46,7 +51,11 @@ func ExportBitwardenHandler(w http.ResponseWriter, r *http.Request) {
Required(),
),
Br(),
ButtonUIDanger(Type("submit"), Text("Download plaintext export")),
Div(
InlineStyle("$me { display: flex; flex-wrap: wrap; gap: $3; margin-top: $3; }"),
ButtonUIDanger(Type("submit"), Name("format"), Value("csv"), Text("Download Bitwarden CSV (recommended)")),
ButtonUIOutline(Type("submit"), Name("format"), Value("json"), Text("Download Bitwarden JSON")),
),
),
),
).Render(w)
@@ -92,16 +101,38 @@ func ExportBitwardenHandler(w http.ResponseWriter, r *http.Request) {
return
}
document, err := json.MarshalIndent(entries.NewBitwardenExport(secretList), "", " ")
if err != nil {
http.Error(w, "Unable to create export", http.StatusInternalServerError)
format := r.Form.Get("format")
var document []byte
var extension string
var contentType string
switch format {
case "", "csv":
var csvDocument bytes.Buffer
if err := entries.WriteBitwardenCSV(&csvDocument, secretList); err != nil {
http.Error(w, "Unable to create export", http.StatusInternalServerError)
return
}
document = csvDocument.Bytes()
extension = "csv"
contentType = "text/csv; charset=utf-8"
case "json":
document, err = json.MarshalIndent(entries.NewBitwardenExport(secretList), "", " ")
if err != nil {
http.Error(w, "Unable to create export", http.StatusInternalServerError)
return
}
extension = "json"
contentType = "application/json; charset=utf-8"
default:
http.Error(w, "Unsupported export format", http.StatusBadRequest)
return
}
filename := "maxwarden-bitwarden-export-" + time.Now().UTC().Format("2006-01-02") + ".json"
filename := "maxwarden-bitwarden-export-" + time.Now().UTC().Format("2006-01-02") + "." + extension
w.Header().Set("Cache-Control", "no-store")
w.Header().Set("Content-Disposition", `attachment; filename="`+filename+`"`)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("Content-Type", contentType)
w.Header().Set("Pragma", "no-cache")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.Write(document)