diff --git a/drivers/alidrive.go b/drivers/alidrive.go index 736c8dfc..ecb94549 100644 --- a/drivers/alidrive.go +++ b/drivers/alidrive.go @@ -347,6 +347,7 @@ func (a AliDrive) RefreshToken(account *model.Account) error { func (a AliDrive) Save(account *model.Account, old *model.Account) error { if old != nil { conf.Cron.Remove(cron.EntryID(old.CronId)) + model.DeleteAccountFromMap(old.Name) } if account.RootFolder == "" { account.RootFolder = "root" @@ -367,7 +368,9 @@ func (a AliDrive) Save(account *model.Account, old *model.Account) error { account.DriveId = resp["default_drive_id"].(string) cronId, err := conf.Cron.AddFunc("@every 2h", func() { name := account.Name + log.Debugf("ali account name: %s", name) newAccount, ok := model.GetAccount(name) + log.Debugf("ali account: %+v", newAccount) if !ok { return } diff --git a/drivers/onedrive.go b/drivers/onedrive.go index f963d9b6..72630d62 100644 --- a/drivers/onedrive.go +++ b/drivers/onedrive.go @@ -8,6 +8,7 @@ import ( "github.com/gin-gonic/gin" "github.com/go-resty/resty/v2" "github.com/robfig/cron/v3" + log "github.com/sirupsen/logrus" "path/filepath" "time" ) @@ -269,6 +270,7 @@ func (o Onedrive) Save(account *model.Account, old *model.Account) error { } if old != nil { conf.Cron.Remove(cron.EntryID(old.CronId)) + model.DeleteAccountFromMap(old.Name) } account.RootFolder = utils.ParsePath(account.RootFolder) err := o.RefreshToken(account) @@ -277,7 +279,9 @@ func (o Onedrive) Save(account *model.Account, old *model.Account) error { } cronId, err := conf.Cron.AddFunc("@every 1h", func() { name := account.Name + log.Debugf("onedrive account name: %s", name) newAccount, ok := model.GetAccount(name) + log.Debugf("onedrive account: %+v", newAccount) if !ok { return } diff --git a/model/account.go b/model/account.go index 39094ede..efc35b9e 100644 --- a/model/account.go +++ b/model/account.go @@ -68,6 +68,10 @@ func DeleteAccount(id uint) error { return nil } +func DeleteAccountFromMap(name string) { + delete(accountsMap, name) +} + func AccountsCount() int { return len(accountsMap) } @@ -86,6 +90,15 @@ func GetAccount(name string) (Account, bool) { return account, ok } +func GetAccountById(id uint) (*Account, error) { + var account Account + account.ID = id + if err := conf.DB.First(&account).Error; err != nil { + return nil, err + } + return &account, nil +} + func GetAccountFiles() ([]*File, error) { files := make([]*File, 0) var accounts []Account diff --git a/server/account.go b/server/account.go index 31587716..9cd7bf5f 100644 --- a/server/account.go +++ b/server/account.go @@ -56,18 +56,18 @@ func SaveAccount(c *gin.Context) { ErrorResp(c, fmt.Errorf("no [%s] driver", req.Type), 400) return } - old, ok := model.GetAccount(req.Name) + old, err := model.GetAccountById(req.ID) + if err != nil { + ErrorResp(c, err, 400) + return + } now := time.Now() req.UpdatedAt = &now if err := model.SaveAccount(&req); err != nil { ErrorResp(c, err, 500) } else { log.Debugf("save account: %+v", req) - if ok { - err = driver.Save(&req, &old) - } else { - err = driver.Save(&req, nil) - } + err = driver.Save(&req, old) if err != nil { ErrorResp(c, err, 500) return