diff --git a/main.go b/main.go index d09e4f0..5a028d5 100644 --- a/main.go +++ b/main.go @@ -199,6 +199,7 @@ func (web *Web) initRouter() { web.router.GET("/backup/create", web.backupCreate) web.router.POST("/backup/test", web.backupTest) web.router.POST("/backup/restore", web.backupRestore) + web.router.POST("/backup/delete", web.backupDelete) web.router.SetTrustedProxies(nil) } @@ -1157,6 +1158,60 @@ func (web *Web) backupRestore(c *gin.Context) { }) } +// backupRestore (/backup/restore/:id) either restores the filesInBackupDir[id] file or from an uploaded file +func (web *Web) backupDelete(c *gin.Context) { + importID, err := strconv.Atoi(c.PostForm("id")) + if err != nil { + c.AbortWithError(http.StatusBadRequest, err) + return + } + if importID <= 0 { + c.Redirect(http.StatusSeeOther, "/backup/view") + return + } + + if !viper.IsSet("database.backup") { + c.HTML(http.StatusOK, "backupView", gin.H{"Error": "database.backup not set"}) + return + } + if !viper.IsSet("database.backup.schedule") { + c.HTML(http.StatusOK, "backupView", gin.H{"Error": "database.backup.schedule not set"}) + return + } + if !viper.IsSet("database.backup.path") { + c.HTML(http.StatusOK, "backupView", gin.H{"Error": "database.backup.path not set"}) + return + } + + backupPath := viper.GetString("database.backup.path") + + backupDir, err := filepath.Abs(filepath.Dir(backupPath)) + if err != nil { + c.HTML(http.StatusOK, "backupView", gin.H{"Error": err}) + return + } + + filesInBackupDir, err := ioutil.ReadDir(backupDir) + if err != nil { + c.HTML(http.StatusOK, "backupView", gin.H{"Error": err}) + return + } + if importID >= len(filesInBackupDir) { + c.HTML(http.StatusOK, "backupView", gin.H{"Error": err}) + return + } + + backupFileName := filesInBackupDir[importID] + backupFullPath := filepath.Join(backupDir, backupFileName.Name()) + + err = os.Remove(backupFullPath) + if err != nil { + c.HTML(http.StatusOK, "backupView", gin.H{"Error": err}) + return + } + c.Redirect(http.StatusSeeOther, "/backup/view") +} + // exportWatch (/watch/export/:id) creates a json export of the current watch func (web *Web) exportWatch(c *gin.Context) { watchID := c.Param("id") diff --git a/templates/backup/view.html b/templates/backup/view.html index 70fd11b..594de15 100644 --- a/templates/backup/view.html +++ b/templates/backup/view.html @@ -23,6 +23,7 @@ GoWatch Backups File Test Restore + Delete Download @@ -38,9 +39,10 @@ GoWatch Backups - + - + + {{ range $i, $backup := .Backups }} @@ -54,7 +56,13 @@ GoWatch Backups
- + +
+ + +
+ +
diff --git a/todo.md b/todo.md index 1638626..2290b99 100644 --- a/todo.md +++ b/todo.md @@ -8,7 +8,7 @@ - ~~scheduled backup~~ - ~~test~~ - ~~restore~~ - - delete + - ~~delete~~ - download - ~~restore from upload~~ - browserless function filters