alist/model/account.go

94 lines
2.2 KiB
Go
Raw Normal View History

2021-10-26 14:28:37 +00:00
package model
import (
"github.com/Xhofe/alist/conf"
2021-10-30 09:34:34 +00:00
"time"
2021-10-26 14:28:37 +00:00
)
type Account struct {
2021-10-27 10:59:03 +00:00
Name string `json:"name" gorm:"primaryKey" validate:"required"`
Type string `json:"type"`
Username string `json:"username"`
Password string `json:"password"`
RefreshToken string `json:"refresh_token"`
AccessToken string `json:"access_token"`
RootFolder string `json:"root_folder"`
2021-10-29 16:35:29 +00:00
Status string `json:"status"`
2021-10-27 10:59:03 +00:00
CronId int
DriveId string
2021-10-30 09:34:34 +00:00
Limit int `json:"limit"`
OrderBy string `json:"order_by"`
OrderDirection string `json:"order_direction"`
Proxy bool `json:"proxy"`
UpdatedAt *time.Time `json:"updated_at"`
2021-11-02 11:25:54 +00:00
Search bool `json:"search"`
2021-11-02 15:53:05 +00:00
ClientId string `json:"client_id"`
ClientSecret string `json:"client_secret"`
Zone string `json:"zone"`
RedirectUri string `json:"redirect_uri"`
SiteUrl string `json:"site_url"`
SiteId string
OnedriveType string `json:"onedrive_type"`
2021-10-26 14:28:37 +00:00
}
var accountsMap = map[string]Account{}
2021-10-27 14:45:36 +00:00
// SaveAccount save account to database
2021-10-26 14:28:37 +00:00
func SaveAccount(account Account) error {
if err := conf.DB.Save(account).Error; err != nil {
return err
}
RegisterAccount(account)
return nil
}
func DeleteAccount(name string) error {
account := Account{
Name: name,
}
if err := conf.DB.Delete(&account).Error; err != nil {
return err
}
delete(accountsMap, name)
return nil
}
func AccountsCount() int {
return len(accountsMap)
}
func RegisterAccount(account Account) {
accountsMap[account.Name] = account
}
func GetAccount(name string) (Account, bool) {
if len(accountsMap) == 1 {
for _, v := range accountsMap {
return v, true
}
}
account, ok := accountsMap[name]
return account, ok
}
func GetAccountFiles() []*File {
files := make([]*File, 0)
for _, v := range accountsMap {
files = append(files, &File{
Name: v.Name,
Size: 0,
Type: conf.FOLDER,
2021-10-30 09:34:34 +00:00
UpdatedAt: v.UpdatedAt,
2021-10-26 14:28:37 +00:00
})
}
return files
}
2021-10-29 16:35:29 +00:00
func GetAccounts() []Account {
accounts := make([]Account, 0)
2021-10-26 14:28:37 +00:00
for _, v := range accountsMap {
2021-10-29 16:35:29 +00:00
accounts = append(accounts, v)
2021-10-26 14:28:37 +00:00
}
return accounts
}