153 lines
4.4 KiB
Go
153 lines
4.4 KiB
Go
package platform
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
)
|
|
|
|
// DesktopConfig is the desktop-only preference set, persisted as JSON under the
|
|
// user config dir. json tags are the contract with the WebView settings page.
|
|
type DesktopConfig struct {
|
|
ClipboardSyncEnabled bool `json:"clipboard_sync_enabled"`
|
|
LaunchAtLogin bool `json:"launch_at_login"`
|
|
DeviceName string `json:"device_name"` // empty = use the hostname
|
|
DownloadDir string `json:"download_dir"` // empty = system Downloads dir
|
|
// DeviceID is this device's stable opaque cdrop id — the broker `meta` and the
|
|
// session<->device join key. Minted once on first login and persisted so re-logins
|
|
// rotate the one managed device session (broker R2) instead of spawning duplicates.
|
|
DeviceID string `json:"device_id,omitempty"`
|
|
}
|
|
|
|
// DeviceType maps the host OS to the cdrop device kind, shown in the unified device list
|
|
// and sent as device_type when 代铸ing this device's session.
|
|
func DeviceType() string {
|
|
switch runtime.GOOS {
|
|
case "darwin":
|
|
return "macos"
|
|
case "windows":
|
|
return "windows"
|
|
case "linux":
|
|
return "linux"
|
|
default:
|
|
return "browser"
|
|
}
|
|
}
|
|
|
|
// ResolveDeviceID returns the persisted stable device_id, minting and persisting a fresh
|
|
// one ("dev_" + base64url(16 random bytes)) on first use. The format matches the backend's
|
|
// newDeviceID, so it passes validDeviceID and the broker's meta validation.
|
|
func ResolveDeviceID() (string, error) {
|
|
cfg, _ := LoadConfig()
|
|
if cfg.DeviceID != "" {
|
|
return cfg.DeviceID, nil
|
|
}
|
|
b := make([]byte, 16)
|
|
if _, err := rand.Read(b); err != nil {
|
|
return "", err
|
|
}
|
|
cfg.DeviceID = "dev_" + base64.RawURLEncoding.EncodeToString(b)
|
|
if err := SaveConfig(cfg); err != nil {
|
|
return "", err
|
|
}
|
|
return cfg.DeviceID, nil
|
|
}
|
|
|
|
// ResolveDeviceName returns the persisted device name, or the hostname when the
|
|
// user hasn't renamed it. Persisting it Go-side is required because the WebView's
|
|
// localStorage doesn't survive restart on the wails:// scheme (see session.go).
|
|
func ResolveDeviceName() string {
|
|
cfg, _ := LoadConfig()
|
|
if cfg.DeviceName != "" {
|
|
return cfg.DeviceName
|
|
}
|
|
if h, err := os.Hostname(); err == nil && h != "" {
|
|
return h
|
|
}
|
|
switch runtime.GOOS {
|
|
case "darwin":
|
|
return "Commilitia Drop (macOS)"
|
|
case "windows":
|
|
return "Commilitia Drop (Windows)"
|
|
default:
|
|
return "Commilitia Drop"
|
|
}
|
|
}
|
|
|
|
// DefaultConfig is what a fresh install gets: clipboard sync on, no autostart.
|
|
func DefaultConfig() DesktopConfig {
|
|
return DesktopConfig{ClipboardSyncEnabled: true, LaunchAtLogin: false}
|
|
}
|
|
|
|
// ResolveDownloadDir returns the directory received files land in: the user's
|
|
// configured override, or the system Downloads dir when unset.
|
|
func ResolveDownloadDir() string {
|
|
cfg, _ := LoadConfig()
|
|
if cfg.DownloadDir != "" {
|
|
return cfg.DownloadDir
|
|
}
|
|
return DefaultDownloadDir()
|
|
}
|
|
|
|
// DefaultDownloadDir is ~/Downloads (the OS default download folder; its path is
|
|
// stable across locales on macOS/Windows). Falls back to the cwd if the home
|
|
// dir can't be resolved.
|
|
func DefaultDownloadDir() string {
|
|
if home, err := os.UserHomeDir(); err == nil && home != "" {
|
|
return filepath.Join(home, "Downloads")
|
|
}
|
|
return "."
|
|
}
|
|
|
|
// configPath is ~/Library/Application Support/cdrop/config.json on macOS
|
|
// (os.UserConfigDir resolves the per-OS base).
|
|
func configPath() (string, error) {
|
|
dir, err := os.UserConfigDir()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return filepath.Join(dir, "cdrop", "config.json"), nil
|
|
}
|
|
|
|
// LoadConfig reads the persisted config, falling back to defaults when the file
|
|
// is absent or unreadable so the app always has a usable preference set.
|
|
func LoadConfig() (DesktopConfig, error) {
|
|
p, err := configPath()
|
|
if err != nil {
|
|
return DefaultConfig(), err
|
|
}
|
|
data, err := os.ReadFile(p)
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
return DefaultConfig(), nil
|
|
}
|
|
if err != nil {
|
|
return DefaultConfig(), err
|
|
}
|
|
cfg := DefaultConfig()
|
|
if err := json.Unmarshal(data, &cfg); err != nil {
|
|
return DefaultConfig(), err
|
|
}
|
|
return cfg, nil
|
|
}
|
|
|
|
// SaveConfig writes the config atomically-ish (mkdir + write) under the user
|
|
// config dir.
|
|
func SaveConfig(cfg DesktopConfig) error {
|
|
p, err := configPath()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
|
|
return err
|
|
}
|
|
data, err := json.MarshalIndent(cfg, "", " ")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return os.WriteFile(p, data, 0o644)
|
|
}
|