CasaOS/service/casa.go

304 lines
8.6 KiB
Go
Raw Normal View History

2021-09-26 02:35:02 +00:00
package service
import (
2022-02-17 10:43:25 +00:00
"encoding/json"
2021-09-26 02:35:02 +00:00
json2 "encoding/json"
2021-12-29 08:42:20 +00:00
"fmt"
"sort"
"strconv"
"time"
2021-09-27 06:17:36 +00:00
"github.com/IceWhaleTech/CasaOS/model"
"github.com/IceWhaleTech/CasaOS/pkg/config"
"github.com/IceWhaleTech/CasaOS/pkg/utils/file"
"github.com/IceWhaleTech/CasaOS/pkg/utils/httper"
2021-09-27 06:17:36 +00:00
httper2 "github.com/IceWhaleTech/CasaOS/pkg/utils/httper"
"github.com/IceWhaleTech/CasaOS/pkg/utils/loger"
"github.com/pkg/errors"
2021-09-26 02:35:02 +00:00
"github.com/tidwall/gjson"
"go.uber.org/zap"
2021-09-26 02:35:02 +00:00
)
type CasaService interface {
GetServerList(index, size, tp, categoryId, key string) (model.ServerAppListCollection, error)
GetServerCategoryList() (list []model.CategoryList, err error)
GetServerAppInfo(id, t string, language string) (model.ServerAppList, error)
2021-11-25 09:35:01 +00:00
ShareAppFile(body []byte) string
GetCasaosVersion() model.Version
AsyncGetServerList() (collection model.ServerAppListCollection, err error)
AsyncGetServerCategoryList() ([]model.CategoryList, error)
2021-09-26 02:35:02 +00:00
}
type casaService struct {
2021-09-26 02:35:02 +00:00
}
2021-11-25 09:35:01 +00:00
func (o *casaService) ShareAppFile(body []byte) string {
head := make(map[string]string)
head["Authorization"] = GetToken()
content := httper2.Post(config.ServerInfo.ServerApi+"/v1/community/add", body, "application/json", head)
return content
}
func (o *casaService) GetServerList(index, size, tp, categoryId, key string) (model.ServerAppListCollection, error) {
2021-12-29 08:42:20 +00:00
keyName := fmt.Sprintf("list_%s_%s_%s_%s_%s", index, size, tp, categoryId, "en")
collection := model.ServerAppListCollection{}
2021-12-29 08:42:20 +00:00
if result, ok := Cache.Get(keyName); ok {
res, ok := result.(string)
if ok {
json2.Unmarshal([]byte(res), &collection)
return collection, nil
2021-12-29 08:42:20 +00:00
}
}
2021-09-26 02:35:02 +00:00
collectionStr := file.ReadFullFile(config.AppInfo.DBPath + "/app_list.json")
2021-09-26 02:35:02 +00:00
err := json2.Unmarshal(collectionStr, &collection)
if err != nil {
loger.Error("marshal error", zap.Any("err", err), zap.Any("content", string(collectionStr)))
collection, err = o.AsyncGetServerList()
if err != nil {
return collection, err
}
}
2021-09-26 02:35:02 +00:00
go o.AsyncGetServerList()
2021-09-26 02:35:02 +00:00
if categoryId != "0" {
categoryInt, _ := strconv.Atoi(categoryId)
nList := []model.ServerAppList{}
for _, v := range collection.List {
if v.CategoryId == categoryInt {
nList = append(nList, v)
}
}
collection.List = nList
nCommunity := []model.ServerAppList{}
for _, v := range collection.Community {
if v.CategoryId == categoryInt {
nCommunity = append(nCommunity, v)
}
}
collection.Community = nCommunity
}
if tp != "name" {
if tp == "new" {
sort.Slice(collection.List, func(i, j int) bool {
return collection.List[i].CreatedAt.After(collection.List[j].CreatedAt)
})
sort.Slice(collection.Community, func(i, j int) bool {
return collection.Community[i].CreatedAt.After(collection.Community[j].CreatedAt)
})
} else if tp == "rank" {
sort.Slice(collection.List, func(i, j int) bool {
return collection.List[i].QueryCount > collection.List[j].QueryCount
})
sort.Slice(collection.Community, func(i, j int) bool {
return collection.Community[i].QueryCount > collection.Community[j].QueryCount
})
}
}
sizeInt, _ := strconv.Atoi(size)
if index != "1" {
indexInt, _ := strconv.Atoi(index)
collection.List = collection.List[(indexInt-1)*sizeInt : indexInt*sizeInt]
collection.Community = collection.Community[(indexInt-1)*sizeInt : indexInt*sizeInt]
} else {
if len(collection.List) > sizeInt {
collection.List = collection.List[:sizeInt]
}
if len(collection.Community) > sizeInt {
collection.Community = collection.Community[:sizeInt]
}
}
2021-09-26 02:35:02 +00:00
if len(collection.List) > 0 {
by, _ := json.Marshal(collection)
Cache.Set(keyName, string(by), time.Minute*10)
2021-12-29 08:42:20 +00:00
}
return collection, nil
2021-09-26 02:35:02 +00:00
}
func (o *casaService) AsyncGetServerList() (collection model.ServerAppListCollection, err error) {
results := file.ReadFullFile(config.AppInfo.DBPath + "/app_list.json")
errr := json2.Unmarshal(results, &collection)
if errr != nil {
loger.Error("marshal error", zap.Any("err", err), zap.Any("content", string(results)))
} else {
if collection.Version == o.GetCasaosVersion().Version {
return collection, err
}
}
2021-09-26 02:35:02 +00:00
head := make(map[string]string)
head["Authorization"] = GetToken()
listS := httper2.Get(config.ServerInfo.ServerApi+"/v2/app/newlist?index=1&size=1000&rank=name&category_id=0&key=&language=en", head)
listModel := []model.ServerAppList{}
communityModel := []model.ServerAppList{}
recommendModel := []model.ServerAppList{}
json2.Unmarshal([]byte(gjson.Get(listS, "data.list").String()), &listModel)
json2.Unmarshal([]byte(gjson.Get(listS, "data.recommend").String()), &recommendModel)
json2.Unmarshal([]byte(gjson.Get(listS, "data.community").String()), &communityModel)
if len(listModel) > 0 {
collection.Community = communityModel
collection.List = listModel
collection.Recommend = recommendModel
collection.Version = o.GetCasaosVersion().Version
var by []byte
by, err = json.Marshal(collection)
if err != nil {
loger.Error("marshal error", zap.Any("err", err))
}
file.WriteToPath(by, config.AppInfo.DBPath, "app_list.json")
}
return
}
// func (o *casaService) GetServerCategoryList() (list []model.ServerCategoryList) {
// keyName := fmt.Sprintf("category_list")
// if result, ok := Cache.Get(keyName); ok {
// res, ok := result.(string)
// if ok {
// json2.Unmarshal([]byte(gjson.Get(res, "data").String()), &list)
// return list
// }
// }
// head := make(map[string]string)
// head["Authorization"] = GetToken()
// listS := httper2.Get(config.ServerInfo.ServerApi+"/v2/app/category", head)
// json2.Unmarshal([]byte(gjson.Get(listS, "data").String()), &list)
// if len(list) > 0 {
// Cache.Set(keyName, listS, time.Hour*24)
// }
// return list
// }
func (o *casaService) GetServerCategoryList() (list []model.CategoryList, err error) {
category := model.ServerCategoryList{}
results := file.ReadFullFile(config.AppInfo.DBPath + "/app_category.json")
err = json2.Unmarshal(results, &category)
if err != nil {
loger.Error("marshal error", zap.Any("err", err), zap.Any("content", string(results)))
return o.AsyncGetServerCategoryList()
}
go o.AsyncGetServerCategoryList()
return category.Item, err
}
func (o *casaService) AsyncGetServerCategoryList() ([]model.CategoryList, error) {
list := model.ServerCategoryList{}
results := file.ReadFullFile(config.AppInfo.DBPath + "/app_category.json")
err := json2.Unmarshal(results, &list)
if err != nil {
loger.Error("marshal error", zap.Any("err", err), zap.Any("content", string(results)))
} else {
if list.Version == o.GetCasaosVersion().Version {
return list.Item, nil
}
}
item := []model.CategoryList{}
head := make(map[string]string)
head["Authorization"] = GetToken()
listS := httper2.Get(config.ServerInfo.ServerApi+"/v2/app/category", head)
if len(listS) == 0 {
return item, errors.New("server error")
}
json2.Unmarshal([]byte(gjson.Get(listS, "data").String()), &item)
if len(item) > 0 {
list.Version = o.GetCasaosVersion().Version
list.Item = item
by, err := json.Marshal(list)
if err != nil {
loger.Error("marshal error", zap.Any("err", err))
}
file.WriteToPath(by, config.AppInfo.DBPath, "app_category.json")
}
return item, nil
}
func (o *casaService) GetServerAppInfo(id, t string, language string) (model.ServerAppList, error) {
head := make(map[string]string)
head["Authorization"] = GetToken()
2022-01-26 10:50:34 +00:00
infoS := httper2.Get(config.ServerInfo.ServerApi+"/v2/app/info/"+id+"?t="+t+"&language="+language, head)
info := model.ServerAppList{}
if infoS == "" {
return info, errors.New("server error")
}
err := json2.Unmarshal([]byte(gjson.Get(infoS, "data").String()), &info)
if err != nil {
fmt.Println(infoS)
return info, err
}
return info, nil
}
func GetToken() string {
2021-09-26 02:35:02 +00:00
t := make(chan string)
keyName := "casa_token"
2021-09-26 02:35:02 +00:00
var auth string
if result, ok := Cache.Get(keyName); ok {
auth, ok = result.(string)
if ok {
return auth
}
}
2021-09-26 02:35:02 +00:00
go func() {
str := httper2.Get(config.ServerInfo.ServerApi+"/token", nil)
t <- gjson.Get(str, "data").String()
}()
auth = <-t
2021-09-26 02:35:02 +00:00
Cache.SetDefault(keyName, auth)
return auth
2021-09-26 02:35:02 +00:00
}
/**
* @description: get remote version
* @return {model.Version}
*/
func (o *casaService) GetCasaosVersion() model.Version {
keyName := "casa_version"
var dataStr string
var version model.Version
if result, ok := Cache.Get(keyName); ok {
dataStr, ok = result.(string)
if ok {
data := gjson.Get(dataStr, "data")
json2.Unmarshal([]byte(data.String()), &version)
return version
}
}
2022-02-17 10:43:25 +00:00
v := httper.OasisGet(config.ServerInfo.ServerApi + "/v1/sys/version")
data := gjson.Get(v, "data")
json2.Unmarshal([]byte(data.String()), &version)
2022-02-17 10:43:25 +00:00
if len(version.Version) > 0 {
Cache.Set(keyName, v, time.Minute*20)
}
2022-02-17 10:43:25 +00:00
return version
2022-02-17 10:43:25 +00:00
}
func NewCasaService() CasaService {
return &casaService{}
2021-09-26 02:35:02 +00:00
}