🐛 fix change name error

pull/548/head
微凉 2021-11-15 14:54:22 +08:00
parent aeb72320ca
commit 27e61c9eb8
4 changed files with 26 additions and 6 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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

View File

@ -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