Add backup delete endpoint
This commit is contained in:
parent
5036482f17
commit
12b4e514aa
@ -121,6 +121,34 @@ func List() ([]Manifest, error) {
|
|||||||
return out, nil
|
return out, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Delete(id string) error {
|
||||||
|
id = strings.TrimSpace(id)
|
||||||
|
if !safeID(id) {
|
||||||
|
return fmt.Errorf("invalid backup id")
|
||||||
|
}
|
||||||
|
|
||||||
|
archiveName := id + ".tar.gz"
|
||||||
|
if manifest, err := readManifestSidecar(id); err == nil && strings.TrimSpace(manifest.Archive) != "" {
|
||||||
|
archiveName = filepath.Base(manifest.Archive)
|
||||||
|
}
|
||||||
|
|
||||||
|
archivePath := filepath.Join(RootDir, archiveName)
|
||||||
|
manifestPath := filepath.Join(RootDir, id+".json")
|
||||||
|
|
||||||
|
archiveErr := os.Remove(archivePath)
|
||||||
|
if archiveErr != nil && !errors.Is(archiveErr, os.ErrNotExist) {
|
||||||
|
return archiveErr
|
||||||
|
}
|
||||||
|
manifestErr := os.Remove(manifestPath)
|
||||||
|
if manifestErr != nil && !errors.Is(manifestErr, os.ErrNotExist) {
|
||||||
|
return manifestErr
|
||||||
|
}
|
||||||
|
if errors.Is(archiveErr, os.ErrNotExist) && errors.Is(manifestErr, os.ErrNotExist) {
|
||||||
|
return os.ErrNotExist
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func Restore(cfg *state.Config, id string) (Manifest, error) {
|
func Restore(cfg *state.Config, id string) (Manifest, error) {
|
||||||
if err := requireMinecraft(cfg); err != nil {
|
if err := requireMinecraft(cfg); err != nil {
|
||||||
return Manifest{}, err
|
return Manifest{}, err
|
||||||
|
|||||||
@ -2,7 +2,9 @@ package handlers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
agentbackup "zlh-agent/internal/backup"
|
agentbackup "zlh-agent/internal/backup"
|
||||||
@ -20,6 +22,39 @@ func HandleGameBackups(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func HandleGameBackupByID(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodDelete {
|
||||||
|
writeJSONError(w, http.StatusMethodNotAllowed, "DELETE only")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
endOp, ok := beginHandlerOperation(w, "backup_delete", true, "deleting backup")
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer endOp()
|
||||||
|
|
||||||
|
if _, ok := requireBackupConfig(w); !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
id := strings.TrimPrefix(r.URL.Path, "/game/backups/")
|
||||||
|
id = strings.TrimSpace(strings.Trim(id, "/"))
|
||||||
|
if id == "" || strings.Contains(id, "/") {
|
||||||
|
writeJSONError(w, http.StatusBadRequest, "backup id required")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := agentbackup.Delete(id); err != nil {
|
||||||
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
|
writeJSONError(w, http.StatusNotFound, "backup not found")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
writeJSONError(w, http.StatusBadRequest, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
writeJSON(w, http.StatusOK, map[string]any{"deleted": true, "id": id})
|
||||||
|
}
|
||||||
|
|
||||||
func handleGameBackupsList(w http.ResponseWriter, r *http.Request) {
|
func handleGameBackupsList(w http.ResponseWriter, r *http.Request) {
|
||||||
if _, ok := requireBackupConfig(w); !ok {
|
if _, ok := requireBackupConfig(w); !ok {
|
||||||
return
|
return
|
||||||
|
|||||||
@ -886,6 +886,7 @@ func NewMux() *http.ServeMux {
|
|||||||
m.HandleFunc("/game/players", handleGamePlayers)
|
m.HandleFunc("/game/players", handleGamePlayers)
|
||||||
m.HandleFunc("/game/backups", agenthandlers.HandleGameBackups)
|
m.HandleFunc("/game/backups", agenthandlers.HandleGameBackups)
|
||||||
m.HandleFunc("/game/backups/restore", agenthandlers.HandleGameBackupRestore)
|
m.HandleFunc("/game/backups/restore", agenthandlers.HandleGameBackupRestore)
|
||||||
|
m.HandleFunc("/game/backups/", agenthandlers.HandleGameBackupByID)
|
||||||
m.HandleFunc("/game/mods", agenthandlers.HandleGameMods)
|
m.HandleFunc("/game/mods", agenthandlers.HandleGameMods)
|
||||||
m.HandleFunc("/game/mods/install", agenthandlers.HandleGameModsInstall)
|
m.HandleFunc("/game/mods/install", agenthandlers.HandleGameModsInstall)
|
||||||
m.HandleFunc("/game/mods/", agenthandlers.HandleGameModByID)
|
m.HandleFunc("/game/mods/", agenthandlers.HandleGameModByID)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user