From cd9ad1d4d79d52885cfc620094576a91bf480049 Mon Sep 17 00:00:00 2001 From: Aaron Lindsay Date: Mon, 19 Jun 2017 21:08:49 -0400 Subject: [PATCH] gnucash: Support directly importing gzipped files --- gnucash.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/gnucash.go b/gnucash.go index 598747b..94391cf 100644 --- a/gnucash.go +++ b/gnucash.go @@ -1,6 +1,8 @@ package main import ( + "bufio" + "compress/gzip" "encoding/xml" "errors" "fmt" @@ -288,7 +290,28 @@ func GnucashImportHandler(w http.ResponseWriter, r *http.Request) { return } - gnucashImport, err := ImportGnucash(part) + bufread := bufio.NewReader(part) + gzHeader, err := bufread.Peek(2) + if err != nil { + WriteError(w, 999 /*Internal Error*/) + log.Print(err) + return + } + + // Does this look like a gzipped file? + var gnucashImport *GnucashImport + if gzHeader[0] == 0x1f && gzHeader[1] == 0x8b { + gzr, err := gzip.NewReader(bufread) + if err != nil { + WriteError(w, 999 /*Internal Error*/) + log.Print(err) + return + } + gnucashImport, err = ImportGnucash(gzr) + } else { + gnucashImport, err = ImportGnucash(bufread) + } + if err != nil { WriteError(w, 3 /*Invalid Request*/) return