fix uploads and downloads

This commit is contained in:
d34dscene
2025-06-22 21:11:54 +02:00
parent a83c0df7f0
commit 11b951466f
13 changed files with 182 additions and 158 deletions
+59
View File
@@ -0,0 +1,59 @@
package handler
import (
"encoding/json"
"fmt"
"net/http"
"path/filepath"
"time"
"github.com/mizuchilabs/mantrae/internal/config"
)
func UploadBackup(a *config.App) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Limit request size to prevent memory issues
r.Body = http.MaxBytesReader(w, r.Body, 100<<20) // 100MB limit
if err := r.ParseMultipartForm(10 << 20); err != nil {
http.Error(w, "File too large or invalid form data", http.StatusBadRequest)
return
}
defer r.MultipartForm.RemoveAll()
file, header, err := r.FormFile("file")
if err != nil {
http.Error(w, "Failed to get uploaded file", http.StatusBadRequest)
return
}
defer file.Close()
extension := filepath.Ext(header.Filename)
if extension != ".db" {
http.Error(w, "Invalid file type", http.StatusBadRequest)
return
}
// Generate unique filename
filename := fmt.Sprintf("upload_%s%s",
time.Now().UTC().Format("20060102_150405"),
filepath.Ext(header.Filename))
// Store the uploaded backup using the backend
if err = a.BM.Storage.Store(r.Context(), filename, file); err != nil {
http.Error(
w,
fmt.Sprintf("Failed to store backup file: %v", err),
http.StatusInternalServerError,
)
return
}
response := map[string]string{"message": "Backup restored successfully"}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(response); err != nil {
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
return
}
}
}
-33
View File
@@ -1,33 +0,0 @@
package handler
import (
"encoding/json"
"net/http"
"github.com/mizuchilabs/mantrae/internal/util"
"github.com/mizuchilabs/mantrae/pkg/build"
)
// GetVersion returns the current version of Mantrae as a plain text response
func GetVersion(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
data := map[string]string{"version": build.Version}
if err := json.NewEncoder(w).Encode(data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
// GetPublicIP attempts to resolve the public IP address of the current machine
func GetPublicIP(w http.ResponseWriter, r *http.Request) {
machineIPs, err := util.GetPublicIPsCached()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(machineIPs); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}