alist/model/account.go

130 lines
3.6 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-12-08 12:00:52 +00:00
ID uint `json:"id" gorm:"primaryKey"` // 唯一ID
Name string `json:"name" gorm:"unique" binding:"required"` // 唯一名称
Index int `json:"index"` // 序号 用于排序
Type string `json:"type"` // 类型即driver
2021-10-27 10:59:03 +00:00
Username string `json:"username"`
Password string `json:"password"`
RefreshToken string `json:"refresh_token"`
AccessToken string `json:"access_token"`
RootFolder string `json:"root_folder"`
2021-12-08 12:00:52 +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"`
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"`
2021-12-16 07:44:18 +00:00
InternalType string `json:"internal_type"`
2021-12-19 09:10:20 +00:00
WebdavProxy bool `json:"webdav_proxy"` // 开启之后只会webdav走中转
Proxy bool `json:"proxy"` // 是否中转,开启之后web和webdav都会走中转
2021-12-08 12:00:52 +00:00
//AllowProxy bool `json:"allow_proxy"` // 是否允许中转下载
2021-12-19 09:10:20 +00:00
DownProxyUrl string `json:"down_proxy_url"` // 用于中转下载服务的URL 两处 1. path请求中返回的链接 2. down下载时进行302
APIProxyUrl string `json:"api_proxy_url"` // 用于中转api的地址
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-11-13 11:31:35 +00:00
func SaveAccount(account *Account) error {
if err := conf.DB.Save(account).Error; err != nil {
2021-11-13 08:49:03 +00:00
return err
}
2021-11-13 11:31:35 +00:00
RegisterAccount(*account)
2021-11-13 08:49:03 +00:00
return nil
}
2021-11-13 11:31:35 +00:00
func CreateAccount(account *Account) error {
if err := conf.DB.Create(account).Error; err != nil {
2021-10-26 14:28:37 +00:00
return err
}
2021-11-13 11:31:35 +00:00
RegisterAccount(*account)
2021-10-26 14:28:37 +00:00
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
}
2021-11-15 06:54:22 +00:00
func DeleteAccountFromMap(name string) {
delete(accountsMap, name)
}
2021-10-26 14:28:37 +00:00
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-15 06:54:22 +00:00
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
}
2021-11-23 07:58:03 +00:00
func GetAccountFiles() ([]File, error) {
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-11-23 07:58:03 +00:00
files = append(files, File{
2021-10-26 14:28:37 +00:00
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
}