From 281ba818cd29397bfdffe052909ea679615c3b18 Mon Sep 17 00:00:00 2001 From: tickstep Date: Sat, 16 Mar 2024 22:06:20 +0800 Subject: [PATCH] support resource drive for sync command --- internal/command/sync.go | 27 +++++++++++++++++++++++---- internal/syncdrive/sync_task.go | 11 +++++++++-- internal/syncdrive/sync_task_mgr.go | 17 +++++++++++++---- 3 files changed, 45 insertions(+), 10 deletions(-) diff --git a/internal/command/sync.go b/internal/command/sync.go index 70f6b80..fd67bce 100644 --- a/internal/command/sync.go +++ b/internal/command/sync.go @@ -71,7 +71,8 @@ func CmdSync() cli.Command { "name": "设计文档备份", "localFolderPath": "D:/tickstep/Documents/设计文档", "panFolderPath": "/sync_drive/我的文档", - "mode": "upload" + "mode": "upload", + "driveName": "backup" } ] } @@ -80,13 +81,14 @@ name - 任务名称 localFolderPath - 本地目录 panFolderPath - 网盘目录 mode - 模式,支持两种: upload(备份本地文件到云盘),download(备份云盘文件到本地) +driveName - 网盘名称,backup(备份盘),resource(资源盘) 例子: 1. 查看帮助 aliyunpan sync start -h - 2. 使用命令行配置启动同步备份服务,将本地目录 D:\tickstep\Documents\设计文档 中的文件备份上传到云盘目录 /sync_drive/我的文档 - aliyunpan sync start -ldir "D:\tickstep\Documents\设计文档" -pdir "/sync_drive/我的文档" -mode "upload" + 2. 使用命令行配置启动同步备份服务,将本地目录 D:\tickstep\Documents\设计文档 中的文件备份上传到"备份盘"的云盘目录 /sync_drive/我的文档 + aliyunpan sync start -ldir "D:\tickstep\Documents\设计文档" -pdir "/sync_drive/我的文档" -mode "upload" -drive "backup" 3. 使用命令行配置启动同步备份服务,将云盘目录 /sync_drive/我的文档 中的文件备份下载到本地目录 D:\tickstep\Documents\设计文档 aliyunpan sync start -ldir "D:\tickstep\Documents\设计文档" -pdir "/sync_drive/我的文档" -mode "download" @@ -158,6 +160,7 @@ mode - 模式,支持两种: upload(备份本地文件到云盘),download(备 localDir := c.String("ldir") panDir := c.String("pdir") mode := c.String("mode") + driveName := c.String("drive") if localDir != "" && panDir != "" { // make path absolute if !utils.IsLocalAbsPath(localDir) { @@ -199,6 +202,18 @@ mode - 模式,支持两种: upload(备份本地文件到云盘),download(备 task.Id = utils.Md5Str(task.LocalFolderPath) task.Priority = syncOpt task.UserId = activeUser.UserId + + // drive id + task.DriveName = driveName + if strings.ToLower(task.DriveName) == "backup" { + task.DriveId = activeUser.DriveList.GetFileDriveId() + } else if strings.ToLower(task.DriveName) == "resource" { + task.DriveId = activeUser.DriveList.GetResourceDriveId() + } + if len(task.DriveId) == 0 { + task.DriveName = "backup" + task.DriveId = activeUser.DriveList.GetFileDriveId() + } } RunSync(task, dp, up, downloadBlockSize, uploadBlockSize, syncOpt, c.Int("ldt")) @@ -206,6 +221,10 @@ mode - 模式,支持两种: upload(备份本地文件到云盘),download(备 return nil }, Flags: []cli.Flag{ + cli.StringFlag{ + Name: "drive", + Usage: "drive name, 网盘名称,backup(备份盘),resource(资源盘)", + }, cli.StringFlag{ Name: "ldir", Usage: "local dir, 本地文件夹完整路径", @@ -310,7 +329,7 @@ func RunSync(defaultTask *syncdrive.SyncTask, fileDownloadParallel, fileUploadPa LocalFileModifiedCheckIntervalSec: localDelayTime, FileRecorder: fileRecorder, } - syncMgr := syncdrive.NewSyncTaskManager(activeUser, activeUser.DriveList.GetFileDriveId(), panClient, syncFolderRootPath, option) + syncMgr := syncdrive.NewSyncTaskManager(activeUser, panClient, syncFolderRootPath, option) syncConfigFile := syncMgr.ConfigFilePath() if tasks != nil { syncConfigFile = "(使用命令行配置)" diff --git a/internal/syncdrive/sync_task.go b/internal/syncdrive/sync_task.go index 197a3ef..2fd29b4 100644 --- a/internal/syncdrive/sync_task.go +++ b/internal/syncdrive/sync_task.go @@ -32,6 +32,8 @@ type ( Id string `json:"id"` // UserId 账号ID UserId string `json:"userId"` + // DriveName 网盘名称,backup-备份盘,resource-资源盘 + DriveName string `json:"driveName"` // DriveId 网盘ID,目前支持文件网盘 DriveId string `json:"-"` // LocalFolderPath 本地目录 @@ -41,9 +43,9 @@ type ( // Mode 同步模式 Mode SyncMode `json:"mode"` // CycleMode 循环模式,OneTime-运行一次,InfiniteLoop-无限循环模式 - CycleModeType CycleMode `json:"cycleModeType"` + CycleModeType CycleMode `json:"-"` // Priority 优先级选项 - Priority SyncPriorityOption `json:"priority"` + Priority SyncPriorityOption `json:"-"` // LastSyncTime 上一次同步时间 LastSyncTime string `json:"lastSyncTime"` @@ -119,6 +121,11 @@ func (t *SyncTask) String() string { } builder.WriteString("本地目录: " + t.LocalFolderPath + "\n") builder.WriteString("云盘目录: " + t.PanFolderPath + "\n") + driveName := "备份盘" + if strings.ToLower(t.DriveName) == "resource" { + driveName = "资源盘" + } + builder.WriteString("目标网盘: " + driveName + "\n") return builder.String() } diff --git a/internal/syncdrive/sync_task_mgr.go b/internal/syncdrive/sync_task_mgr.go index 8b12490..ef47106 100644 --- a/internal/syncdrive/sync_task_mgr.go +++ b/internal/syncdrive/sync_task_mgr.go @@ -9,6 +9,7 @@ import ( "github.com/tickstep/library-go/logger" "io/ioutil" "path" + "strings" "time" ) @@ -38,7 +39,6 @@ type ( syncDriveConfig *SyncDriveConfig syncOption SyncOption PanUser *config.PanUser - DriveId string PanClient *config.PanClient SyncConfigFolderPath string @@ -57,11 +57,10 @@ var ( ErrSyncTaskListEmpty error = fmt.Errorf("no sync task") ) -func NewSyncTaskManager(user *config.PanUser, driveId string, panClient *config.PanClient, syncConfigFolderPath string, +func NewSyncTaskManager(user *config.PanUser, panClient *config.PanClient, syncConfigFolderPath string, option SyncOption) *SyncTaskManager { return &SyncTaskManager{ PanUser: user, - DriveId: driveId, PanClient: panClient, SyncConfigFolderPath: syncConfigFolderPath, syncOption: option, @@ -80,6 +79,7 @@ func (m *SyncTaskManager) parseConfigFile() error { "localFolderPath": "D:\\smb\\datadisk\\game", "panFolderPath": "/sync_drive/game", "mode": "sync", + "driveName": "backup", "lastSyncTime": "" } ] @@ -139,6 +139,16 @@ func (m *SyncTaskManager) Start(tasks []*SyncTask) (bool, error) { if len(task.Id) == 0 { task.Id = utils.UuidStr() } + // check driveId + if strings.ToLower(task.DriveName) == "backup" { + task.DriveId = m.PanUser.DriveList.GetFileDriveId() + } else if strings.ToLower(task.DriveName) == "resource" { + task.DriveId = m.PanUser.DriveList.GetResourceDriveId() + } + if len(task.DriveId) == 0 { + task.DriveId = m.PanUser.DriveList.GetFileDriveId() + } + // check userId if len(task.UserId) > 0 { if task.UserId != m.PanUser.UserId { @@ -161,7 +171,6 @@ func (m *SyncTaskManager) Start(tasks []*SyncTask) (bool, error) { continue } task.panUser = m.PanUser - task.DriveId = m.DriveId task.syncDbFolderPath = m.SyncConfigFolderPath task.panClient = m.PanClient task.syncOption = m.syncOption