alist/model/account.go

113 lines
2.7 KiB
Go
Raw Normal View History

2021-10-26 14:28:37 +00:00
package model
import (
"github.com/Xhofe/alist/conf"
2021-11-06 09:25:07 +00:00
"github.com/robfig/cron/v3"
2021-10-30 09:34:34 +00:00
"time"
2021-10-26 14:28:37 +00:00
)
type Account struct {
2021-11-13 08:49:03 +00:00
ID uint `json:"id" gorm:"primaryKey"`
Name string `json:"name" gorm:"unique" binding:"required"`
2021-11-04 05:23:17 +00:00
Index int `json:"index"`
2021-10-27 10:59:03 +00:00
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"`
2021-11-04 05:23:17 +00:00
SiteId string `json:"site_id"`
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 {
2021-11-13 08:49:03 +00:00
if err := conf.DB.Save(&account).Error; err != nil {
return err
}
RegisterAccount(account)
return nil
}
func CreateAccount(account Account) error {
if err := conf.DB.Create(&account).Error; err != nil {
2021-10-26 14:28:37 +00:00
return err
}
RegisterAccount(account)
return nil
}
2021-11-13 09:10:44 +00:00
func DeleteAccount(id uint) error {
var account Account
account.ID = id
if err := conf.DB.First(&account).Error; err != nil {
return err
2021-10-26 14:28:37 +00:00
}
2021-11-13 09:10:44 +00:00
name := account.Name
2021-11-06 09:25:07 +00:00
conf.Cron.Remove(cron.EntryID(account.CronId))
2021-10-26 14:28:37 +00:00
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
}
2021-11-03 02:37:33 +00:00
func GetAccountFiles() ([]*File, error) {
2021-10-26 14:28:37 +00:00
files := make([]*File, 0)
2021-11-03 02:37:33 +00:00
var accounts []Account
if err := conf.DB.Order("`index`").Find(&accounts).Error; err != nil {
return nil, err
}
for _, v := range accounts {
2021-10-26 14:28:37 +00:00
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
})
}
2021-11-03 02:37:33 +00:00
return files, nil
2021-10-26 14:28:37 +00:00
}
2021-11-03 02:37:33 +00:00
func GetAccounts() ([]Account, error) {
var accounts []Account
if err := conf.DB.Order("`index`").Find(&accounts).Error; err != nil {
return nil, err
2021-10-26 14:28:37 +00:00
}
2021-11-03 02:37:33 +00:00
return accounts, nil
2021-10-26 14:28:37 +00:00
}