ability to export gz file

This commit is contained in:
Umputun
2017-12-29 15:58:11 -06:00
parent 2ab8d6e025
commit 7c7426f4c5
2 changed files with 16 additions and 3 deletions
+1 -1
View File
@@ -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_
+15 -2
View File
@@ -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")
}
}