From 7c7426f4c59ec2422f26c5c253e434c04a50de2e Mon Sep 17 00:00:00 2001 From: Umputun Date: Fri, 29 Dec 2017 15:58:11 -0600 Subject: [PATCH] ability to export gz file --- README.md | 2 +- app/rest/admin.go | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 0593a1f7..c54e5a3c 100644 --- a/README.md +++ b/README.md @@ -109,5 +109,5 @@ Sort can be `time` or `score`. Supported sort order with prefix -/+, i.e. `-time - `DELETE /api/v1/admin/comment/{id}?site=site-id&url=post-url` - delete comment by `id`. _auth and admin required_ - `PUT /api/v1/admin/user/{userid}?site=site-id&block=1` - block or unblock user. _auth and admin required_ -- `GET /api/v1/admin/export?site=side-id&block=1` - export all comments. _auth and admin required_ +- `GET /api/v1/admin/export?site=side-id&mode=[stream|file]` - export all comments to json stream or gz file. _auth and admin required_ - `PUT /api/v1/admin/pin/{id}?site=site-id&url=post-url&pin=1` - pin or unpin comment. _auth and admin required_ diff --git a/app/rest/admin.go b/app/rest/admin.go index 0b6f716d..a6879133 100644 --- a/app/rest/admin.go +++ b/app/rest/admin.go @@ -1,8 +1,12 @@ package rest import ( + "compress/gzip" + "fmt" + "io" "log" "net/http" + "time" "github.com/go-chi/chi" "github.com/go-chi/render" @@ -76,10 +80,19 @@ func (a *admin) setPinCtrl(w http.ResponseWriter, r *http.Request) { render.JSON(w, r, JSON{"id": commentID, "loc": locator, "pin": pinStatus}) } -// GET /export?site=site-id +// GET /export?site=site-id?mode=file|stream func (a *admin) exportCtrl(w http.ResponseWriter, r *http.Request) { siteID := r.URL.Query().Get("site") - if err := a.exporter.Export(w, siteID); err != nil { + var writer io.Writer = w + if r.URL.Query().Get("mode") == "file" { + exportFile := fmt.Sprintf("%s-%s.json.gz", siteID, time.Now().Format("20060102")) + w.Header().Set("Content-Type", "application/gzip") + w.Header().Set("Content-Disposition", "attachment;filename="+exportFile) + w.WriteHeader(http.StatusOK) + writer = gzip.NewWriter(w) + } + + if err := a.exporter.Export(writer, siteID); err != nil { httpError(w, r, http.StatusInternalServerError, err, "export failed") } }