Files
up_go/api/ExtlinuxWrite.go
wt 7f0e5b7b10
Build / Build (push) Successful in 10s
up some deprecated fixes
2026-03-19 14:54:33 +07:00

99 lines
2.4 KiB
Go

package api
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"path/filepath"
"syscall"
"up/utils"
)
func ExtlinuxWrite(w http.ResponseWriter, r *http.Request) {
var MOUNT_POINT = os.Getenv("MOUNT_POINT")
var EXTLINUX_FILE = os.Getenv("EXTLINUX_FILE")
r.Body = http.MaxBytesReader(w, r.Body, 4096)
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, `{"error":"Failed to read body"}`, http.StatusBadRequest)
return
}
var requestData struct {
Content string `json:"content"`
}
if err := json.Unmarshal(body, &requestData); err != nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(`{"error":"Invalid JSON"}`))
return
}
if requestData.Content == "" {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(`{"error":"Missing content field"}`))
return
}
timestamp := r.URL.Query().Get("timestamp")
if timestamp != "" {
decoded, err := url.QueryUnescape(timestamp)
if err == nil {
timestamp = decoded
}
}
if utils.MountDevice() < 0 {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`{"error":"Failed to mount device"}`))
return
}
filePath := filepath.Join(MOUNT_POINT, EXTLINUX_FILE)
fmt.Println(filePath)
f, err := os.Create(filePath)
if err != nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`{"error":"Failed to open file"}`))
return
}
defer f.Close()
if _, err := f.WriteString(requestData.Content); err != nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`{"error":"Failed to write file"}`))
return
}
if timestamp != "" {
versionPath := filepath.Join(MOUNT_POINT, EXTLINUX_FILE+".version")
vf, err := os.Create(versionPath)
if err == nil {
var readableDate string
if len(timestamp) >= 19 {
readableDate = timestamp[:10] + " " + timestamp[11:19] // "YYYY-MM-DD HH:MM:SS"
} else {
readableDate = timestamp
}
vf.WriteString(readableDate + "\n")
vf.Close()
log.Printf("Written extlinux.conf version: %s\n", readableDate)
}
}
syscall.Sync()
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"status":"success","message":"extlinux.conf updated"}`))
}