From 2473309a5183ad3c9ab2569d0aaa25437bc2b23e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=AE=E5=87=89?= Date: Sat, 15 Jan 2022 19:36:37 +0800 Subject: [PATCH] :sparkler: execute save while delete account --- drivers/123/driver.go | 3 +++ drivers/139/driver.go | 3 +++ drivers/189/driver.go | 5 ++++- drivers/alidrive/driver.go | 3 +++ drivers/alist/driver.go | 3 +++ drivers/ftp/driver.go | 3 +++ drivers/google/driver.go | 3 +++ drivers/lanzou/driver.go | 3 +++ drivers/mediatrack/driver.go | 3 +++ drivers/native/driver.go | 3 +++ drivers/onedrive/driver.go | 9 ++++++--- drivers/pikpak/driver.go | 3 +++ drivers/s3/driver.go | 3 +++ drivers/shandian/driver.go | 3 +++ drivers/teambition/driver.go | 3 +++ drivers/webdav/driver.go | 3 +++ model/account.go | 10 ++++++++-- 17 files changed, 60 insertions(+), 6 deletions(-) diff --git a/drivers/123/driver.go b/drivers/123/driver.go index 3ea51bfc..f36b8c5b 100644 --- a/drivers/123/driver.go +++ b/drivers/123/driver.go @@ -65,6 +65,9 @@ func (driver Pan123) Items() []base.Item { } func (driver Pan123) Save(account *model.Account, old *model.Account) error { + if account == nil { + return nil + } if account.RootFolder == "" { account.RootFolder = "0" } diff --git a/drivers/139/driver.go b/drivers/139/driver.go index 84286c9a..c7e45841 100644 --- a/drivers/139/driver.go +++ b/drivers/139/driver.go @@ -64,6 +64,9 @@ func (driver Cloud139) Items() []base.Item { } func (driver Cloud139) Save(account *model.Account, old *model.Account) error { + if account == nil { + return nil + } _, err := driver.Request("/orchestration/personalCloud/user/v1.0/qryUserExternInfo", base.Post, nil, nil, nil, base.Json{ "qryUserExternInfoReq": base.Json{ "commonAccountInfo": base.Json{ diff --git a/drivers/189/driver.go b/drivers/189/driver.go index f01d718a..d9e5b282 100644 --- a/drivers/189/driver.go +++ b/drivers/189/driver.go @@ -73,9 +73,12 @@ func (driver Cloud189) Items() []base.Item { } func (driver Cloud189) Save(account *model.Account, old *model.Account) error { - if old != nil && old.Name != account.Name { + if old != nil { delete(client189Map, old.Name) } + if account == nil { + return nil + } if err := driver.Login(account); err != nil { account.Status = err.Error() _ = model.SaveAccount(account) diff --git a/drivers/alidrive/driver.go b/drivers/alidrive/driver.go index 4fb3b4ff..85d80d7e 100644 --- a/drivers/alidrive/driver.go +++ b/drivers/alidrive/driver.go @@ -67,6 +67,9 @@ func (driver AliDrive) Save(account *model.Account, old *model.Account) error { if old != nil { conf.Cron.Remove(cron.EntryID(old.CronId)) } + if account == nil { + return nil + } if account.RootFolder == "" { account.RootFolder = "root" } diff --git a/drivers/alist/driver.go b/drivers/alist/driver.go index c915e3ac..044cba70 100644 --- a/drivers/alist/driver.go +++ b/drivers/alist/driver.go @@ -48,6 +48,9 @@ func (driver Alist) Items() []base.Item { } func (driver Alist) Save(account *model.Account, old *model.Account) error { + if account == nil { + return nil + } account.SiteUrl = strings.TrimRight(account.SiteUrl, "/") if account.RootFolder == "" { account.RootFolder = "/" diff --git a/drivers/ftp/driver.go b/drivers/ftp/driver.go index 6295a95f..6132c8a4 100644 --- a/drivers/ftp/driver.go +++ b/drivers/ftp/driver.go @@ -61,6 +61,9 @@ func (driver FTP) Save(account *model.Account, old *model.Account) error { delete(connMap, old.Name) } } + if account == nil { + return nil + } if account.RootFolder == "" { account.RootFolder = "/" } diff --git a/drivers/google/driver.go b/drivers/google/driver.go index f3caebb6..af64ebb8 100644 --- a/drivers/google/driver.go +++ b/drivers/google/driver.go @@ -67,6 +67,9 @@ func (driver GoogleDrive) Items() []base.Item { } func (driver GoogleDrive) Save(account *model.Account, old *model.Account) error { + if account == nil { + return nil + } account.Proxy = true err := driver.RefreshToken(account) if err != nil { diff --git a/drivers/lanzou/driver.go b/drivers/lanzou/driver.go index edcbf069..5632cf26 100644 --- a/drivers/lanzou/driver.go +++ b/drivers/lanzou/driver.go @@ -53,6 +53,9 @@ func (driver Lanzou) Items() []base.Item { } func (driver Lanzou) Save(account *model.Account, old *model.Account) error { + if account == nil { + return nil + } if account.InternalType == "cookie" { if account.RootFolder == "" { account.RootFolder = "-1" diff --git a/drivers/mediatrack/driver.go b/drivers/mediatrack/driver.go index de14c759..d5be0e3f 100644 --- a/drivers/mediatrack/driver.go +++ b/drivers/mediatrack/driver.go @@ -62,6 +62,9 @@ func (driver MediaTrack) Items() []base.Item { } func (driver MediaTrack) Save(account *model.Account, old *model.Account) error { + if account == nil { + return nil + } return nil } diff --git a/drivers/native/driver.go b/drivers/native/driver.go index 9598c276..7bcbe079 100644 --- a/drivers/native/driver.go +++ b/drivers/native/driver.go @@ -39,6 +39,9 @@ func (driver Native) Items() []base.Item { } func (driver Native) Save(account *model.Account, old *model.Account) error { + if account == nil { + return nil + } log.Debugf("save a account: [%s]", account.Name) if !utils.Exists(account.RootFolder) { account.Status = fmt.Sprintf("[%s] not exist", account.RootFolder) diff --git a/drivers/onedrive/driver.go b/drivers/onedrive/driver.go index 5fa0d96e..6355c493 100644 --- a/drivers/onedrive/driver.go +++ b/drivers/onedrive/driver.go @@ -92,13 +92,16 @@ func (driver Onedrive) Items() []base.Item { } func (driver Onedrive) Save(account *model.Account, old *model.Account) error { + if old != nil { + conf.Cron.Remove(cron.EntryID(old.CronId)) + } + if account == nil { + return nil + } _, ok := onedriveHostMap[account.Zone] if !ok { return fmt.Errorf("no [%s] zone", account.Zone) } - if old != nil { - conf.Cron.Remove(cron.EntryID(old.CronId)) - } account.RootFolder = utils.ParsePath(account.RootFolder) err := driver.RefreshToken(account) if err != nil { diff --git a/drivers/pikpak/driver.go b/drivers/pikpak/driver.go index f759659b..73cb118a 100644 --- a/drivers/pikpak/driver.go +++ b/drivers/pikpak/driver.go @@ -51,6 +51,9 @@ func (driver PikPak) Items() []base.Item { } func (driver PikPak) Save(account *model.Account, old *model.Account) error { + if account == nil { + return nil + } err := driver.Login(account) return err } diff --git a/drivers/s3/driver.go b/drivers/s3/driver.go index 870e83de..b3b1e231 100644 --- a/drivers/s3/driver.go +++ b/drivers/s3/driver.go @@ -79,6 +79,9 @@ func (driver S3) Items() []base.Item { } func (driver S3) Save(account *model.Account, old *model.Account) error { + if account == nil { + return nil + } if account.Limit == 0 { account.Limit = 4 } diff --git a/drivers/shandian/driver.go b/drivers/shandian/driver.go index 5fb0aed9..d3b66094 100644 --- a/drivers/shandian/driver.go +++ b/drivers/shandian/driver.go @@ -48,6 +48,9 @@ func (driver Shandian) Items() []base.Item { } func (driver Shandian) Save(account *model.Account, old *model.Account) error { + if account == nil { + return nil + } if account.RootFolder == "" { account.RootFolder = "0" } diff --git a/drivers/teambition/driver.go b/drivers/teambition/driver.go index 63c7ac64..028b3d80 100644 --- a/drivers/teambition/driver.go +++ b/drivers/teambition/driver.go @@ -64,6 +64,9 @@ func (driver Teambition) Items() []base.Item { } func (driver Teambition) Save(account *model.Account, old *model.Account) error { + if account == nil { + return nil + } _, err := driver.Request("/api/v2/roles", base.Get, nil, nil, nil, nil, nil, account) return err } diff --git a/drivers/webdav/driver.go b/drivers/webdav/driver.go index f3305317..0385f223 100644 --- a/drivers/webdav/driver.go +++ b/drivers/webdav/driver.go @@ -45,6 +45,9 @@ func (driver WebDav) Items() []base.Item { } func (driver WebDav) Save(account *model.Account, old *model.Account) error { + if account == nil { + return nil + } account.Status = "work" _ = model.SaveAccount(account) return nil diff --git a/model/account.go b/model/account.go index 0bd22388..4dac72aa 100644 --- a/model/account.go +++ b/model/account.go @@ -2,7 +2,8 @@ package model import ( "github.com/Xhofe/alist/conf" - "github.com/robfig/cron/v3" + "github.com/Xhofe/alist/drivers/base" + log "github.com/sirupsen/logrus" "time" ) @@ -71,7 +72,12 @@ func DeleteAccount(id uint) error { return err } name := account.Name - conf.Cron.Remove(cron.EntryID(account.CronId)) + driver, ok := base.GetDriver(account.Type) + if ok { + _ = driver.Save(nil, &account) + } else { + log.Errorf("no driver: %s", account.Type) + } if err := conf.DB.Delete(&account).Error; err != nil { return err }