From 02db7a917d80dc5ec654d7bb38d86b7284d13368 Mon Sep 17 00:00:00 2001 From: Dmitry Verkhoturov Date: Sun, 28 Jan 2024 13:46:15 +0100 Subject: [PATCH] fix lack of error on file site export Previously, status 200 was set for file export, which is used for backup, which resulted in an inability to set an error status code in case of a problem with file generation. After this change, status code 200 would be written automatically by Go before we start writing the response's body. --- backend/app/rest/api/migrator.go | 1 - backend/app/rest/api/migrator_test.go | 9 +++++++++ backend/app/rest/api/rss.go | 1 - backend/remark.rest | 8 ++++++++ 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/backend/app/rest/api/migrator.go b/backend/app/rest/api/migrator.go index 731a4ed56b..4a38c2a9aa 100644 --- a/backend/app/rest/api/migrator.go +++ b/backend/app/rest/api/migrator.go @@ -136,7 +136,6 @@ func (m *Migrator) exportCtrl(w http.ResponseWriter, r *http.Request) { 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) gzWriter := gzip.NewWriter(w) defer func() { if e := gzWriter.Close(); e != nil { diff --git a/backend/app/rest/api/migrator_test.go b/backend/app/rest/api/migrator_test.go index e1f5ff608d..4625087841 100644 --- a/backend/app/rest/api/migrator_test.go +++ b/backend/app/rest/api/migrator_test.go @@ -391,6 +391,15 @@ func TestMigrator_Export(t *testing.T) { require.Equal(t, http.StatusAccepted, resp.StatusCode) waitForMigrationCompletion(t, ts) + // export wrong site, should result in error + req, err = http.NewRequest("GET", ts.URL+"/api/v1/admin/export?mode=file&site=test", http.NoBody) + require.NoError(t, err) + req.SetBasicAuth("admin", "password") + resp, err = client.Do(req) + require.NoError(t, err) + require.Equal(t, http.StatusInternalServerError, resp.StatusCode) + require.Equal(t, "application/json", resp.Header.Get("Content-Type")) + // check file mode req, err = http.NewRequest("GET", ts.URL+"/api/v1/admin/export?mode=file&site=remark42", http.NoBody) require.NoError(t, err) diff --git a/backend/app/rest/api/rss.go b/backend/app/rest/api/rss.go index 4be92da079..da6617d6c2 100644 --- a/backend/app/rest/api/rss.go +++ b/backend/app/rest/api/rss.go @@ -56,7 +56,6 @@ func (s *rss) postCommentsCtrl(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/xml; charset=utf-8") w.WriteHeader(http.StatusOK) - if _, err = w.Write(data); err != nil { log.Printf("[WARN] failed to send response to %s, %s", r.RemoteAddr, err) } diff --git a/backend/remark.rest b/backend/remark.rest index 7febc2180e..da088aedc8 100644 --- a/backend/remark.rest +++ b/backend/remark.rest @@ -103,6 +103,14 @@ GET {{host}}/api/v1/admin/blocked?site={{site}} ### delete comment by id DELETE {{host}}/api/v1/admin/comment/3665976683?site={{site}}&url={{url}} +### export site (for backup) +GET {{host}}/api/v1/admin/export?site={{site}}&mode=stream +X-JWT: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJyZW1hcmsiLCJleHAiOjE5NzYwNTY3NTYsImp0aSI6IjJlOGJmMTE5OTI0MjQxMDRjYjFhZGRlODllMWYwNGFiMTg4YWZjMzQiLCJpYXQiOjE1NzYwNTY0NTYsImlzcyI6InJlbWFyazQyIiwidXNlciI6eyJuYW1lIjoiZGV2X3VzZXIiLCJpZCI6ImRldl91c2VyIiwicGljdHVyZSI6Imh0dHA6Ly8xMjcuMC4wLjE6ODA4MC9hcGkvdjEvYXZhdGFyL2NjZmEyYWJkMDE2Njc2MDViNGUxZmM0ZmNiOTFiMWUxYWYzMjMyNDAuaW1hZ2UiLCJhdHRycyI6eyJhZG1pbiI6dHJ1ZSwiYmxvY2tlZCI6ZmFsc2V9fX0.6Qt5s2enBMRC-Jmsua01yViVYI95Dx6BPBMaNjj36d4 + +### export site (for backup) to .gz file +GET {{host}}/api/v1/admin/export?site={{site}}&mode=file +X-JWT: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJyZW1hcmsiLCJleHAiOjE5NzYwNTY3NTYsImp0aSI6IjJlOGJmMTE5OTI0MjQxMDRjYjFhZGRlODllMWYwNGFiMTg4YWZjMzQiLCJpYXQiOjE1NzYwNTY0NTYsImlzcyI6InJlbWFyazQyIiwidXNlciI6eyJuYW1lIjoiZGV2X3VzZXIiLCJpZCI6ImRldl91c2VyIiwicGljdHVyZSI6Imh0dHA6Ly8xMjcuMC4wLjE6ODA4MC9hcGkvdjEvYXZhdGFyL2NjZmEyYWJkMDE2Njc2MDViNGUxZmM0ZmNiOTFiMWUxYWYzMjMyNDAuaW1hZ2UiLCJhdHRycyI6eyJhZG1pbiI6dHJ1ZSwiYmxvY2tlZCI6ZmFsc2V9fX0.6Qt5s2enBMRC-Jmsua01yViVYI95Dx6BPBMaNjj36d4 + ### get post info GET {{host}}/api/v1/info?site={{site}}&url={{url}}