mirror of
https://github.com/0x1d/rcond.git
synced 2025-12-14 18:25:21 +01:00
feat: implement file upload
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
@@ -26,3 +27,31 @@ func HandleShutdown(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]string{"status": "success"})
|
||||
}
|
||||
|
||||
func HandleFileUpload(w http.ResponseWriter, r *http.Request) {
|
||||
// Parse the request body
|
||||
var fileUpload struct {
|
||||
Path string `json:"path"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&fileUpload); err != nil {
|
||||
WriteError(w, "Invalid request body", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Decode base64 encoded content to bytes
|
||||
contentBytes, err := base64.StdEncoding.DecodeString(fileUpload.Content)
|
||||
if err != nil {
|
||||
WriteError(w, "Failed to decode base64 content", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Store the file
|
||||
if err := system.StoreFile(fileUpload.Path, contentBytes); err != nil {
|
||||
WriteError(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]string{"status": "success"})
|
||||
}
|
||||
|
||||
@@ -80,6 +80,7 @@ func (s *Server) RegisterRoutes() {
|
||||
s.router.HandleFunc("/cluster/join", s.verifyToken(ClusterAgentHandler(s.clusterAgent, HandleClusterJoin))).Methods(http.MethodPost)
|
||||
s.router.HandleFunc("/cluster/leave", s.verifyToken(ClusterAgentHandler(s.clusterAgent, HandleClusterLeave))).Methods(http.MethodPost)
|
||||
s.router.HandleFunc("/cluster/event", s.verifyToken(ClusterAgentHandler(s.clusterAgent, HandleClusterEvent))).Methods(http.MethodPost)
|
||||
s.router.HandleFunc("/system/file", s.verifyToken(HandleFileUpload)).Methods(http.MethodPost)
|
||||
}
|
||||
|
||||
func (s *Server) healthHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
25
pkg/system/file.go
Normal file
25
pkg/system/file.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// StoreFile stores a file on the file system at the given path.
|
||||
// If the path does not exist, it will be created.
|
||||
func StoreFile(path string, content []byte) error {
|
||||
// Ensure the directory exists
|
||||
dir := filepath.Dir(path)
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
return fmt.Errorf("failed to create directory: %v", err)
|
||||
}
|
||||
|
||||
// Write the file
|
||||
if err := ioutil.WriteFile(path, content, 0644); err != nil {
|
||||
return fmt.Errorf("failed to write file: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user