Added the backup feature

This commit is contained in:
2025-07-17 00:12:00 +08:00
parent b4b0ad9bba
commit 9fff7bcfca
39 changed files with 1876 additions and 1018 deletions

28
internal/models/backup.go Normal file
View File

@@ -0,0 +1,28 @@
package models
// Git备份相关类型定义
type (
// AuthMethod 定义Git认证方式
AuthMethod string
)
const (
// 认证方式
Token AuthMethod = "token"
SSHKey AuthMethod = "ssh_key"
UserPass AuthMethod = "user_pass"
)
// GitBackupConfig Git备份配置
type GitBackupConfig struct {
Enabled bool `json:"enabled"`
RepoURL string `json:"repo_url"`
AuthMethod AuthMethod `json:"auth_method"`
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
Token string `json:"token,omitempty"`
SSHKeyPath string `json:"ssh_key_path,omitempty"`
SSHKeyPass string `json:"ssh_key_passphrase,omitempty"`
BackupInterval int `json:"backup_interval"` // 分钟
AutoBackup bool `json:"auto_backup"`
}

View File

@@ -124,7 +124,7 @@ type AppConfig struct {
Editing EditingConfig `json:"editing"` // 编辑设置
Appearance AppearanceConfig `json:"appearance"` // 外观设置
Updates UpdatesConfig `json:"updates"` // 更新设置
Sync GitSyncConfig `json:"sync"` // Git同步设置
Backup GitBackupConfig `json:"backup"` // Git备份设置
Metadata ConfigMetadata `json:"metadata"` // 配置元数据
}
@@ -139,7 +139,6 @@ func NewDefaultAppConfig() *AppConfig {
currentDir, _ := os.UserHomeDir()
dataDir := filepath.Join(currentDir, ".voidraft", "data")
repoPath := filepath.Join(currentDir, ".voidraft", "sync-repo")
return &AppConfig{
General: GeneralConfig{
@@ -175,10 +174,10 @@ func NewDefaultAppConfig() *AppConfig {
CustomTheme: *NewDefaultCustomThemeConfig(),
},
Updates: UpdatesConfig{
Version: "1.2.0",
Version: "1.3.0",
AutoUpdate: true,
PrimarySource: UpdateSourceGithub,
BackupSource: UpdateSourceGitea,
PrimarySource: UpdateSourceGitea,
BackupSource: UpdateSourceGithub,
BackupBeforeUpdate: true,
UpdateTimeout: 30,
Github: GithubConfig{
@@ -191,21 +190,16 @@ func NewDefaultAppConfig() *AppConfig {
Repo: "voidraft",
},
},
Sync: GitSyncConfig{
Enabled: false,
RepoURL: "",
Branch: "main",
AuthMethod: Token,
Username: "",
Password: "",
Token: "",
SSHKeyPath: "",
SyncInterval: 60,
LastSyncTime: time.Time{},
AutoSync: true,
LocalRepoPath: repoPath,
SyncStrategy: LocalFirst,
FilesToSync: []string{"voidraft.db"},
Backup: GitBackupConfig{
Enabled: false,
RepoURL: "",
AuthMethod: UserPass,
Username: "",
Password: "",
Token: "",
SSHKeyPath: "",
BackupInterval: 60,
AutoBackup: false,
},
Metadata: ConfigMetadata{
LastUpdated: time.Now().Format(time.RFC3339),

View File

@@ -11,7 +11,7 @@ type Document struct {
Content string `json:"content" db:"content"`
CreatedAt time.Time `json:"createdAt" db:"created_at"`
UpdatedAt time.Time `json:"updatedAt" db:"updated_at"`
IsDeleted bool `json:"is_deleted"`
IsDeleted bool `json:"is_deleted" db:"is_deleted"`
IsLocked bool `json:"is_locked" db:"is_locked"` // 锁定标志,锁定的文档无法被删除
}

View File

@@ -1,63 +0,0 @@
package models
import "time"
// AuthMethod 定义Git认证方式
type AuthMethod string
const (
Token AuthMethod = "token" // 个人访问令牌
SSHKey AuthMethod = "ssh_key" // SSH密钥
UserPass AuthMethod = "user_pass" // 用户名密码
)
// SyncStrategy 定义同步策略
type SyncStrategy string
const (
// LocalFirst 本地优先:如有冲突,保留本地修改
LocalFirst SyncStrategy = "local_first"
// RemoteFirst 远程优先:如有冲突,采用远程版本
RemoteFirst SyncStrategy = "remote_first"
)
// GitSyncConfig 保存Git同步的配置信息
type GitSyncConfig struct {
Enabled bool `json:"enabled"`
RepoURL string `json:"repo_url"`
Branch string `json:"branch"`
AuthMethod AuthMethod `json:"auth_method"`
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
Token string `json:"token,omitempty"`
SSHKeyPath string `json:"ssh_key_path,omitempty"`
SSHKeyPassphrase string `json:"ssh_key_passphrase,omitempty"`
SyncInterval int `json:"sync_interval"` // 同步间隔(分钟)
LastSyncTime time.Time `json:"last_sync_time"`
AutoSync bool `json:"auto_sync"` // 是否启用自动同步
LocalRepoPath string `json:"local_repo_path"`
SyncStrategy SyncStrategy `json:"sync_strategy"` // 合并冲突策略
FilesToSync []string `json:"files_to_sync"` // 要同步的文件列表,默认为数据库文件
}
// GitSyncStatus 保存同步状态信息
type GitSyncStatus struct {
IsSyncing bool `json:"is_syncing"`
LastSyncTime time.Time `json:"last_sync_time"`
LastSyncStatus string `json:"last_sync_status"` // success, failed, conflict
LastErrorMsg string `json:"last_error_msg,omitempty"`
LastCommitID string `json:"last_commit_id,omitempty"`
RemoteCommitID string `json:"remote_commit_id,omitempty"`
CommitAhead int `json:"commit_ahead"` // 本地领先远程的提交数
CommitBehind int `json:"commit_behind"` // 本地落后远程的提交数
}
// SyncLogEntry 记录每次同步操作的日志
type SyncLogEntry struct {
ID int64 `json:"id"`
Timestamp time.Time `json:"timestamp"`
Action string `json:"action"` // push, pull, reset
Status string `json:"status"` // success, failed
Message string `json:"message,omitempty"`
ChangedFiles int `json:"changed_files"`
}

View File

@@ -66,9 +66,9 @@ func NewDefaultDarkTheme() ThemeColorConfig {
Cursor: "#ffffff",
Selection: "#0865a9",
SelectionBlur: "#225377",
ActiveLine: "#ffffff",
LineNumber: "#ffffff",
ActiveLineNumber: "#ffffff",
ActiveLine: "#ffffff0a",
LineNumber: "#ffffff26",
ActiveLineNumber: "#ffffff99",
// 边框分割线
BorderColor: "#1e222a",
@@ -104,17 +104,17 @@ func NewDefaultLightTheme() ThemeColorConfig {
Cursor: "#000000",
Selection: "#77baff",
SelectionBlur: "#b2c2ca",
ActiveLine: "#000000",
LineNumber: "#000000",
ActiveLineNumber: "#000000",
ActiveLine: "#0000000a",
LineNumber: "#00000040",
ActiveLineNumber: "#000000aa",
// 边框分割线
BorderColor: "#dfdfdf",
BorderLight: "#0000000d",
BorderLight: "#0000000c",
// 搜索匹配
SearchMatch: "#005cc5",
MatchingBracket: "#0000001a",
MatchingBracket: "#00000019",
}
}