diff options
author | Steve Manuel <nilslice@gmail.com> | 2017-05-15 04:44:21 -0700 |
---|---|---|
committer | Steve Manuel <nilslice@gmail.com> | 2017-05-15 04:44:21 -0700 |
commit | c76b8e021091e08c49bd7ba28e1b1c8a7632f749 (patch) | |
tree | 8afcae4878e8f173928c0810cad996468e45cac7 | |
parent | 779631d5dc46afbc765ee138a79bab962f11312b (diff) |
manually call remove and close, debug response
-rw-r--r-- | system/admin/export.go | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/system/admin/export.go b/system/admin/export.go index cdd43b7..46a659b 100644 --- a/system/admin/export.go +++ b/system/admin/export.go @@ -7,7 +7,6 @@ import ( "log" "net/http" "os" - "path/filepath" "strings" "time" @@ -70,8 +69,6 @@ func exportCSV(res http.ResponseWriter, req *http.Request, pt func() interface{} res.WriteHeader(http.StatusInternalServerError) return } - defer os.Remove(filepath.Join(os.TempDir(), tmpFile.Name())) - defer tmpFile.Close() csvBuf := csv.NewWriter(tmpFile) @@ -80,6 +77,7 @@ func exportCSV(res http.ResponseWriter, req *http.Request, pt func() interface{} // get content data and loop through creating a csv row per result bb := db.ContentAll(t) + // add field names to first row err = csvBuf.Write(fields) if err != nil { res.WriteHeader(http.StatusInternalServerError) @@ -111,24 +109,29 @@ func exportCSV(res http.ResponseWriter, req *http.Request, pt func() interface{} csvBuf.Flush() // write the buffer to a content-disposition response - csvB, err := ioutil.ReadAll(tmpFile) + fi, err := tmpFile.Stat() if err != nil { - log.Println("Failed to read tmp file for CSV export:", err) + log.Println("Failed to read tmp file info for CSV export:", err) res.WriteHeader(http.StatusInternalServerError) return } + err = tmpFile.Close() + if err != nil { + log.Println("Failed to close tmp file for CSV export:", err) + } + ts := time.Now().Unix() disposition := `attachment; filename="export-%s-%d.csv"` res.Header().Set("Content-Type", "text/csv") res.Header().Set("Content-Disposition", fmt.Sprintf(disposition, t, ts)) - res.Header().Set("Content-Length", fmt.Sprintf("%d", len(csvB))) + res.Header().Set("Content-Length", fmt.Sprintf("%d", int(fi.Size()))) - _, err = res.Write(csvB) + http.ServeFile(res, req, tmpFile.Name()) + + err = os.Remove(tmpFile.Name()) if err != nil { - log.Println("Failed to write tmp file to response for CSV export:", err) - res.WriteHeader(http.StatusInternalServerError) - return + log.Println("Failed to remove tmp file for CSV export:", err) } } |