🐛 fix add account

This commit is contained in:
微凉 2021-11-13 19:31:35 +08:00
parent 48a65784c7
commit b785945210
5 changed files with 19 additions and 16 deletions

View File

@ -31,7 +31,7 @@
- 本地存储 - 本地存储
- 阿里云盘 - 阿里云盘
- Onedrive - Onedrive/世纪互联
- ... - ...
### 如何使用 ### 如何使用

View File

@ -191,7 +191,7 @@ func (a AliDrive) GetFiles(fileId string, account *model.Account) ([]AliFile, er
if err != nil { if err != nil {
return nil, err return nil, err
} else { } else {
_ = model.SaveAccount(*account) _ = model.SaveAccount(account)
return a.GetFiles(fileId, account) return a.GetFiles(fileId, account)
} }
} }
@ -312,7 +312,7 @@ func (a AliDrive) Link(path string, account *model.Account) (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} else { } else {
_ = model.SaveAccount(*account) _ = model.SaveAccount(account)
return a.Link(path, account) return a.Link(path, account)
} }
} }
@ -372,13 +372,13 @@ func (a AliDrive) Save(account *model.Account, old *model.Account) error {
return return
} }
err = a.RefreshToken(&newAccount) err = a.RefreshToken(&newAccount)
_ = model.SaveAccount(newAccount) _ = model.SaveAccount(&newAccount)
}) })
if err != nil { if err != nil {
return err return err
} }
account.CronId = int(cronId) account.CronId = int(cronId)
err = model.SaveAccount(*account) err = model.SaveAccount(account)
if err != nil { if err != nil {
return err return err
} }

View File

@ -282,13 +282,13 @@ func (o Onedrive) Save(account *model.Account, old *model.Account) error {
return return
} }
err = o.RefreshToken(&newAccount) err = o.RefreshToken(&newAccount)
_ = model.SaveAccount(newAccount) _ = model.SaveAccount(&newAccount)
}) })
if err != nil { if err != nil {
return err return err
} }
account.CronId = int(cronId) account.CronId = int(cronId)
err = model.SaveAccount(*account) err = model.SaveAccount(account)
if err != nil { if err != nil {
return err return err
} }
@ -296,7 +296,7 @@ func (o Onedrive) Save(account *model.Account, old *model.Account) error {
} }
func (o Onedrive) Proxy(c *gin.Context) { func (o Onedrive) Proxy(c *gin.Context) {
c.Request.Header.Del("Origin")
} }
func (o Onedrive) Preview(path string, account *model.Account) (interface{}, error) { func (o Onedrive) Preview(path string, account *model.Account) (interface{}, error) {

View File

@ -37,19 +37,19 @@ type Account struct {
var accountsMap = map[string]Account{} var accountsMap = map[string]Account{}
// SaveAccount save account to database // SaveAccount save account to database
func SaveAccount(account Account) error { func SaveAccount(account *Account) error {
if err := conf.DB.Save(&account).Error; err != nil { if err := conf.DB.Save(account).Error; err != nil {
return err return err
} }
RegisterAccount(account) RegisterAccount(*account)
return nil return nil
} }
func CreateAccount(account Account) error { func CreateAccount(account *Account) error {
if err := conf.DB.Create(&account).Error; err != nil { if err := conf.DB.Create(account).Error; err != nil {
return err return err
} }
RegisterAccount(account) RegisterAccount(*account)
return nil return nil
} }

View File

@ -5,6 +5,7 @@ import (
"github.com/Xhofe/alist/drivers" "github.com/Xhofe/alist/drivers"
"github.com/Xhofe/alist/model" "github.com/Xhofe/alist/model"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"strconv" "strconv"
"time" "time"
) )
@ -31,9 +32,10 @@ func CreateAccount(c *gin.Context) {
} }
now := time.Now() now := time.Now()
req.UpdatedAt = &now req.UpdatedAt = &now
if err := model.CreateAccount(req); err != nil { if err := model.CreateAccount(&req); err != nil {
ErrorResp(c, err, 500) ErrorResp(c, err, 500)
} else { } else {
log.Debugf("new account: %+v", req)
err = driver.Save(&req, nil) err = driver.Save(&req, nil)
if err != nil { if err != nil {
ErrorResp(c, err, 500) ErrorResp(c, err, 500)
@ -57,9 +59,10 @@ func SaveAccount(c *gin.Context) {
old, ok := model.GetAccount(req.Name) old, ok := model.GetAccount(req.Name)
now := time.Now() now := time.Now()
req.UpdatedAt = &now req.UpdatedAt = &now
if err := model.SaveAccount(req); err != nil { if err := model.SaveAccount(&req); err != nil {
ErrorResp(c, err, 500) ErrorResp(c, err, 500)
} else { } else {
log.Debugf("save account: %+v", req)
if ok { if ok {
err = driver.Save(&req, &old) err = driver.Save(&req, &old)
} else { } else {