mirror of
https://github.com/MizuchiLabs/mantrae.git
synced 2026-04-26 02:19:27 -05:00
fix uploads and downloads
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user