package handler import ( "net/http" "os" "runtime" "strings" "u-desk/internal/agent/model" "github.com/labstack/echo/v4" ) // Ping 健康检查 func (h *Handler) Ping(c echo.Context) error { return c.JSON(http.StatusOK, model.OK(map[string]string{ "status": "ok", })) } // Info 返回 Agent 信息 func (h *Handler) Info(c echo.Context) error { hostname, _ := os.Hostname() return c.JSON(http.StatusOK, model.OK(map[string]interface{}{ "version": "0.1.0", "os": runtime.GOOS, "arch": runtime.GOARCH, "hostname": hostname, })) } // CommonPaths 返回常用系统路径 func (h *Handler) CommonPaths(c echo.Context) error { paths := map[string]string{} home, _ := os.UserHomeDir() if home != "" { paths["home"] = home paths["desktop"] = home + "/Desktop" paths["documents"] = home + "/Documents" paths["downloads"] = home + "/Downloads" } // 根据平台添加盘符/根路径 if runtime.GOOS == "windows" { for _, drive := range "ABCDEFGHIJKLMNOPQRSTUVWXYZ" { _, err := os.Stat(string(drive) + ":\\") if err == nil { paths["drive_"+strings.ToLower(string(drive))] = string(drive) + ":\\" } } } else { paths["root"] = "/" _, err := os.Stat("/home") if err == nil { paths["users"] = "/home" } } return c.JSON(http.StatusOK, model.OK(paths)) } // Drives 返回可用磁盘列表 func (h *Handler) Drives(c echo.Context) error { type DriveInfo struct { Name string `json:"name"` Path string `json:"path"` FsType string `json:"fs_type,omitempty"` Total uint64 `json:"total"` Free uint64 `json:"free"` } var drives []DriveInfo if runtime.GOOS == "windows" { for _, drive := range "ABCDEFGHIJKLMNOPQRSTUVWXYZ" { drivePath := string(drive) + ":\\" if _, err := os.Stat(drivePath); err != nil { continue } drives = append(drives, DriveInfo{ Name: strings.ToLower(string(drive)), Path: drivePath, Total: 0, Free: 0, }) } } else { parts, err := os.ReadDir("/") if err == nil { for _, p := range parts { name := p.Name() if len(name) == 2 && name[0] != '.' && name[1] != '.' && p.IsDir() { // 可能是挂载点 fullPath := "/" + name if stat, err := os.Stat(fullPath); err == nil && stat.IsDir() { drives = append(drives, DriveInfo{ Name: name, Path: fullPath, }) _ = stat } } } } // 至少返回根目录 if len(drives) == 0 { drives = append(drives, DriveInfo{Name: "/", Path: "/"}) } } return c.JSON(http.StatusOK, model.OK(drives)) }