summaryrefslogtreecommitdiff
path: root/system/admin
diff options
context:
space:
mode:
Diffstat (limited to 'system/admin')
-rw-r--r--system/admin/handlers.go9
-rw-r--r--system/admin/upload/backup.go25
2 files changed, 29 insertions, 5 deletions
diff --git a/system/admin/handlers.go b/system/admin/handlers.go
index 4f8ae83..4734ba0 100644
--- a/system/admin/handlers.go
+++ b/system/admin/handlers.go
@@ -195,9 +195,12 @@ func configHandler(res http.ResponseWriter, req *http.Request) {
}
func backupHandler(res http.ResponseWriter, req *http.Request) {
+ ctx, cancel := context.WithCancel(context.Background())
+ defer cancel()
+
switch req.URL.Query().Get("source") {
case "system":
- err := db.Backup(res)
+ err := db.Backup(ctx, res)
if err != nil {
log.Println("Failed to run backup on system:", err)
res.WriteHeader(http.StatusInternalServerError)
@@ -205,7 +208,7 @@ func backupHandler(res http.ResponseWriter, req *http.Request) {
}
case "analytics":
- err := analytics.Backup(res)
+ err := analytics.Backup(ctx, res)
if err != nil {
log.Println("Failed to run backup on analytics:", err)
res.WriteHeader(http.StatusInternalServerError)
@@ -213,7 +216,7 @@ func backupHandler(res http.ResponseWriter, req *http.Request) {
}
case "uploads":
- err := upload.Backup(res)
+ err := upload.Backup(ctx, res)
if err != nil {
log.Println("Failed to run backup on uploads:", err)
res.WriteHeader(http.StatusInternalServerError)
diff --git a/system/admin/upload/backup.go b/system/admin/upload/backup.go
index 28b1b8e..b4f6393 100644
--- a/system/admin/upload/backup.go
+++ b/system/admin/upload/backup.go
@@ -3,6 +3,7 @@ package upload
import (
"archive/tar"
"compress/gzip"
+ "context"
"fmt"
"io"
"net/http"
@@ -13,7 +14,7 @@ import (
// Backup creates an archive of a project's uploads and writes it
// to the response as a download
-func Backup(res http.ResponseWriter) error {
+func Backup(ctx context.Context, res http.ResponseWriter) error {
ts := time.Now().Unix()
filename := fmt.Sprintf("uploads-%d.bak.tar.gz", ts)
tmp := os.TempDir()
@@ -30,7 +31,8 @@ func Backup(res http.ResponseWriter) error {
gz := gzip.NewWriter(f)
tarball := tar.NewWriter(gz)
- err = filepath.Walk("uploads", func(path string, info os.FileInfo, err error) error {
+ errChan := make(chan error, 1)
+ walkFn := func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
@@ -53,6 +55,7 @@ func Backup(res http.ResponseWriter) error {
return err
}
defer src.Close()
+
_, err = io.Copy(tarball, src)
if err != nil {
return err
@@ -70,6 +73,24 @@ func Backup(res http.ResponseWriter) error {
}
return nil
+ }
+
+ // stop processing if we get a cancellation signal
+ err = filepath.Walk("uploads", func(path string, info os.FileInfo, err error) error {
+ go func() { errChan <- walkFn(path, info, err) }()
+
+ select {
+ case <-ctx.Done():
+ if err := ctx.Err(); err != nil {
+ return err
+ }
+ case err := <-errChan:
+ if err != nil {
+ return err
+ }
+ }
+
+ return nil
})
if err != nil {
fmt.Println(err)